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 mytestCreate new user table and admin tableCREATE 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:
|
<<: New ways to play with CSS fonts: implementation of colored fonts
>>: Tomcat configuration and how to start it in Eclipse
Disable right-click menu <body oncontextmenu=s...
Table of contents Preface 1. Install Docker 2. In...
Tutorial Series MySQL series: Basic concepts of M...
Table of contents Preface 1. Tomcat class loader ...
Samba Services: This content is for reference of ...
in conclusion % of width: defines the percentage ...
Table of contents Install mysql Configuring envir...
Given a div with the following background image: ...
The installation process of MySQL 8.0 Windows zip...
Let's make a simple 3D Rubik's Cube today...
MySQL DDL statements What is DDL, DML. DDL is dat...
Introduce two methods to view MySQL user permissi...
This article summarizes some common MySQL optimiz...
There are too much knowledge to learn recently, a...
Go to https://dev.mysql.com/downloads/mysql/ to d...