事務的特征:
Atomicity(原子性)
Consistency(穩定性,一致性)
Isolation(隔離性)
Durability(可靠性)
【事務只針對對數據數據產生影響的語句有效】
show engines //查看mysql鎖支持的數據引擎
MyISAM不支持事物,InnoDB支持事物
默認情況下,MySQL將以自動提交模式運行,這意味著沒一條小命令都將當做一個只有一條命令的事物來執行。
如果要讓mysql支持支持事務,只需要修改數據引擎(alter table person type=INNODB)
使用start transaction或者begin命令來開啟一個事物,使用commit,或者rollback來結束事物。
事物的結束:事物除了commit,rollback會結束外,使用DDL或者DCL語句也會結束。
保存點:通過保存點機制:用戶可以在事物里用savepoint name命令設置一些保存點,以后用戶在使用rollback to savepoint name結束事物時,name之前的數據保存,之后的數據不保存。
mysql使用事務的關鍵字
1.begin //打開一個事務
2.commit //提交到數據庫
3.rollback //取消操作
4.savepoint //保存,部分取消,部分提交
alter table person type=INNODB //修改數據引擎
示例:
begin
update person set name='efgh' where id =10
select * from person
rollback
select * from person
示例:
alter table person type=INNODB
begin
update person set name='efgh' where id =10
select * from person
commit
select * from person
begin
delete from person where id=21
update person set name='efgh' where id =10
commit/rollback
針對上面部分提交,必須用到保存點
保存點注意:
1.只能取消到某個保存點 rollback to savepoint p1
2.不能提交某個保存 commit to savepoint p2//錯誤寫法
3.最后commit 把未取消的保存點去不提交到數據
事務保存點使用例子
1. begin;
2. update score set score=40 where scoreid=1;
3. savepoint s1;
4. update score set score=50 where scoreid=2;
5. select * from score;
6. rollback to savepoint s1;
7. select * from score;
8. commit;
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com