How to set an AUTO_INCREMENT field with to start with the value 6000 in mysql?

后端 未结 3 1623
清酒与你
清酒与你 2021-02-19 11:11

How to set a field auto increment without auto increment key in mysql or how set a field auto increment with start value 6000 in mysql?

相关标签:
3条回答
  • 2021-02-19 11:37

    ... how set a field auto increment with start value 6000 in mysql?

    If your table already exists:

    ALTER TABLE your_table AUTO_INCREMENT = 6000;
    

    If you are creating your table from scratch:

    CREATE TABLE your_table () AUTO_INCREMENT = 6000;
    

    Source and further reading:

    • MySQL 5.1 Reference Manual :: Using AUTO_INCREMENT

    Test case:

    CREATE TABLE users (
       user_id  int NOT NULL, 
       name     varchar(50),
       PRIMARY KEY (user_id)
    );
    
    INSERT INTO users VALUES (1, 'Bob');
    INSERT INTO users VALUES (2, 'Joe');
    INSERT INTO users VALUES (3, 'Paul');
    
    ALTER TABLE users MODIFY user_id int NOT NULL AUTO_INCREMENT;
    ALTER TABLE users AUTO_INCREMENT = 6000;
    
    INSERT INTO users (name) VALUES ('Keith');
    INSERT INTO users (name) VALUES ('Steve');
    INSERT INTO users (name) VALUES ('Jack');
    
    SELECT * FROM users;
    +---------+-------+
    | user_id | name  |
    +---------+-------+
    |       1 | Bob   |
    |       2 | Joe   |
    |       3 | Paul  |
    |    6000 | Keith |
    |    6001 | Steve |
    |    6002 | Jack  |
    +---------+-------+
    6 rows in set (0.01 sec)
    
    0 讨论(0)
  • 2021-02-19 11:37
    ALTER TABLE tbl_name AUTO_INCREMENT = 6000
    

    but be aware you should have no PK lager 6000 in this table !

    0 讨论(0)
  • 2021-02-19 11:47

    mysql will show you the correct syntax for this, and more, if you execute the following for a table that contains an auto increment PK & some data already:

    SHOW CREATE TABLE your_tablename;
    
    0 讨论(0)
提交回复
热议问题