共計 3420 個字符,預計需要花費 9 分鐘才能閱讀完成。
本篇文章給大家分享的是有關如何理解 mysql 自增長列,丸趣 TV 小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著丸趣 TV 小編一起來看看吧。
自增長列必須是索引列,否則無法創建成功表,對 myisma 和 innodb 都一樣
(localhost@testdb)[root] create table test5 (id int auto_increment,name varchar(10)) engine=innodb;
ERROR 1075 (42000):
(localhost@testdb)[root]
(localhost@testdb)[root]
(localhost@testdb)[root] create table test5 (id int auto_increment,name varchar(10),index(id)) engine=innodb;
Query OK, 0 rows affected (0.01 sec)
(localhost@testdb)[root] create table test5 (id int auto_increment,name varchar(10)) engine=myisam;
ERROR 1075 (42000):
(localhost@testdb)[root] create table test5 (id int auto_increment,name varchar(10),index(id)) engine=myisam;
Query OK, 0 rows affected (0.00 sec)
(localhost@testdb)[root]
(localhost@testdb)[root]
(localhost@testdb)[root]
創建成功后 id 列沒有插入數據,但是可以自動增長
(localhost@testdb)[root] insert into test5(name) values(aa),(bb),(cc
Query OK, 3 rows affected (0.00 sec)
(localhost@testdb)[root] select * from test5;
+—-+——+
| id | name |
+—-+——+
| 1 | aa |
| 2 | bb |
| 3 | cc |
+—-+——+
3 rows in set (0.00 sec)
索引
(localhost@testdb)[root] (localhost@testdb)[root] show index from test5;
+——-+————+———-+————–+————-+———–+————-+———-+——–+——+————+———+—————+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+——-+————+———-+————–+————-+———–+————-+———-+——–+——+————+———+—————+
| test5 | 1 | id | 1 | id | A | NULL | NULL | NULL | | BTREE | | |
+——-+————+———-+————–+————-+———–+————-+———-+——–+——+————+———+—————+
1 row in set (0.00 sec)
刪除表里的數據,在插入數據 id 列會依據原來的值繼續增長
(localhost@testdb)[root] delete from test5;
Query OK, 3 rows affected (0.00 sec)
(localhost@testdb)[root]
(localhost@testdb)[root]
(localhost@testdb)[root] select * from test5;
Empty set (0.00 sec)
(localhost@testdb)[root] insert into test5(name) values(aa),(bb),(cc
Query OK, 3 rows affected (0.00 sec)
(localhost@testdb)[root] select * from test5;
+—-+——+
| id | name |
+—-+——+
| 4 | aa |
| 5 | bb |
| 6 | cc |
+—-+——+
3 rows in set (0.00 sec)
truncate 表里的數據后在插入數據,id 列會從 1 開始增長。
(localhost@testdb)[root] truncate table test5;
Query OK, 0 rows affected (0.00 sec)
(localhost@testdb)[root] select * from test5;
Empty set (0.00 sec)
(localhost@testdb)[root] insert into test5(name) values(aa),(bb),(cc
Query OK, 3 rows affected (0.00 sec)
(localhost@testdb)[root] select * from test5;
+—-+——+
| id | name |
+—-+——+
| 1 | aa |
| 2 | bb |
| 3 | cc |
+—-+——+
3 rows in set (0.00 sec)
(localhost@testdb)[root]
對于復合索引的自增長列
myisam 引擎的自增長列,在索引中是非前導列可以創建成功
innodb 引擎的自增長列,在索引中必須是前導列,表才能創建成功
(localhost@testdb)[root] create table test4 (id1 int auto_increment,id2 int,name varchar(10),index(id2,id1)) engine=myisam;
Query OK, 0 rows affected (0.00 sec)
(localhost@testdb)[root]
(localhost@testdb)[root]
(localhost@testdb)[root] drop table test4;
Query OK, 0 rows affected (0.00 sec)
(localhost@testdb)[root] create table test4 (id1 int auto_increment,id2 int,name varchar(10),index(id2,id1)) engine=innodb;
ERROR 1075 (42000):
(localhost@testdb)[root]
(localhost@testdb)[root]
(localhost@testdb)[root] create table test4 (id1 int auto_increment,id2 int,name varchar(10),index(id1,id2)) engine=innodb;
Query OK, 0 rows affected (0.01 sec)
(localhost@testdb)[root]
(localhost@testdb)[root]
(localhost@testdb)[root]
以上就是如何理解 mysql 自增長列,丸趣 TV 小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注丸趣 TV 行業資訊頻道。