set auto increment initial value for mysql table

后端 未结 2 1548
天涯浪人
天涯浪人 2020-12-29 14:59

I\'m trying to create a table in my sql using PHP but I\'m not sure how to set an initial value for the auto increment field.

This is what i have so far:

相关标签:
2条回答
  • 2020-12-29 15:36

    Assuming your auto increment column is the first one:

     $sql = "CREATE TABLE MY_TABLE
     (
     table_1 INT AUTO_INCREMENT,
     table_2 varchar(45),
     table_3 varchar(999),
     table_4 varchar(45)
     ) AUTO_INCREMENT = 231";
    

    The starting value will be, here, 231.

    I changed the column type to INT, because you can't use a VARCHAR for auto-increment.

    (and remove the or die(mysql_error()) on this line btw, its pointless because it's just a variable creation, not a SQL query being executed)

    0 讨论(0)
  • 2020-12-29 15:43

    If you don't have the auto-increment column in the table yet:

    $sql = "ALTER TABLE MY_TABLE ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT,
        ADD INDEX (id);";
    

    Then to set the auto-increment starting value:

    $sql = "ALTER TABLE MY_TABLE AUTO_INCREMENT = 111111;";
    

    Potential duplicate of this post.

    0 讨论(0)
提交回复
热议问题