数据库事务演示
程序员文章站
2022-07-12 16:55:11
...
1 创建数据库和账户表
DROP DATABASE IF EXISTS tx;
CREATE DATABASE tx;
USE tx;
CREATE TABLE account(
id INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(40),
money FLOAT
);
INSERT INTO account(`name`,money) VALUES('SuperMan',1000);
INSERT INTO account(`name`,money) VALUES('BatMan',1000);
SELECT * FROM account;
2 重账户置表
TRUNCATE TABLE account;
INSERT INTO account(`name`,money) VALUES('SuperMan',1000);
INSERT INTO account(`name`,money) VALUES('BatMan',1000);
SELECT * FROM account;
3 MySql8.0 查看事务隔离级别
SELECT @@global.transaction_isolation,@@transaction_isolation;
SHOW VARIABLES LIKE '%iso%';
MySql 默认的事务隔离级别:REPEATABLE READ
4 修改事务的隔离级别
SET SESSION TRANSACTION ISOLATION LEVEL Xxx ;
5 模拟 “银行转账”
START TRANSACTION;
UPDATE account SET money = money - 100 WHERE `name` = 'SuperMan';
UPDATE account SET money = money + 100 WHERE `name` = 'BatMan';
COMMIT;
上一篇: 分布式事务TXC挂起方法
下一篇: 某某杂记