命令行窗口1 | 命令行窗口2 | 命令行窗口3 |
---|
mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set |mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; 等待 |mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '3' for update; +--------+------+----------+ | userID | name | passWord | +--------+------+----------+ | 3 | tong | 123456 | +--------+------+----------+ 1 row in set |mysql> commit; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
(2)查詢非索引的字段來查詢數(shù)據(jù)庫使用行鎖
打開兩個命令行窗口進(jìn)行測試
命令行窗口1 | 命令行窗口2 |
---|
|mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'liujie' for update; +--------+--------+----------+ userID name passWord +--------+--------+----------+ 1 liujie 123456 +--------+--------+----------+ 1 row in set |mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'tong' for update; 等待| mysql> commit; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'liujie' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
##### (3)查詢非唯一索引字段來查詢數(shù)據(jù)庫使用行鎖鎖住多行
mysql的行鎖是針對索引假的鎖,不是針對記錄,所以可能會出現(xiàn)鎖住不同記錄的場景
打開三個命令行窗口進(jìn)行測試
命令行窗口1 命令行窗口2 命令行窗口3
mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where password = '123456 ' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | | 3 | tong | 123456 | +--------+--------+----------+ 2 rows in set mysql> set autocommit =0 ; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update;
等待
mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '2 ' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 2 | yitong | 123 | +--------+--------+----------+ 1 row in set commit; mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
##### (4)條件中使用索引來操作檢索數(shù)據(jù)庫時,是否使用索引還需有mysql通過判斷不同執(zhí)行計劃來決定,是否使用該索引,如需判定如何使用explain來判斷索引,請聽下回分解
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
標(biāo)簽:鄂爾多斯 黔西 昌都 駐馬店 荊門 梅河口 陜西 北京
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL中的行級鎖定示例詳解》,本文關(guān)鍵詞 MySQL,中的,行級,鎖定,示例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。