Mysql中Identity 詳細(xì)介紹
假如表中包含一列為auto_increment,
如果是Myisam類(lèi)型的引擎,那么在刪除了最新一筆數(shù)據(jù),無(wú)論是否重啟Mysql,下一次插入之后仍然會(huì)使用上次刪除的最大ID+1.
mysql> create table test_myisam (id int not null auto_increment primary key, name char(5)) engine=myisam; Query OK, 0 rows affected (0.04 sec) mysql> insert into test_myisam (name) select ‘a(chǎn)‘; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into test_myisam (name) select ‘b‘; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into test_myisam (name) select ‘c‘; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into test_myisam (name) select name from test_myisam; Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from test_myisam; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | a | | 5 | b | | 6 | c | +----+------+ 6 rows in set (0.00 sec) mysql> delete from test_myisam where id=6; Query OK, 1 row affected (0.00 sec)
mysql> insert into test_myisam(name) select ‘d‘; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from test_myisam; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | a | | 5 | b | | 7 | d | +----+------+ 6 rows in set (0.00 sec)
下面是對(duì)Innodb表的測(cè)試。
mysql> create table test_innodb(id int not null auto_increment primary key, name char(5)) engine=innodb; Query OK, 0 rows affected (0.26 sec) mysql> insert into test_innodb (name)select ‘a(chǎn)‘; Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into test_innodb (name)select ‘b‘; Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into test_innodb (name)select ‘c‘; Query OK, 1 row affected (0.07 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from test_innodb; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | +----+------+ 3 rows in set (0.00 sec) mysql> delete from test_innodb where id=3; Query OK, 1 row affected (0.05 sec) mysql> insert into test_innodb (name)select ‘d‘; Query OK, 1 row affected (0.20 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from test_innodb; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 4 | d | +----+------+ 3 rows in set (0.00 sec) mysql> exit Bye [2@a data]$ mysql -uroot -pwsdad Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.5.37-log Source distribution Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use wison Database changed mysql> delete from test_innodb where id=4; Query OK, 1 row affected (0.07 sec) mysql> exit Bye [2@a data]$ sudo service mysql restart Shutting down MySQL... SUCCESS! Starting MySQL.. SUCCESS! [2@a data]$ mysql -uroot -pwison Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.37-log Source distribution Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> use wison Database changed mysql> insert into test_innodb (name) select ‘z‘; Query OK, 1 row affected (0.07 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from test_innodb; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | z | +----+------+ 3 rows in set (0.00 sec)
可以看到在mysql數(shù)據(jù)庫(kù)沒(méi)有重啟時(shí),innodb的表新插入數(shù)據(jù)會(huì)是之前被刪除的數(shù)據(jù)再加1.
但是當(dāng)Mysql服務(wù)被重啟后,再向InnodB的自增表表里插入數(shù)據(jù),那么會(huì)使用當(dāng)前Innodb表里的最大的自增列再加1.
原因:
Myisam類(lèi)型存儲(chǔ)引擎的表將最大的ID值是記錄到數(shù)據(jù)文件中,不管是否重啟最大的ID值都不會(huì)丟失。但是InnoDB表的最大的ID值是存在內(nèi)存中的,若不重啟Mysql服務(wù),新加入數(shù)據(jù)會(huì)使用內(nèi)存中最大的數(shù)據(jù)+1.但是重啟之后,會(huì)使用當(dāng)前表中最大的值再+1
感謝閱讀此文,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- mysql alter table命令修改表結(jié)構(gòu)實(shí)例詳解
- MySQL中的alter table命令的基本使用方法及提速優(yōu)化
- MySQL學(xué)習(xí)筆記5:修改表(alter table)
- mysql中key 、primary key 、unique key 與index區(qū)別
- mysql 復(fù)制表結(jié)構(gòu)和數(shù)據(jù)實(shí)例代碼
- PHP讀MYSQL中文亂碼的快速解決方法
- sysbench對(duì)mysql壓力測(cè)試的詳細(xì)教程
- MySQL隱式類(lèi)型的轉(zhuǎn)換陷阱和規(guī)則
- JDBC 連接MySQL實(shí)例詳解
- Mysql5.6啟動(dòng)內(nèi)存占用過(guò)高解決方案
- mysql alter table命令修改表結(jié)構(gòu)實(shí)例
相關(guān)文章
mysql和oracle默認(rèn)排序的方法 - 不指定order by
這篇文章主要介紹了mysql和oracle默認(rèn)排序的方法 - 不指定order by。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07關(guān)于mysql數(shù)據(jù)庫(kù)連接編碼問(wèn)題
這篇文章主要介紹了關(guān)于mysql數(shù)據(jù)庫(kù)連接編碼問(wèn)題,默認(rèn)的編碼和數(shù)據(jù)庫(kù)表中的數(shù)據(jù)使用的編碼是不一致的,如果是中文,那么在數(shù)據(jù)庫(kù)中執(zhí)行時(shí)已經(jīng)是亂碼了,需要的朋友可以參考下2023-04-04python中的mysql數(shù)據(jù)庫(kù)LIKE操作符詳解
LIKE操作符用于在WHERE子句中搜索列中的指定模式,like操作符的語(yǔ)法在文章開(kāi)頭也給大家提到,通過(guò)兩種示例代碼給大家介紹python中的mysql數(shù)據(jù)庫(kù)LIKE操作符知識(shí),感興趣的朋友跟隨小編一起看看吧2021-07-07mysql如何創(chuàng)建和刪除唯一索引(unique key)
這篇文章主要介紹了mysql如何創(chuàng)建和刪除唯一索引(unique key)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12mysql.help_topic生成序列的方法實(shí)現(xiàn)
本文探討了如何使用MySQL的help_topic表生成序列,并介紹了相應(yīng)的SQL查詢(xún)語(yǔ)句和實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10Linux環(huán)境下安裝MySQL數(shù)據(jù)庫(kù)
這篇文章介紹了Linux環(huán)境下安裝MySQL數(shù)據(jù)庫(kù)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04