Oracle为DML操作自动获取行锁和表锁,操作的类型决定了锁的行为,下面对DML操作锁的情况作了一个汇总:
SQL Statement | Row Locks | Table Lock Mode | RS | RX | S | SRX | X |
---|---|---|---|---|---|---|---|
SELECT ... FROM table ... |
—— | none | Y | Y | Y | Y | Y |
INSERT INTO table ... |
Yes | SX | Y | Y | N | N | N |
UPDATE table ... |
Yes | SX | Y(注) | Y(注) | N | N | N |
MERGE INTO table ... |
Yes | SX | Y | Y | N | N | N |
DELETE FROM table ... |
Yes | SX | Y(注) | Y(注) | N | N | N |
SELECT ... FROM table FOR UPDATE OF ... |
Yes | SX | Y(注) | Y(注) | N | N | N |
LOCK TABLE table IN ... |
—— | ||||||
ROW SHARE MODE |
SS | Y | Y | Y | Y | N | |
ROW EXCLUSIVE MODE |
SX | Y | Y | N | N | N | |
SHARE MODE |
S | Y | N | Y | N | N | |
SHARE ROW EXCLUSIVE MODE |
SSX | Y | N | N | N | N | |
EXCLUSIVE MODE |
X | N | N | N | N | N |
注:如果另一个事务和当前事务出现行冲突,则需要等待
下面阐述当行被查询和修改时会涉及到的锁。
UPDATE t SET x = ( SELECT y FROM t2 WHERE t2.z = t.z ) WHERE a > 5;事务将不会对子查询(SELECT y FROM t2 WHERE t2.z = t.z)涉及的行加锁。
原文地址:http://blog.csdn.net/tomato__/article/details/42711111