首先创建一个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;

SELECT
	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'

 

 

  

相关文章:

  • 2021-10-29
  • 2021-12-23
  • 2022-12-23
  • 2021-11-07
  • 2022-12-23
  • 2021-08-15
  • 2021-08-18
  • 2021-10-14
猜你喜欢
  • 2021-08-12
  • 2022-12-23
  • 2021-05-18
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2021-12-01
相关资源
相似解决方案