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

Three principles of efficient navigation design that web designers must know

Designing navigation for a website is like laying...

CSS implements 0.5px lines to solve mobile compatibility issues (recommended)

【content】: 1. Use background-image gradient style...

Thoughts on truncation of multi-line text with a "show more" button

I just happened to encounter this small requireme...

Some experience in building the React Native project framework

React Native is a cross-platform mobile applicati...

MySQL batch removes spaces in a certain field

Is there any way to remove spaces from a certain ...

Introduction to the use of base link tag base

<br />When you click the link, the web page ...

JS implements sliding up and down on the mobile terminal one screen at a time

This article shares with you the specific code of...

Detailed example of SpringBoot+nginx to achieve resource upload function

Recently, I have been learning to use nginx to pl...

CSS implements the web component function of sliding the message panel

Hello everyone, I wonder if you have the same con...

Whitespace processing in HTML/CSS and how to preserve whitespace in the page

Whitespace rules in HTML In HTML, multiple spaces...

MySQL efficient query left join and group by (plus index)

mysql efficient query MySQL sacrifices group by t...

Implementation of effective user groups and initial user groups in Linux

First check the /etc/group file: [root@localhost ...

Application of CSS3 animation effects in activity pages

background Before we know it, a busy year is comi...

Implementation of Vue top tags browsing history

Table of contents nonsense Functions implemented ...