共同点:两者都必须在事务中使用
不同点:for update 对记录加写锁,此时记录不能被其他线程加读锁或者写锁。
lock in share mode对记录加读锁,此时记录能被其他线程加读锁,不能加写锁。但是可以在当前事务内,再加写锁。
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test where id=7 lock in share mode;
+----+----------+-------------+----------+------+----------+
| id | testEnum | testdecimal | sizetest | date | dateCopy |
+----+----------+-------------+----------+------+----------+
| 7 | 2 | NULL | NULL | NULL | NULL |
+----+----------+-------------+----------+------+----------+
1 row in set (0.00 sec)
mysql> select * from test where id=7 for update;
+----+----------+-------------+----------+------+----------+
| id | testEnum | testdecimal | sizetest | date | dateCopy |
+----+----------+-------------+----------+------+----------+
| 7 | 2 | NULL | NULL | NULL | NULL |
+----+----------+-------------+----------+------+----------+
1 row in set (0.00 sec)