Updating AUTO_INCREMENT value of all tables in a MySQL database

前端 未结 8 1632
旧巷少年郎
旧巷少年郎 2020-12-02 22:52

It is possbile set/reset the AUTO_INCREMENT value of a MySQL table via

ALTER TABLE some_table AUTO_INCREMENT = 1000

However I need

8条回答
  •  独厮守ぢ
    2020-12-02 23:23

    The Quickest solution to Update/Reset AUTO_INCREMENT in MySQL Database

    Ensure that the AUTO_INCREMENT column has not been used as a FOREIGN_KEY on another table.

    Firstly, Drop the AUTO_INCREMENT COLUMN as:

    ALTER TABLE table_name DROP column_name
    

    Example: ALTER TABLE payments DROP payment_id

    Then afterward re-add the column, and move it as the first column in the table

    ALTER TABLE table_name ADD column_name DATATYPE AUTO_INCREMENT PRIMARY KEY FIRST
    

    Example: ALTER TABLE payments ADD payment_id INT AUTO_INCREMENT PRIMARY KEY FIRST

提交回复
热议问题