zoukankan      html  css  js  c++  java
  • Mysql-事务,原子性操作

    事物分为 自动(默认的)和手动(需开启事务)两种

    --
    事务 start transaction; -- commite; 提交事务 -- rollback; 回滚事务
    create table if not exists account(
        id int primary key auto_increment,
        name varchar(14) not null,
        RMB int
    );
    
    insert into account(name,RMB) values('Which',1000),('Tj',998);

    rollback; 进行一步操作后,可以撤回这个操作

    -- 开始事务    手动事物

    start transaction;

    -- 事物操作
    update account set RMB =RMB-992 where name = 'Which';
    -- q  退出mysql(中断事务)
    -- mysql -u name -p;  回到mysql   
    -- 继续事务操作
        -- 回滚事务
        rollback;
    +----+-------+------+   -- insert
    | id | name  | RMB  |
    +----+-------+------+
    |  1 | Which |    8 |
    |  2 | Tj    |  998 |
    +----+-------+------+ 
    -- update  |
    --+-------+------+
    | id | name  | RMB  |
    +----+-------+------+
    |  1 | Which |    8 |
    |  2 | Tj    |  998 |
    +----+-------+------+
    -- rollback | +----+-------+------+ | id | name | RMB | +----+-------+------+ | 1 | Which | 8 | | 2 | Tj | 998 | +----+-------+------+

    commite:   自动提交

    -- 事物操作
    update account set RMB =RMB-992 where name = 'Which';
    -- q  退出mysql(事务)
    -- mysql -u name -p;  回到mysql
    -- 实际上在这儿退出后,再回到account表,值并没有-992,但是再进行下一步操作后,992就转给Tj了。
     -- 继续事务操作 update account set RMB =RMB+992 where name = 'Tj';
    -- insert   Which 1000,Tj 998
    +----+-------+------+
    | id | name  | RMB  |
    +----+-------+------+
    |  1 | Which | 1000 |
    |  2 | Tj    |  998 |
    +----+-------+------+
    -- update   Which-992
    +----+-------+------+
    | id | name  | RMB  |
    +----+-------+------+
    |  1 | Which |    8 |
    |  2 | Tj    |  998 |
    +----+-------+------+
    -- update   Tj+992
    +----+-------+------+
    | id | name  | RMB  |
    +----+-------+------+
    |  1 | Which |    8    |
    |  2 | Tj       | 1990  |
    +----+-------+------+

    原子性操作:Innodb引擎支持事务

    要么全部成功,要么全部失败 

  • 相关阅读:
    数据挖掘中所需的概率论与数理统计知识、上
    pig语法学习 FOREACH GENERATE group AS
    pig flatten
    F-stack及其Nginx、redis的编译安装
    f-stack中nginx配置后make出现error: ignoring return value of ‘ftruncate’
    高速网络流量测量方法的记录
    DPDK测试用例(sample)编译
    VPP(Vector Packet Processing)配置工具
    DMA(Direct Memory Access)简介
    VPP(Vector Packet Processing)浅析
  • 原文地址:https://www.cnblogs.com/tangpg/p/8125313.html
Copyright © 2011-2022 走看看