本篇內容介紹了“數據庫auto_increment自增值回溯”的有關(guān)知識,在實(shí)際案例的操作過(guò)程中,不少人都會(huì )遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學(xué)有所成!
# 創(chuàng )建關(guān)于表t,其中a字段為主鍵自增
mysql> create table t(a bigint primary key auto_increment, b tinyint);
Query OK, 0 rows affected (0.03 sec)
# 插入一些數據
mysql> insert into t select null, 10;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t select null, 20;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t select null, 30;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t select null, 40;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
# 查看表記錄
mysql> select * from t;
+---+------+
| a | b |
+---+------+
| 1 | 10 |
| 2 | 20 |
| 3 | 30 |
| 4 | 40 |
+---+------+
4 rows in set (0.00 sec)
# 刪除最后一條數據
mysql> delete from t where a=4;
Query OK, 1 row affected (0.02 sec)
# 查看表創(chuàng )建語(yǔ)句,發(fā)現AUTO_INCREMENT=5
mysql> show create table t\G
*************************** 1. row ***************************
Table: t
Create Table: CREATE TABLE `t` (
`a` bigint(20) NOT NULL AUTO_INCREMENT,
`b` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
# 進(jìn)行主鍵回溯模擬
# 重啟數據庫
[root@mysql ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
# 重新查看表創(chuàng )建語(yǔ)句,發(fā)現AUTO_INCREMENT=4
mysql> show create table t\G
*************************** 1. row ***************************
Table: t
Create Table: CREATE TABLE `t` (
`a` bigint(20) NOT NULL AUTO_INCREMENT,
`b` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
# 繼續插入語(yǔ)句
mysql> insert into t select null, 50;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
# 查看表的數據,發(fā)現上述自增ID=4又重新出現
mysql> select * from t;
+---+------+
| a | b |
+---+------+
| 1 | 10 |
| 2 | 20 |
| 3 | 30 |
| 4 | 50 |
+---+------+
4 rows in set (0.00 sec)
這是因為在MySQL5.7中的表的AUTO_INCREMENT是基于內存,不會(huì )持久化在磁盤(pán)中,每次啟動(dòng)數據庫時(shí),會(huì )對每張表進(jìn)行max(auto_increment) + 1重新作為該表下一次的主鍵ID的自增值。在MySQL8.0中就不會(huì )出現該問(wèn)題,因為數據會(huì )在磁盤(pán)中持久化。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站