MySQL identity column

Source: Internet
Author: User

#标识列/* 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.