1. 程式人生 > >MySQL事務以及select...for update

MySQL事務以及select...for update

舉個例子:

假設有個表單products ,裡面有id 跟name 二個欄位,id 是主鍵。

例1: (明確指定主鍵,並且有此資料,row lock)

SELECT * FROM products WHERE id='3' FOR UPDATE;

例2: (明確指定主鍵,若查無此資料,無lock)

SELECT * FROM products WHERE id='-1' FOR UPDATE;

例2: (無主鍵,table lock)

SELECT * FROM products WHERE name='Mouse' FOR UPDATE;

例3: (主鍵不明確,table lock)

SELECT * FROM products WHERE id<>'3' FOR UPDATE;

例4: (主鍵不明確,table lock)

SELECT * FROM products WHERE id LIKE '3' FOR UPDATE;