事務(wù)的特征:
Atomicity(原子性)
Consistency(穩(wěn)定性,一致性)
Isolation(隔離性)
Durability(可靠性)
【事務(wù)只針對對數(shù)據(jù)數(shù)據(jù)產(chǎn)生影響的語句有效】
show engines //查看mysql鎖支持的數(shù)據(jù)引擎
MyISAM不支持事物,InnoDB支持事物
默認(rèn)情況下,MySQL將以自動提交模式運行,這意味著沒一條小命令都將當(dāng)做一個只有一條命令的事物來執(zhí)行。
如果要讓mysql支持支持事務(wù),只需要修改數(shù)據(jù)引擎(alter table person type=INNODB)
使用start transaction或者begin命令來開啟一個事物,使用commit,或者rollback來結(jié)束事物。
事物的結(jié)束:事物除了commit,rollback會結(jié)束外,使用DDL或者DCL語句也會結(jié)束。
保存點:通過保存點機制:用戶可以在事物里用savepoint name命令設(shè)置一些保存點,以后用戶在使用rollback to savepoint name結(jié)束事物時,name之前的數(shù)據(jù)保存,之后的數(shù)據(jù)不保存。
mysql使用事務(wù)的關(guān)鍵字
1.begin //打開一個事務(wù)
2.commit //提交到數(shù)據(jù)庫
3.rollback //取消操作
4.savepoint //保存,部分取消,部分提交
alter table person type=INNODB //修改數(shù)據(jù)引擎
示例:
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 把未取消的保存點去不提交到數(shù)據(jù)
事務(wù)保存點使用例子
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;
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com