#标识列/* is also known as the self-growth column meaning: You can not manually insert the value, the system provides the default sequence value characteristics: 1, the identity column must be combined with the primary key? Not necessarily, but the requirement is a key (or unique) 2, a table can have several identity columns? At most one! 3, the identity column type can only be numeric type 4, the identity column can be set auto_increment_increment=3; Set the step can be manually inserted values, set the starting value */#一, create a table when the Identity column DROP table if EXISTS tab_identity; CREATE TABLE tab_identity (id int PRIMARY KEY auto_increment, NAME FLOAT UNIQUE, seat INT); DROP TABLE IF EXISTS tab_identity; CREATE TABLE tab_identity (id int, NAME FLOAT UNIQUE auto_increment, seat int); TRUNCATE TABLE tab_identity; # either with all the fields inserted, the self-growing column corresponds to inserting a NULL insert into tab_identity (id,name) VALUES (null, ' John '); #要么 without self-growth columns, insert only the other fields inserted into tab_identity (NAME) VALUES (' Lucy '); SELECT * from Tab_identity; # The self-growth column is incremented by default from 1, with a step of 1 SHOW VARIABLES like '%auto_increment% '; # auto_increment_increment: Step # Auto_increment_offset: Start position (cannot be set manually in MySQL) set auto_increment_increment=3; # Set Step #如果想自己设置起始位置: #先执行下一句通过 Manually insert values, set startValue INSERT into tab_identity (id,name) VALUES (' John '); #然后 as before, insert multiple lines next: INSERT INTO Tab_identity (NAME) VALUES (' Lucy '); Setting the identity column #二, modifying the table DROP table if EXISTS test; CREATE TABLE Test (id INT PRIMARY KEY, NAME VARCHAR (20)); ALTER TABLE test MODIFY COLUMN ID INT auto_increment; INSERT into Test VALUES (NULL, ' AAA '); SELECT * from Test; TRUNCATE TABLE test; #清空表, note the drop TABLE IF EXISTS test; The difference, one is emptied, the other removes SHOW VARIABLES like '%auto_increment% '; SET auto_increment_increment = 2; INSERT into Test VALUES (5, ' haha '); INSERT into Test VALUES (NULL, ' haha ') and #运行多次该句 SELECT * from test; # The result is id = 5,7,9 .... DESC test; Delete an identity column when the table is modified #二 alter TABLES test MODIFY column ID INT; DESC test;
MySQL identity column