十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
本篇內(nèi)容介紹了“MySQL數(shù)據(jù)庫(kù)auto_increment自增值回溯”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、成都做網(wǎng)站與策劃設(shè)計(jì),布爾津網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:布爾津等地區(qū)。布爾津做網(wǎng)站價(jià)格咨詢:18980820575
# 創(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)建語(yǔ)句,發(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ù)庫(kù)
[root@mysql ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
# 重新查看表創(chuàng)建語(yǔ)句,發(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ù)插入語(yǔ)句
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)存,不會(huì)持久化在磁盤(pán)中,每次啟動(dòng)數(shù)據(jù)庫(kù)時(shí),會(huì)對(duì)每張表進(jìn)行max(auto_increment) + 1重新作為該表下一次的主鍵ID的自增值。在MySQL8.0中就不會(huì)出現(xiàn)該問(wèn)題,因?yàn)閿?shù)據(jù)會(huì)在磁盤(pán)中持久化。
“MySQL數(shù)據(jù)庫(kù)auto_increment自增值回溯”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!