久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

如何解決mysql中auto

157次閱讀
沒有評論

共計 2068 個字符,預計需要花費 6 分鐘才能閱讀完成。

這篇文章將為大家詳細講解有關如何解決 mysql 中 auto_increment 的問題,丸趣 TV 小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

mysql 中的 auto_increment 的問題
 
今兒在逛時,發現一個看似很簡單的問題,卻引起了大家的廣泛關注:
這是一道很早的面試題:
 
一張表, 里面有 ID 自增主鍵, 當 insert 了 17 條記錄之后, 刪除了第 15,16,17 條記錄, 再把 Mysql 重啟,
再 insert 一條記錄, 這條記錄的 ID 是 18 還是 15。 www.2cto.com  
答案:
如果表的類型是 MyISAM,那么是 18。 
因為 MyISAM 表會把自增主鍵的最大 ID 記錄到數據文件里,重啟 MySQL 自增主鍵的最大 ID 也不會丟失。 
如果表的類型是 InnoDB,那么是 15。 
InnoDB 表只是把自增主鍵的最大 ID 記錄到內存中,所以重啟或者是對表進行 OPTIMIZE 操作,
都會導致最大 ID 丟失。 
自己也做了個實驗,結果證實了上面的說法。真是慚愧啊,看似簡單的問題自己也打錯了。
[html]
select * from test1;  
+—-+———–+  
| id | name      |  
+—-+———–+  
|  1 | 陳兵輝    |  
|  2 | chen      |  
|  3 | chen      |  
|  4 | chen      |  
|  5 | chen      |  
|  6 | chen      |  
|  7 | chen      |  
|  8 | chen      |  
|  9 | chen      |  
| 10 | chen      |  
| 11 | chen      |  
+—-+———–+  
11 rows in set (0.00 sec)  
    www.2cto.com  
mysql delete from test1 where id in (10,11,9);  
Query OK, 3 rows affected (0.03 sec)  
 
mysql show create table test1;  
 CREATE TABLE `test1` ( 
  `id` int(11) NOT NULL auto_increment,  
  `name` varchar(10) default NULL,  
  PRIMARY KEY  (`id`)  
) ENGINE=InnoDB AUTO_INCREMENT= span >mysql exit;  
Bye  
[root@fsailing1 ~]# service mysqld restart  
停止 MySQL:                                              [確定]  
啟動 MySQL:                                              [確定]  
[root@fsailing1 ~]# mysql -uroot -p  
Enter password:  
Welcome to the MySQL monitor.  Commands end with ; or \g.  
Your MySQL connection id is 2  
Server version: 5.0.95 Source distribution  
Copyright (c) 2000, 2011,  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 test;  
Reading table information for completion of table and column names  
You can turn off this feature to get a quicker startup with -A  
 
Database changed  
mysql show create table test1;  
| CREATE TABLE `test1` ( 
  `id` int(11) NOT NULL auto_increment,  
  `name` varchar(10) default NULL,  
  PRIMARY KEY  (`id`)  
) ENGINE=InnoDB AUTO_INCREMENT= span > 
2,另外還有一個就是獲取當前數據庫表的自增字段數。
[html]
mysql select last_insert_id();  
+——————+  
| last_insert_id() |  
+——————+  
|                0 |  
+——————+  
1 row in set (0.00 sec)  
 
 
 

關于“如何解決 mysql 中 auto_increment 的問題”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-15發表,共計2068字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 苏州市| 静宁县| 隆安县| 洞头县| 壤塘县| 景谷| 津南区| 土默特左旗| 平和县| 和静县| 邓州市| 应用必备| 伊春市| 成武县| 宿迁市| 江永县| 宝丰县| 漳平市| 石景山区| 南雄市| 咸丰县| 林口县| 永平县| 抚远县| 镇赉县| 九龙城区| 建德市| 获嘉县| 胶州市| 丹巴县| 威信县| 鄂尔多斯市| 垣曲县| 阳谷县| 新余市| 西平县| 嘉峪关市| 安龙县| 来安县| 留坝县| 潞城市|