-- 转账
CREATE DATABASE shop CHARACTER SET utf8 COLLATE utf8_general_ci
USE shop
CREATE TABLE `account`(
`id` INT(18) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(10) NOT NULL,
`money` DECIMAL(9,2) NOT NULL,
PRIMARY KEY (`id`)
)ENGINE = INNODB DEFAULT CHARSET=utf8
-- 插入数据
INSERT INTO `account`(`name`,`money`)
VALUES ('A','1000'),('B','200')
-- 这是我之前多运行了一编,导致出现两个A两个B
UPDATE `account` SET `id`=1 WHERE `id`=3
UPDATE `account` SET `id`=2 WHERE `id`=4
-- 模拟转账:事务
SET autocommit = 0; -- 关闭事务自动提交
START TRANSACTION -- 开启一个事务
UPDATE `account` SET money = money - 500 WHERE `name` = 'A'
UPDATE `account` SET money = money + 500 WHERE `name` = 'B'
COMMIT; -- 提交事务 持久化事务
ROLLBACK; -- 回滚
SET autocommit = 1; -- 回复提交