mysql如果主键重复了会发⽣什么情况⾸先创建⼀个person表:
create TABLE `person`(
`id` int not null auto_increment,
`name` VARCHAR(255) ,
`age` int,
PRIMARY key (`id`)
)
同时打开两个sql窗⼝
set autocommit=off;
set @id=-1;
SELECT
auto_increment into @id
FROM
information_schema.`TABLES`
WHERE
table_name = 'person'
AND TABLE_SCHEMA = 'test';  -- 第1步运⾏到这⾥
INSERT into person(id,name,age) VALUES(@id,'lisi',28);  -- 第3步运⾏这⾥
COMMIT;  -- 第5步运⾏这⾥(第⼆种,第4步先运⾏这⾥)
set autocommit=off;
set @id:=-1;
SELECTvalues什么意思
auto_increment into @id
FROM
information_schema.`TABLES`
WHERE
table_name = 'person'
AND TABLE_SCHEMA = 'test';    -- 第2步运⾏到这⾥
INSERT into person(id,name,age) VALUES(@id,'wangwu',28);  -- 第4步运⾏这⾥(第⼆种,第5步运⾏这⾥)
COMMIT;  -- 第6步运⾏这⾥
第⼀种,运⾏到第4步的时候,报错了:
[SQL] INSERT into person(id,name,age) VALUES(@id,'wangwu',28);
[Err] 1205 - Lock wait timeout exceeded; try restarting transaction
第⼆种,运⾏到第5步的时候
[SQL] INSERT into person(id,name,age) VALUES(@id,'wangwu',28);
[Err] 1062 - Duplicate entry '9' for key 'PRIMARY'

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。