• <ul id="mayc0"></ul>
    <ul id="mayc0"><center id="mayc0"></center></ul>
    <strike id="mayc0"><input id="mayc0"></input></strike>
    <ul id="mayc0"></ul>
  • 始創于2000年 股票代碼:831685
    咨詢熱線:0371-60135900 注冊有禮 登錄
    • 掛牌上市企業
    • 60秒人工響應
    • 99.99%連通率
    • 7*24h人工
    • 故障100倍補償
    您的位置: 網站首頁 > 幫助中心>文章內容

    MySQL中的auto_increment的問題

    發布時間:  2012/7/30 17:08:35

    發現一個看似很簡單的MySQL問題,卻引起了大家的廣泛關注:

    這是一道很早的面試題:

    一張表,里面有ID自增主鍵,當insert了17條記錄之后,刪除了第15,16,17條記錄,再把Mysql重啟,再insert一條記錄,這條記錄的ID是18還是15 。

    答案:

    如果表的類型是MyISAM,那么是18。
    因為MyISAM表會把自增主鍵的最大ID記錄到數據文件里,重啟MySQL自增主鍵的最大ID也不會丟失。
    -
     

    如果表的類型是InnoDB,那么是15。
    InnoDB表只是把自增主鍵的最大ID記錄到內存中,所以重啟數據庫或者是對表進行OPTIMIZE操作,都會導致最大ID丟失。

    自己也做了個實驗,結果證實了上面的說法。真是慚愧啊,看似簡單的問題自己也打錯了。

    1.mysql> select * from test1; 
    2.+----+-----------+ 
    3.| id | name      | 
    4.+----+-----------+ 
    5.|  1 | 陳兵輝    | 
    6.|  2 | chen      | 
    7.|  3 | chen      | 
    8.|  4 | chen      | 
    9.|  5 | chen      | 
    10.|  6 | chen      | 
    11.|  7 | chen      | 
    12.|  8 | chen      | 
    13.|  9 | chen      | 
    14.| 10 | chen      | 
    15.| 11 | chen      | 
    16.+----+-----------+ 
    17.11 rows in set (0.00 sec) 
    18. 
    19.mysql> delete from test1 where id in (10,11,9); 
    2.Query OK, 3 rows affected (0.03 sec) 
    3. 
    4.mysql> show create table test1;  5. CREATE TABLE `test1` ( 
    6.  `id` int(11) NOT NULL auto_increment, 
    7.  `name` varchar(10) default NULL, 
    8.  PRIMARY KEY  (`id`) 
    9.) ENGINE=InnoDB AUTO_INCREMENT=<span style="color:#ff0000;">12</span> DEFAULT CHARSET=utf8 |  10.mysql> exit;  11.Bye 
    12.[root@fsailing1 ~]# service mysqld restart 
    13.停止 MySQL:                                               [確定] 
    14.啟動 MySQL:                                               [確定] 
    15.[root@fsailing1 ~]# mysql -uroot -p 
    16.Enter password: 
    17.Welcome to the MySQL monitor.  Commands end with ; or \g. 
    18.Your MySQL connection id is 2 
    19.Server version: 5.0.95 Source distribution 
    20.Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. 
    21. 
    22.Oracle is a registered trademark of Oracle Corporation and/or its 
    23.affiliates. Other names may be trademarks of their respective 
    24.owners. 
    25. 
    26.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
    27. 
    28.mysql> use test;  29.Reading table information for completion of table and column names 
    30.You can turn off this feature to get a quicker startup with -A 
    31. 
    32.Database changed 
    33.mysql> show create table test1;  34.| CREATE TABLE `test1` ( 
    35.  `id` int(11) NOT NULL auto_increment, 
    36.  `name` varchar(10) default NULL, 
    37.  PRIMARY KEY  (`id`) 
    38.) ENGINE=InnoDB AUTO_INCREMENT=<span style="color:#ff0000;">9</span> DEFAULT CHARSET=utf8 |  2,另外還有一個就是獲取當前數據庫表的自增字段數。

    1.mysql> select last_insert_id(); 
    2.+------------------+ 
    3.| last_insert_id() | 
    4.+------------------+ 
    5.|                0 | 
    6.+------------------+ 
    7.1 row in set (0.00 sec) 
     


    本文出自:億恩科技【www.vbseamall.com】

    服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]

  • 您可能在找
  • 億恩北京公司:
  • 經營性ICP/ISP證:京B2-20150015
  • 億恩鄭州公司:
  • 經營性ICP/ISP/IDC證:豫B1.B2-20060070
  • 億恩南昌公司:
  • 經營性ICP/ISP證:贛B2-20080012
  • 服務器/云主機 24小時售后服務電話:0371-60135900
  • 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
  • 專注服務器托管17年
    掃掃關注-微信公眾號
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區翠竹街1號總部企業基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網安備41019702002023號
      1
     
     
     
     

    0371-60135900
    7*24小時客服服務熱線