zoukankan      html  css  js  c++  java
  • mysql事务实例

    use test_1;
    -- 创建一个账户表
    create table my_account(
    number char(16) not null unique comment '账户',
    name varchar(50) not NULL,
    money decimal(10,2) default 0.0 comment '账户余额'
    )charset utf8;

    -- 插入数据
    insert into my_account VALUES
    ('000000001','张三',1000),
    ('000000002','李四',2000);

    drop table my_account;

    select * from my_account;
    alter table my_account add id int primary key auto_increment first;
    update my_account set money = money -1000 where id=1;

    -- 事务(手动操作)
    -- 开启事务
    start transaction;
    -- 李四账户减少
    update my_account set money = money -1000 where id=2;
    -- 张三账户增加
    update my_account set money = money +1000 where id=1;
    -- 关闭事务
    -- a)提交事务:同步数据库(操作成功)commit
    -- b)回滚事务:直接清空日志表(操作失败)rollback
    commit;

    -- 事务(回滚点)
    -- 开启事务
    start transaction;
    -- 事务1:张三加钱
    update my_account set money = money + 10000 where id = 1;
    -- 设置回滚点
    savepoint sp1;
    -- 银行扣税
    update my_account set money = money - 10000 * 0.05 where id = 2;-- 错误
    -- 回滚到回滚点
    rollback to sp1;
    -- 继续操作
    update my_account set money = money - 10000 * 0.05 where id = 1;
    -- 关闭事务
    commit;

    -- 事务(自动)
    -- 关闭自动提交:set autocommit = off/0;
    show variables like 'autocommit';
    set autocommit = 0;
    -- 张三加钱
    update my_account set money = money - 10000 where id = 1;
    -- 自动提交
    set autocommit = 1;

    -- 事务的特性:ACID 原子性,一致性,隔离性,持久性

    生活就要逢山开路遇水搭桥,愿共勉!
  • 相关阅读:
    LSA
    DBSCAN
    层次聚类
    crontab 不产生邮件
    vue页面添加当前日期,并且格式化
    SQL去重复数据
    Idea防沉迷插件StopCoding的安装使用教程
    动漫
    intellij-idea开启rundashboard配置
    SpringCloud之Eureka注册中心原理及其搭建
  • 原文地址:https://www.cnblogs.com/TianMu/p/7843473.html
Copyright © 2011-2022 走看看