当前位置:网站首页>Mysql--事务
Mysql--事务
2022-06-11 10:01:00 【不想当个程序员】
事务:要么都成功、要么都失败(比如两个人转账)
将一组sql放在一个批次中去执行
事务原则:ACID原则
- 原子性(Atomicity):针对同一个事务,要么都完成,要么都不完成
- 一致性(Consistency):(最终一致性)针对一个事务操作前与操作后的状态一致
- 隔离性(Isolation):针对多个用户同时操作,主要是排除其他食物对本次事务的影响
- 持久性(Durability):事务结束后的数据不随着外界原因导致丢失(事务没有提交,恢复到原状,事务已经提交,持久化到数据库)
事务的隔离级别:
- 脏读:指一个事务读取了另外一个事务未提交的数据
- 不可重复度:在一个事务读取表中某一行数据,多次读取结果不同。(这个不一定是错误,只是某些场合不对)
- 虚读(幻读):是指在一个事务内读取到了别的事务插入的数据,导致前后读取不一致。(一般是行影响,多了一行)
执行事务
– mysql是默认开启事务自动提交的
set autocommit = 0 /*关闭*/
set autocommit = 1 /*开启(默认的)*/
-- 手动处理事务
-- 事务开启
start transaction --标记一个事务开始,从之后的sql都在一个事务内
insert **
insert **
-- 提交:持久化(成功!)
commit
--回滚:回到原来的样子(失败!)
rollback
--事务结束
set autocommit = 1 --开启自动提交
--了解
savepoint 保存点名称 --设置一个事务的保存点
rollback to savepoint 保存点名称 --回滚到保存点
release savepoint 保存点名称 --撤销保存点
模拟转账
create database shop character set utf8 collate utf8_general_ci
use shop
create table account
(
id int(3)not null auto_increment ,
name varchar(20) not null,
money decimal(9,2) not null,
primary key (id)
)engine=InnoDB default charset = utf8;
插入两行数据
模拟转账事务
set autocommit = 0; --1 关闭自动提交
start transaction --2 开启一个事务
update account set money=money-500 where name='ming'--3
update account set money=money+500 where name='hao'--4
commit;--5
rollback;--6
set autocommit = 1;--7
一句一句执行,在执行第3、4句之后,如果直接commit 就无法rollback。
边栏推荐
- An error can't locate env pm in @INC
- Tree topology networking structure of ZigBee module communication protocol
- 不卷了!入职字节跳动一周就果断跑了。
- Es6新特性——箭头函数
- 「INS-30131」 安装程序验证所需的初始设置失败
- Empire CMS imitation "game bird" template source code /92kaifa version large mobile game portal template
- Cisp-pte XSS Foundation
- parker派克先导式电磁阀和直动式电磁阀的区别有哪些?
- Interface, abstract class and relationship between classes
- Rebuilding Oracle XdB components
猜你喜欢

What are the differences between Parker pilot solenoid valve and direct acting solenoid valve?

Reconstruction des composants Oracle XDB

How much do you know about software compatibility testing? How to select a software compatibility testing organization?

ESP8266_ SNTP(Simple Network Time Protocol)

Oracle XDB组件的重建

ugui图片墙

赛灵思引脚约束文件 .xdc

BeanFactoryPostProcessor 与BeanPostProcessor的区别

Empire CMS imitates DIY handmade website source code of craft activity /92kaifa imitates self-adaptive mobile phone version template of craft activity

Empire CMS imitation "game bird" template source code /92kaifa version large mobile game portal template
随机推荐
流式计算知识
「INS-30131」 安装程序验证所需的初始设置失败
LeetCode刷题 —— 手撕二叉树
BeanFactoryPostProcessor 与BeanPostProcessor的区别
Image quality evaluation including Matlab source code
With determination to forge ahead, JASMINER continues to deepen its brand strength
ESP8266_ Mqtt protocol
Cisp-pte XSS Foundation
帝国CMS仿《游戏鸟》模板源码/92kaifa版大型手机游戏门户网站模板
CISP-PTE XSS基础
卸载grid时运行脚本报错Can‘t locate Env.pm in @INC
How do online app stores of laundry chain stores do?
ugui图片墙
DataGrip 2022,DataGrip 功能
LoRa模块无线收发通信技术详解
Ora-00059 exceeds DB_ Files limit
GDB debugging common commands
Can station B make money?
ZigBee模块无线传输星形拓扑组网结构简介
有哪些ABAP关键字和语法,到了ABAP云环境上就没办法用了?