mysql implements the operation of setting multiple primary keys

mysql implements the operation of setting multiple primary keys

User table, ID number must be unique, mobile phone number, email address must be unique

Implementation:

The table structure does not need to be changed. A primary key ID plus index implementation

As shown in the figure, set the index type to Unique to select the only field and give it a name. The index method is btree. Ok~

Supplement: MySQL implements multi-table primary key non-duplicate

There are two tables in the same database, and the fields are the same, but the stored data needs to be distinguished. But the primary key cannot be repeated. The specific implementation is as follows:

Create a new database mytest

Create new user table and admin table

CREATE TABLE `user` (
 `user_id` INT(11) NOT NULL,
 `user_name` VARCHAR(255) NOT NULL,
 `password` VARCHAR(255) NOT NULL,
 `phone` VARCHAR(255) NOT NULL,
 PRIMARY KEY (`user_id`)
)
COMMENT = 'User table'
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
CREATE TABLE `admin` (
 `user_id` INT(11) NOT NULL,
 `user_name` VARCHAR(255) NOT NULL,
 `password` VARCHAR(255) NOT NULL,
 `phone` VARCHAR(255) NOT NULL,
 PRIMARY KEY (`user_id`)
)
COMMENT = 'Administrator table'
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

Create a new sequence table:

CREATE TABLE `sequence` (
 `seq_name` VARCHAR(50) NOT NULL,
 `current_val` INT(11) NOT NULL,
 `increment_val` INT(11) NOT NULL DEFAULT '1',
 PRIMARY KEY (`seq_name`)
)
COMMENT = 'Sequence List'
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

Add a new sequence:

INSERT INTO sequence VALUES ('seq_test', '0', '1');

Create a currval function to get the current value of the sequence:

delimiter #
create function currval(v_seq_name VARCHAR(50)) 
returns integer(11) 
begin
 declare value integer;
 set value = 0;
 select current_val into value from sequence where seq_name = v_seq_name;
 return value;
end;

Query the current value:

select currval('seq_test');

Create a nextval function to get the next value of the sequence:

delimiter #
create function nextval (v_seq_name VARCHAR(50)) returns integer(11) 
begin
 update sequence set current_val = current_val + increment_val where seq_name = v_seq_name;
 return currval(v_seq_name);
end;

Query the next value

select nextval('seq_test');

Specific implementation:

<insert id="addUser" parameterType="User">
  <selectKey keyProperty="userId" resultType="int" order="BEFORE">
   select nextval('seq_test');
  </selectKey>
  insert into user(user_id,user_name,password,phone) values
  (#{userId},#{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR}, #{phone, jdbcType=VARCHAR})
 </insert>
<insert id="addAdmin" parameterType="Admin">
  <selectKey keyProperty="userId" resultType="int" order="BEFORE">
   select nextval('seq_test');
  </selectKey>
  insert into admin(user_id,user_name,password,phone) values
  (#{userId},#{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR}, #{phone, jdbcType=VARCHAR})
 </insert>

Final implementation:

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Tutorial on primary key in MySQL and setting its auto-increment
  • In-depth explanation of the settings for automatic growth of primary keys in Mysql, SqlServer, and Oracle
  • PHP code to create fields and set primary keys in MySQL

<<:  New ways to play with CSS fonts: implementation of colored fonts

>>:  Tomcat configuration and how to start it in Eclipse

Recommend

Example of Html shielding right-click menu and left-click typing function

Disable right-click menu <body oncontextmenu=s...

An example of using Dapr to simplify microservices from scratch

Table of contents Preface 1. Install Docker 2. In...

MySQL Series 8 MySQL Server Variables

Tutorial Series MySQL series: Basic concepts of M...

Tomcat's class loading mechanism process and source code analysis

Table of contents Preface 1. Tomcat class loader ...

Tutorial on building file sharing service Samba under CentOS6.5

Samba Services: This content is for reference of ...

CSS inheritance method

Given a div with the following background image: ...

Detailed installation process of MySQL 8.0 Windows zip package version

The installation process of MySQL 8.0 Windows zip...

Example code for implementing 3D Rubik's Cube with CSS

Let's make a simple 3D Rubik's Cube today...

Basic statements of MySQL data definition language DDL

MySQL DDL statements What is DDL, DML. DDL is dat...

Summary of methods for querying MySQL user permissions

Introduce two methods to view MySQL user permissi...

Steps to run ASP.NET Core in Docker container

There are too much knowledge to learn recently, a...