您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“MySQL數(shù)據(jù)庫auto_increment自增值回溯”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
# 創(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)
# 插入一些數(shù)據(jù)
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)
# 刪除最后一條數(shù)據(jù)
mysql> delete from t where a=4;
Query OK, 1 row affected (0.02 sec)
# 查看表創(chuàng)建語句,發(fā)現(xiàn)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)行主鍵回溯模擬
# 重啟數(shù)據(jù)庫
[root@mysql ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
# 重新查看表創(chuàng)建語句,發(fā)現(xiàn)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)
# 繼續(xù)插入語句
mysql> insert into t select null, 50;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
# 查看表的數(shù)據(jù),發(fā)現(xiàn)上述自增ID=4又重新出現(xiàn)
mysql> select * from t;
+---+------+
| a | b |
+---+------+
| 1 | 10 |
| 2 | 20 |
| 3 | 30 |
| 4 | 50 |
+---+------+
4 rows in set (0.00 sec)
這是因?yàn)樵贛ySQL5.7中的表的AUTO_INCREMENT是基于內(nèi)存,不會持久化在磁盤中,每次啟動數(shù)據(jù)庫時,會對每張表進(jìn)行max(auto_increment) + 1重新作為該表下一次的主鍵ID的自增值。在MySQL8.0中就不會出現(xiàn)該問題,因?yàn)閿?shù)據(jù)會在磁盤中持久化。
“MySQL數(shù)據(jù)庫auto_increment自增值回溯”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。