事务就是是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位
ACID,是指在可靠数据库管理系统(DBMS)中,事务(transaction)中应该具有的四个特性:原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、持久性(Durability)。
指事务是一个不可再分割的工作单位,事务中的操作要么都发生,要么都不发生。
指在事务开始之前和事务结束以后,数据库的完整性约束没有被破坏
指在并发环境中,当不同的事务同时操纵相同的数据时,每个事务都有各自的完整数据空间
show global variables like '%isolation%';
select @@global.tx_isolation;
show session variables like '%isolation%';
select @@session.tx_isolation;
select @@tx_isolation;
set global transaction isolation level serializable;
set session transaction isolation level read committed;
begin 或 start transaction:显式地开启一个事务。
commit 或 commit work:提交事务,并使已对数据库进行的所有修改变为永久性的。
rollback 或 rollback work:回滚会结束用户的事务,并撤销正在进行的所有未提交的修改。
savepoint s1:使用 savepoint 允许在事务中创建一个回滚点,一个事务中可以有多个
savepoint;“s1”代表回滚点名称。
rollback to [savepoint] S1:把事务回滚到标记点。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UkYTQRQ2-1663227511325)(F:\typorase\jpg\377.png)]](https://1000bd.com/contentImg/2023/11/07/234019269.png)
insert into info values(1,'A',1000),(2,'B',2000);
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9xnnQQBl-1663227511326)(F:\typorase\jpg\378.png)]](https://1000bd.com/contentImg/2023/11/07/234019247.png)
begin;
update info set money= money-100 where id=1;
select * from info;
commit;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EeOnEtnn-1663227511326)(F:\typorase\jpg\379.png)]](https://1000bd.com/contentImg/2023/11/07/234019521.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yVZUy4wE-1663227511327)(F:\typorase\jpg\380.png)]](https://1000bd.com/contentImg/2023/11/07/234019647.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VZzmQnbr-1663227511327)(F:\typorase\jpg\381.png)]](https://1000bd.com/contentImg/2023/11/07/234019635.png)
begin;
update info set money= money+900 where id=1;
select * from info;
rollback;
#开始事务
#将id=1的记录money+900
#发现数据错误,进行回滚
#因为没有提交事务,所以别的用户看的数据一直没有变化
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mW9UaO8X-1663227511327)(F:\typorase\jpg\382.png)]](https://1000bd.com/contentImg/2023/11/07/234019292.png)
begin;
update info set money= money+50 where id=1;
savepoint a;
select * from info;
update info set money= money+500 where id=2;
savepoint b;
select * from info;
rollback to a;
commit;
select * from info;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8XYZsX2v-1663227511327)(F:\typorase\jpg\383.png)]](https://1000bd.com/contentImg/2023/11/07/234019664.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HWWeRtsQ-1663227511328)(F:\typorase\jpg\384.png)]](https://1000bd.com/contentImg/2023/11/07/234019573.png)
SET AUTOCOMMIT=0; #禁止自动提交
SET AUTOCOMMIT=1; #开启自动提交,Mysql默认为1
SHOW VARIABLES LIKE 'AUTOCOMMIT'; #查看Mysql中的AUTOCOMMIT值
如果没有开启自动提交,当前会话连接的mysql的所有操作都会当成一个事务直到你输入rollback|commit;当前事务才算结束。当前事务结束前新的mysql连接时无法读取到任何当前会话的操作结果。
如果开起了自动提交,mysql会把每个sql语句当成一个事务,然后自动的commit。
当然无论开启与否,begin; commit|rollback; 都是独立的事务。
select * from info;
SET AUTOCOMMIT=0;
SHOW VARIABLES LIKE 'AUTOCOMMIT';
update info set money= mpney-50 where id=1;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JB6e86Fw-1663227511328)(F:\typorase\jpg\385.png)]](https://1000bd.com/contentImg/2023/11/07/234019922.png)
我们用另一个用户查看这张表,发现数据并没有发生变化,因为禁止了自动提交。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lZScWSdJ-1663227511328)(F:\typorase\jpg\386.png)]](https://1000bd.com/contentImg/2023/11/07/234019914.png)
commit;
手动提交后,数据变化。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2XxbpPWm-1663227511328)(F:\typorase\jpg\387.png)]](https://1000bd.com/contentImg/2023/11/07/234019266.png)