如何理解mysql自增长列

34次阅读
没有评论

共计 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 行业资讯频道。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-19发表,共计3420字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)