mysql increment by 2_关于mysql auto-increment
創(chuàng)建表語(yǔ)句如下
mysql> show create table Tautoincrement\G
*************************** 1. row ***************************
Table: Tautoincrement
Create Table: CREATE TABLE `Tautoincrement` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
如下插入數(shù)據(jù)報(bào)錯(cuò)
mysql> insert into Tautoincrement values('zs');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
即需要提供與建表時(shí)相同的列值
故用下述方式插入數(shù)據(jù)
mysql> insert into Tautoincrement(name) values('zs');
查詢結(jié)果如下
+----+------+
| id | name |
+----+------+
| 1 | zs |
繼續(xù)插入數(shù)據(jù)
mysql> insert into Tautoincrement(id,name) values(3,'zs');
查詢結(jié)果如下
+----+------+
| id | name |
+----+------+
| 1 | zs |
| 3 | zs |
依舊沒(méi)什么問(wèn)題,但是可以得出一個(gè)結(jié)論,innodb類(lèi)型的數(shù)據(jù)庫(kù)允許用戶插入autoincrement限制的列的值,與sqlserver有所不同,
繼續(xù)插入數(shù)據(jù)
mysql> insert into Tautoincrement(name) values('zss');
查詢結(jié)果如下
+----+------+
| id | name |
+----+------+
| 1 | zs |
| 3 | zs |
| 4 | zss |
可以發(fā)現(xiàn),已經(jīng)autoincrement的默認(rèn)值已經(jīng)跳過(guò)id=2的情況,需注意,
此時(shí)執(zhí)行 select last_insert_id() 返回結(jié)果為4 ###還有一個(gè)問(wèn)題,該函數(shù)返回該數(shù)據(jù)庫(kù)下的最新值,所以說(shuō)不一定是你最新插入的id,有點(diǎn)尷尬
后面驗(yàn)證得知
同時(shí)插入大量數(shù)據(jù), last_insert_id() 返回第一次插入時(shí)id
總結(jié)
以上是生活随笔為你收集整理的mysql increment by 2_关于mysql auto-increment的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jsp怎么连接mysql_jsp如何连接
- 下一篇: mysql事务未提交读_mysql事务之