读锁(共享锁):针对同一份数据,多个读操作可以同时进行而不会互相影响。
写锁(排它锁):当前操作没有完成之前,它会阻断其他的写操作或读操作。
表锁:操作时,会锁定整个表。开销小,加锁快;不会出现死锁;锁定力度大,发生锁冲突概率高,并发度最低。
行锁:操作时,会锁定当前操作的表的某些行。开销大,加锁慢;会出现死锁;锁定粒度小,发生锁冲突的概率低,并发度高。
页锁:操作时,会锁定当前操作的表的某些页。开销和加锁速度介于表锁和行锁之间;会出现死锁;锁定粒度介于表锁和行锁之间,并发度一般。
读锁:lock table table_name read;
写锁:lock table table_name write;
解锁:unlock tables;
create
table tb_myisam(
id
INT AUTO_INCREMENT,
name
VARCHAR(
30),
age
INT,
PRIMARY
KEY(
id))
engine=myisam;
insert
into tb_myisam(
name,age)
value(
'zhang3',
20);
insert
into tb_myisam(
name,age)
value(
'li4',
21);
insert
into tb_myisam(
name,age)
value(
'wang5',
22);


3.2 InnoDB的表锁和行锁
读锁:select * from table_name where ... lock in share mode;
写锁:select * from table_name where ... for update;
create table tb_innodb (id INT AUTO_INCREMENT,name VARCHAR(30),age INT,PRIMARY KEY(id)) engine=innodb; insert into tb_innodb(name,age) value('zhang3',20);insert into tb_innodb(name,age) value('li4',21);insert into tb_innodb(name,age) value('wang5',22);



3.2.4 演示InnoDB行的写锁:

