mysql自增長列
自增長列必須是索引列,否則無法建立成功表,對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]>
(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]>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29108064/viewspace-1995239/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL設定表自增步長MySql
- mysql自增和orcale自增MySql
- Mysql實現自增長編號,日期+序列MySql
- mysql自增長id用完了該怎麼辦MCBZMySql
- MySQL自增約束MySql
- 深入剖析 MySQL 自增鎖MySql
- 自增長列和序列的區別(identity與sequence的區別)IDE
- MySQL 主鍵自增也有坑?MySql
- MySQL 中的自增主鍵MySql
- 向Mysql主鍵自增長表中新增資料並返回主鍵MySql
- Mysql關於自增主鍵,自增主鍵優化總結MySql優化
- MySQL 主鍵自增 Auto Increment用法MySqlREM
- MySQL新增自增主鍵的坑MySql
- MySQL8自增主鍵變化MySql
- MySQL自增主鍵跳號問題MySql
- SqlServer主鍵和自增長設定SQLServer
- MySQL 8.0特性-自增變數的持久化MySql變數持久化
- 深入瞭解MySQL中的自增主鍵MySql
- MySQL 8 和 MySQL 5.7 在自增計數上的區別MySql
- mysql獲取指定表當前自增id值MySql
- 基於MySQL自增ID欄位增量掃描研究MySql
- MySQL 8 新特性之自增主鍵的持久化MySql持久化
- 一文詳解MySQL如何同時自增自減多個欄位MySql
- MySQL 資料庫自增主鍵生成的優缺點MySql資料庫
- SQLite設定主鍵自動增長及插入語法SQLite
- 自增長主鍵回顯實現,批次資料插入
- 自動增長Textareas的最乾淨技巧「心得分享」
- PLG SaaS 案例:如何實踐外鏈自動增長策略?
- 好程式設計師大資料培訓分享MySQL資料庫約束條件和自增長序列程式設計師大資料MySql資料庫
- mysql預先取自動增長主鍵的下一個值MySql
- mplus資料分析:增長模型潛增長模型與增長混合模型再解釋模型
- 實訓:使用while與自增運算子迴圈遍歷陣列While陣列
- 自動增長配置不合理導致的效能抖動
- 騰訊雲自研伺服器成爆款雲產品 規模增長30倍,客戶增長50倍伺服器
- linux 中判斷一列資料是否按照指定步長遞增Linux
- 如何增長程式碼長度
- MyBatis實現MySQL表欄位及結構的自動增刪MyBatisMySql
- java面試一日一題:mysql中的自增主鍵Java面試MySql
- DB2巧用欄位自動增長主鍵的方法DFDB2