MySQL數(shù)據(jù)庫auto_increment自增值回溯

本篇內(nèi)容介紹了“MySQL數(shù)據(jù)庫auto_increment自增值回溯”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、成都做網(wǎng)站與策劃設(shè)計,布爾津網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:布爾津等地區(qū)。布爾津做網(wǎng)站價格咨詢: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)建語句,發(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)

這是因為在MySQL5.7中的表的AUTO_INCREMENT是基于內(nèi)存,不會持久化在磁盤中,每次啟動數(shù)據(jù)庫時,會對每張表進(jìn)行max(auto_increment) + 1重新作為該表下一次的主鍵ID的自增值。在MySQL8.0中就不會出現(xiàn)該問題,因為數(shù)據(jù)會在磁盤中持久化。

“MySQL數(shù)據(jù)庫auto_increment自增值回溯”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

名稱欄目:MySQL數(shù)據(jù)庫auto_increment自增值回溯
本文網(wǎng)址:http://muchs.cn/article6/jcjiig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、網(wǎng)站維護(hù)網(wǎng)站改版、Google、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司