This article uses an example to illustrate the usage of MySQL non-primary key self-increment. Share with you for your reference, the details are as follows: In MySQL, not only the primary key can be set to auto-increment, but the column set as the key can be set to auto-increment. as follows: CREATE TABLE t1 ( id INT, col1 INT auto_increment NOT NULL ); The results are as follows: If you set the col1 column as the key, you can create an auto-increment column. CREATE TABLE t1 ( id INT, col1 INT auto_increment NOT NULL, key(col1) ); The results are as follows: If we set id as the primary key, we can still create it successfully. CREATE TABLE t2 ( id INT PRIMARY KEY, col1 INT auto_increment NOT NULL, key(col1) ); The results are as follows: Therefore, the auto-increment column must be a key, but it does not necessarily have to be a primary key. But can a table have multiple auto-increment columns? Answer: A table can only have one auto-increment column. CREATE TABLE t3 ( id INT PRIMARY KEY auto_increment, col1 INT auto_increment NOT NULL, key(col1) ); The results are as follows: Readers who are interested in more MySQL-related content can check out the following topics on this site: "Summary of MySQL Index Operation Skills", "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills". I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
<<: echars 3D map solution for custom colors of regions
>>: VMware pro15 installation macOS10.13 detailed installation diagram (picture and text)
Introduction to sudo authority delegation su swit...
There are the following log files in MySQL: 1: re...
In the previous article, we played with timeouts ...
Preface The SQL mode affects the SQL syntax that ...
HTML Part Copy code The code is as follows: <!D...
This article uses examples to illustrate the usag...
centos7 switch boot kernel Note: If necessary, it...
1. Download cuda10.1: NVIDIA official website lin...
In the process of designing a web page, designers...
Apache Tika is a library for file type detection ...
Sending emails using PHP's mail function The ...
Table of contents Prototype chain diagram Essenti...
It is very convenient to configure virtual host v...
Table of contents 1. Get a random Boolean value (...
The <link> tag defines the relationship bet...