欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mysql中Identity 詳細(xì)介紹

 更新時(shí)間:2016年09月30日 16:27:07   投稿:lqh  
這篇文章主要介紹了Mysql中Identity 的相關(guān)資料,并附示例代碼,需要的朋友可以參考下

假如表中包含一列為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ì)本站的支持!

相關(guān)文章

  • MYSQL字符串強(qiáng)轉(zhuǎn)的方法示例

    MYSQL字符串強(qiáng)轉(zhuǎn)的方法示例

    這篇文章主要給大家介紹了關(guān)于MYSQL字符串強(qiáng)轉(zhuǎn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • MySQL筆記之系統(tǒng)信息函數(shù)詳解

    MySQL筆記之系統(tǒng)信息函數(shù)詳解

    本篇文章對(duì)MySQL系統(tǒng)信息函數(shù)進(jìn)行了詳解的介紹。需要的朋友參考下
    2013-05-05
  • mysql和oracle默認(rèn)排序的方法 - 不指定order by

    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)題

    這篇文章主要介紹了關(guān)于mysql數(shù)據(jù)庫(kù)連接編碼問(wèn)題,默認(rèn)的編碼和數(shù)據(jù)庫(kù)表中的數(shù)據(jù)使用的編碼是不一致的,如果是中文,那么在數(shù)據(jù)庫(kù)中執(zhí)行時(shí)已經(jīng)是亂碼了,需要的朋友可以參考下
    2023-04-04
  • python中的mysql數(shù)據(jù)庫(kù)LIKE操作符詳解

    python中的mysql數(shù)據(jù)庫(kù)LIKE操作符詳解

    LIKE操作符用于在WHERE子句中搜索列中的指定模式,like操作符的語(yǔ)法在文章開(kāi)頭也給大家提到,通過(guò)兩種示例代碼給大家介紹python中的mysql數(shù)據(jù)庫(kù)LIKE操作符知識(shí),感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • MYSQL修改所有表的存儲(chǔ)引擎格式語(yǔ)句

    MYSQL修改所有表的存儲(chǔ)引擎格式語(yǔ)句

    MYSQL如何修改所有表的存儲(chǔ)引擎格式,或許下面的sql語(yǔ)句對(duì)大家有所幫助
    2013-08-08
  • mysql如何創(chuàng)建和刪除唯一索引(unique key)

    mysql如何創(chuàng)建和刪除唯一索引(unique key)

    這篇文章主要介紹了mysql如何創(chuàng)建和刪除唯一索引(unique key)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • mysql.help_topic生成序列的方法實(shí)現(xiàn)

    mysql.help_topic生成序列的方法實(shí)現(xiàn)

    本文探討了如何使用MySQL的help_topic表生成序列,并介紹了相應(yīng)的SQL查詢(xún)語(yǔ)句和實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • SQL 聚合、分組和排序

    SQL 聚合、分組和排序

    這篇文章主要介紹了SQL 聚合、分組和排序,文章圍繞SQL 聚合、分組、排序的相關(guān)資料展開(kāi)具體內(nèi)容,需要的朋友可以參考一下
    2021-11-11
  • Linux環(huán)境下安裝MySQL數(shù)據(jù)庫(kù)

    Linux環(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

最新評(píng)論