How to set initial value and auto increment in MySQL?

前端 未结 10 1873
Happy的楠姐
Happy的楠姐 2020-11-22 13:40

How do I set the initial value for an \"id\" column in a MySQL table that start from 1001?

I want to do an insert \"INSERT INTO users (name, email) VALUES (\'{

10条回答
  •  忘了有多久
    2020-11-22 14:22

    MySQL - Setup an auto-incrementing primary key that starts at 1001:

    Step 1, create your table:

    create table penguins(
      my_id       int(16) auto_increment, 
      skipper     varchar(4000),
      PRIMARY KEY (my_id)
    )
    

    Step 2, set the start number for auto increment primary key:

    ALTER TABLE penguins AUTO_INCREMENT=1001;
    

    Step 3, insert some rows:

    insert into penguins (skipper) values("We need more power!");
    insert into penguins (skipper) values("Time to fire up");
    insert into penguins (skipper) values("kowalski's nuclear reactor.");
    

    Step 4, interpret the output:

    select * from penguins
    

    prints:

    '1001', 'We need more power!'
    '1002', 'Time to fire up'
    '1003', 'kowalski\'s nuclear reactor'
    

提交回复
热议问题