MySQL is not only the primary key to self-growth, but the column set as the key can be set to self-growth.
As follows:
1234 |
CREATE TABLE t1 ( id INT, col1 INT auto_increment NOT NULL ); |
The result is as follows: If you set the Col1 column as the key, you can create a self-increment.
12345 |
CREATE TABLE t1 ( id INT, col1 INT auto_increment NOT NULL, key(col1) ); |
The results are as follows: If we set the ID as the primary key, we can still create the success.
12345 |
CREATE TABLE t2 ( id INT PRIMARY KEY, col1 INT auto_increment NOT NULL, key(col1) ); |
The results are as follows:
so the self-increment column must be a key , but not necessarily a primary key. But can a table have multiple self-increment columns?
A: A table can only have one self-increment column .
12345 |
CREATE TABLE t3 ( id INT PRIMARY KEY auto_increment, col1 INT auto_increment NOT NULL, key(col1) ); |
The results are as follows:
The conclusion is that each table can only be set to a field that is self-growing, which can be either a primary key or a primary key, or it must be set to a key if it is not a primary key.
In fact, the primary key (primary key) is also one of the keys, key also includes the foreign key (foreign key), unique key, etc., about key discussion can see the post http://www.cnblogs.com/tanzq/p /9064511.html
MySQL non-primary key self-growth