//先建个表
create table book(
id smallint unsigned primary key,
name char(10) not null default '',
num int unsigned not null default 0
)engine =innodb charset=utf8
//插入几条数据
insert into book
values
('1','三国','1'),
('2','水浒','2');
set autocommit = 1;//设置mysql 不默认自动提交
start transaction;//开始事务
select * from book where id= 1 for update;//针对update锁行
update book set num = num-1 where id=1;
倘若此时另外一个用户:update book set nun = num-1 where id =1;
查询会停滞...
直到上边的操作提交:
commit;//一个事务结束
注意:这里id是主键索引 所以只是行锁
倘若没加索引:那就是表锁了:你update book set num = num -1 where id=2 //id=2的受到影响了---》 表锁
也是会停滞的...