欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据库事务演示

程序员文章站 2022-07-12 16:55:11
...

https://blog.csdn.net/qq_40794973/article/details/104057217


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;

 

相关标签: 分布式事务