Mysql中Identity 详细介绍
假如表中包含一列为auto_increment,
如果是Myisam类型的引擎,那么在删除了最新一笔数据,无论是否重启Mysql,下一次插入之后仍然会使用上次删除的最大ID+1.
mysql>createtabletest_myisam(idintnotnullauto_incrementprimarykey,namechar(5))engine=myisam; QueryOK,0rowsaffected(0.04sec) mysql>insertintotest_myisam(name)select‘a‘; QueryOK,1rowaffected(0.00sec) Records:1Duplicates:0Warnings:0 mysql>insertintotest_myisam(name)select‘b‘; QueryOK,1rowaffected(0.00sec) Records:1Duplicates:0Warnings:0 mysql>insertintotest_myisam(name)select‘c‘; QueryOK,1rowaffected(0.00sec) Records:1Duplicates:0Warnings:0 mysql>insertintotest_myisam(name)selectnamefromtest_myisam; QueryOK,3rowsaffected(0.00sec) Records:3Duplicates:0Warnings:0 mysql>select*fromtest_myisam; +----+------+ |id|name| +----+------+ |1|a| |2|b| |3|c| |4|a| |5|b| |6|c| +----+------+ 6rowsinset(0.00sec) mysql>deletefromtest_myisamwhereid=6; QueryOK,1rowaffected(0.00sec)
mysql>insertintotest_myisam(name)select‘d‘; QueryOK,1rowaffected(0.00sec) Records:1Duplicates:0Warnings:0 mysql>select*fromtest_myisam; +----+------+ |id|name| +----+------+ |1|a| |2|b| |3|c| |4|a| |5|b| |7|d| +----+------+ 6rowsinset(0.00sec)
下面是对Innodb表的测试。
mysql>createtabletest_innodb(idintnotnullauto_incrementprimarykey,namechar(5))engine=innodb; QueryOK,0rowsaffected(0.26sec) mysql>insertintotest_innodb(name)select‘a‘; QueryOK,1rowaffected(0.06sec) Records:1Duplicates:0Warnings:0 mysql>insertintotest_innodb(name)select‘b‘; QueryOK,1rowaffected(0.06sec) Records:1Duplicates:0Warnings:0 mysql>insertintotest_innodb(name)select‘c‘; QueryOK,1rowaffected(0.07sec) Records:1Duplicates:0Warnings:0 mysql>select*fromtest_innodb; +----+------+ |id|name| +----+------+ |1|a| |2|b| |3|c| +----+------+ 3rowsinset(0.00sec) mysql>deletefromtest_innodbwhereid=3; QueryOK,1rowaffected(0.05sec) mysql>insertintotest_innodb(name)select‘d‘; QueryOK,1rowaffected(0.20sec) Records:1Duplicates:0Warnings:0 mysql>select*fromtest_innodb; +----+------+ |id|name| +----+------+ |1|a| |2|b| |4|d| +----+------+ 3rowsinset(0.00sec) mysql>exit Bye [2@adata]$mysql-uroot-pwsdad WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis5 Serverversion:5.5.37-logSourcedistribution Copyright(c)2000,2014,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type‘help;‘or‘\h‘forhelp.Type‘\c‘toclearthecurrentinputstatement. mysql>usewison Databasechanged mysql>deletefromtest_innodbwhereid=4; QueryOK,1rowaffected(0.07sec) mysql>exit Bye [2@adata]$sudoservicemysqlrestart ShuttingdownMySQL...SUCCESS! StartingMySQL..SUCCESS! [2@adata]$mysql-uroot-pwison WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis1 Serverversion:5.5.37-logSourcedistribution Copyright(c)2000,2014,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type‘help;‘or‘\h‘forhelp.Type‘\c‘toclearthecurrentinputstatement. mysql>usewison Databasechanged mysql>insertintotest_innodb(name)select‘z‘; QueryOK,1rowaffected(0.07sec) Records:1Duplicates:0Warnings:0 mysql>select*fromtest_innodb; +----+------+ |id|name| +----+------+ |1|a| |2|b| |3|z| +----+------+ 3rowsinset(0.00sec)
可以看到在mysql数据库没有重启时,innodb的表新插入数据会是之前被删除的数据再加1.
但是当Mysql服务被重启后,再向InnodB的自增表表里插入数据,那么会使用当前Innodb表里的最大的自增列再加1.
原因:
Myisam类型存储引擎的表将最大的ID值是记录到数据文件中,不管是否重启最大的ID值都不会丢失。但是InnoDB表的最大的ID值是存在内存中的,若不重启Mysql服务,新加入数据会使用内存中最大的数据+1.但是重启之后,会使用当前表中最大的值再+1
感谢阅读此文,希望能帮助到大家,谢谢大家对本站的支持!