mysql重設自增ID(織夢文章ID的重新排序)

來源:互聯網
上載者:User


做網站時經常會遇到由於刪除過文章而導致文章ID不連續,由於文章ID欄位在資料庫中的屬性一般是自動成長的,這是mysql中的一個記數器記錄的,在刪除記錄再新增時,自增的數值並不會佔用你刪除的值,而是繼續在原基礎上++。

資料表中的不連續的記錄對於處女座來說是不能忍受的,所以下面來看看如何將已有資料的自增ID重新排列。
這裡以織夢系統(dedecms)的文章表為例:假設要操作的表是dede_archives, 也就是織夢的內容主表。
1.在要操作的表上建立一個欄位,用於儲存當前的ID。

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

2. 將當前的ID儲存到這個新欄位中。

update dede_archives set old_id=id;

3. 刪除當前表的ID欄位(自增主鍵)。

alter table dede_archives drop id;

4. 重建立立ID欄位,資料類型可按它原先的來。

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

5. 設定一下這個新ID欄位的屬性,設定為自增和主鍵。

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

經過上面這步,新的ID欄位已經重新排列了~
下面可以更新一下其它相關聯的表,這也就是建立那個old_id的作用了。

update dede_archives as  a, dede_副表 as b set b.aid=a.id where b.aid=a.old_id;

7. 完成!現在的文章ID已經從1開始重新計數了~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.