dongnaopa6200 2015-11-27 11:27
浏览 255
已采纳

MySQL | 主键需要重置

I have a table which stores the ID of support cases using the primary key (column name = caseid).

I have now got to about 100,000 caseid and the number is just too big. I wish to somehow start from a lower number such as 1000.

How do I achieve something like this by not having to delete/archive existing records and not having to change the unique caseid's to another column (keep it as the primary key column)

  • 写回答

3条回答 默认 最新

  • dongse5528 2015-11-27 11:58
    关注

    To reset Primary Key, you can follow below steps:

    1. Create temporary table with structure same as main table. Let's say table name is tbl_cases

      CREATE TABLE tbl_cases_tmp LIKE tbl_cases;
      ALTER TABLE tbl_cases_tmp ADD old_caseid int NOT NULL DEFAULT '0';
      
    2. DUMP all data from tbl_cases to tbl_cases_tmp. caseid will be stored in old_caseid column.

      INSERT INTO tbl_cases_tmp (name, summary, old_caseid)
      SELECT name, summary, caseid FROM tbl_cases;
      
    3. For any other tables having references to tbl_cases. Let's say tbl_reference

      UPDATE tbl_reference tr 
      JOIN tbl_cases_tmp tc
      ON tr.caseid = tc.old_caseid
      SET tr.caseid = tc.caseid;
      

    Before using Steps 4 and 5, ensure your tables tbl_cases_tmp and all references are properly updated.

    1. Drop tbl_cases

      DROP table tbl_cases;
      
    2. Rename tbl_cases_tmp to tbl_cases

      RENAME TABLE tbl_cases_tmp TO tbl_cases;
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示