將一張表的主鍵 ID 重置為從1開始自增排列

2022-09-16 16:39:11 字數 909 閱讀 7005

終於有一天,使用這張表的某個系統要匯入正式資料了,強迫症這時候就表現的明顯了,渾身不自在,

這時候你就需要將這個主鍵id重置一波了,方法是在這張表中新增乙個字段,將id裡面的資料複製過去,

然後刪除id欄位,接著新建乙個id欄位,再接著將id欄位自增且設為主鍵,最後將這個新增的id列挪到第一列,

將那個用於複製最初id內容的新增字段刪除,一切ok.

1、在要操作的表 tablename 中新增乙個字段 old_id;

alter table  tablename  add old_id int(10) not null;

2、將 id 欄位的資料複製給 old_id;

update tablename set old_id=id;

3、刪除 id 字段;

alter table tablename drop id;

4、新增乙個 id 字段;

alter table tablename add id int(10) not null;

5、將這個新增的 id 字段設定為自增主鍵;

alter table tablename modify column id int(10) not null auto_increment, add primary key (id);

6、刪除 old_id 列;

alter table tablename drop column old_id;

7、將最新的 id 列挪到最前面;

alter table tablename modify id int(10) first;  

將一張表的資料插入另外一張表

表cmb send sms create table cmb send sms send id bigint 20 not null auto increment comment 主鍵id phone no varchar 32 not null comment 手機號碼 status varcha...

將一張表的查詢結果插入到另一張表

select into 和 insert into select 兩種表複製語句 2select into desttbl from srctbl34 insert into desttbl fld1,fld2 select fld1,5 from srctbl56 以上兩句都是將 srctbl 的...

迴圈將一張表的資料新增到另一張表,重複資料不新增

1 迴圈將一張表的資料新增到另一張表,重複資料不新增 a表必須存在 create or replace procedure sp import as i count int begin for r in select from a loop 判斷會員卡 select count into i cou...