事務(wù)特性 | 作用 |
---|---|
原子性(Atomic) | 事務(wù)的所有操作,要么全部完成,要么全部不完成,不會(huì)結(jié)束在某個(gè)中間環(huán)節(jié)。 |
一致性(Consistency) | 事務(wù)開始之前和事務(wù)結(jié)束之后,數(shù)據(jù)庫的完整性限制未被破壞。 |
隔離性(Isolation) | 當(dāng)多個(gè)事務(wù)并發(fā)訪問數(shù)據(jù)庫中的同一數(shù)據(jù)時(shí),所表現(xiàn)出來的是相互關(guān)系。 |
持久性(Durability) | 事務(wù)完成之后,所做的修改會(huì)進(jìn)行持久化保存,不會(huì)丟失。 |
區(qū)別:
隔離級(jí)別:
隔離級(jí)別 | 作用 |
---|---|
SERIALIZABLE (串行化) |
避免臟讀、不可重復(fù)讀、幻讀 |
REPEATABLE-READ (可重復(fù)讀) |
避免臟讀、不可重復(fù)讀 |
READ-COMMITTED (讀已提交) |
避免臟讀 |
READ-UNCOMMITTED (讀未提交) |
無作用 |
MySQL 支持上面 4 種隔離級(jí)別,默認(rèn)為可重復(fù)讀。如若想修改隔離級(jí)別需: sed -i '/\[mysqld]/a transaction-isolation = SERIALIZABLE' /etc/my.cnf
mysql> show variables like '%tx_is%'; mysql> exit [root@MySQL ~]# sed -i '/\[mysqld]/a transaction-isolation = SERIALIZABLE' /etc/my.cnf [root@MySQL ~]# systemctl restart mysqld [root@MySQL ~]# mysql -uroot -p123123 -e "show variables like '%tx_is%';"
管理事務(wù)的三個(gè)命令:
mysql> create table C(ID int); mysql> insert into C values(1),(2); mysql> select * from C; mysql> BEGIN; mysql> insert into C values(3); mysql> COMMIT; mysql> select * from C;
mysql> show variables like 'autocommit'; #查看是否開啟自動(dòng)提交事務(wù) mysql> BEGIN; mysql> insert into C values(4) mysql> select * from C; mysql> exit [root@localhost ~]# mysql -uroot -p123123 -e "select * from Coco.C where ID=4"
set autocommit=0
:在數(shù)據(jù)庫中修改為臨時(shí)生效(如若想永久修改需 sed -i '/\[mysqld]/a autocommit=0' /etc/my.cnf
來修改)
mysql> set autocommit=0; mysql> select * from Coco.C; mysql> insert into Coco.C values(4); mysql> select * from Coco.C where ID=4; [root@localhost ~]# mysql -uroot -p123123 -e "select * from Coco.C where ID=4"
注意:
mysql> select ID as "編號(hào)",Name as "姓名",Department as "部門" from A where ID=1; mysql> select ID "編號(hào)",Name "姓名",Department "部門" from A where ID=1;
mysql> select distinct Department from A;
AND:邏輯與(條件都要滿足);OR:邏輯或(條件只需要滿足一個(gè))。
mysql> select * from A where ID >= 3 and Department = 2; mysql> select * from A where ID >= 3 or Department = 2;
mysql> select * from A where ID in(1,3,4); mysql> select * from A where ID not in(1,3,4); mysql> select * from A where ID between 1 and 3;
mysql> select * from A where Name like "%三%"; mysql> select * from A where Name like "%三%" or Name like "%四";
mysql> select * from A order by ID desc; mysql> select * from A order by Department,ID desc;
mysql> select * from C; mysql> select * from C limit 2; mysql> select * from C limit 0,2;
到此這篇關(guān)于MySQL主鍵與事務(wù)的文章就介紹到這了,更多相關(guān)MySQL主鍵與事務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:西寧 迪慶 麗水 龍巖 徐州 南充 無錫 自貢
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《圖文詳解MySQL中的主鍵與事務(wù)》,本文關(guān)鍵詞 圖文,詳解,MySQL,中的,主鍵,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。