Auto Increment after delete in MySQL

后端 未结 17 2128
醉酒成梦
醉酒成梦 2020-11-22 09:50

I have a MySQL table with a primary key field that has AUTO_INCREMENT on. After reading other posts on here I\'ve noticed people with the same problem and with varied answer

17条回答
  •  被撕碎了的回忆
    2020-11-22 10:19

    MYSQL Query Auto Increment Solution. It works perfect when you have inserted many records during testing phase of software. Now you want to launch your application live to your client and You want to start auto increment from 1.

    To avoid any unwanted problems, for safer side First export .sql file.
    Then follow the below steps:

    • Step 1) First Create the copy of an existing table MySQL Command to create Copy:

      CREATE TABLE new_Table_Name  SELECT * FROM existing_Table_Name;
      

      The exact copy of a table is created with all rows except Constraints.
      It doesn’t copy constraints like Auto Increment and Primary Key into new_Table_name

    • Step 2) Delete All rows If Data is not inserted in testing phase and it is not useful. If Data is important then directly go to Step 3.

      DELETE from new_Table_Name;
      
    • Step 3) To Add Constraints, Goto Structure of a table

      • 3A) Add primary key constraint from More option (If You Require).
      • 3B) Add Auto Increment constraint from Change option. For this set Defined value as None.

提交回复
热议问题