zoukankan      html  css  js  c++  java
  • MariaDB TRANSACTION

    MariaDB TRANSACTION


    • Transaction—A block of SQL statements

    • Rollback—The process of undoing specified SQL statements

    • Commit—Writing unsaved SQL statements to the database tables

    • Savepoint—A temporary placeholder in a transaction set to which you can issue a rollback (as opposed to rolling back an entire transaction) 


    始终以START TRANSACTION为新起点遇到最后的ROLLBACK savepoint或COMMIT时自动关闭

    ROLLBACK

    (jlive)[crashcourse]>SELECT * FROM ordertotals; START TRANSACTION;

    +-----------+---------+

    | order_num | total   |

    +-----------+---------+

    |     20005 |  158.86 |

    |     20006 |   58.30 |

    |     20007 | 1060.00 |

    |     20008 |  132.50 |

    |     20009 |   40.78 |

    |     20009 |   40.78 |

    +-----------+---------+

    6 rows in set (0.00 sec)

    (jlive)[crashcourse]>START TRANSACTION;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>DELETE FROM ordertotals;

    Query OK, 6 rows affected (0.00 sec)


    (jlive)[crashcourse]>SELECT * FROM ordertotals;

    Empty set (0.00 sec)


    (jlive)[crashcourse]>ROLLBACK;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>SELECT * FROM ordertotals;

    +-----------+---------+

    | order_num | total   |

    +-----------+---------+

    |     20005 |  158.86 |

    |     20006 |   58.30 |

    |     20007 | 1060.00 |

    |     20008 |  132.50 |

    |     20009 |   40.78 |

    |     20009 |   40.78 |

    +-----------+---------+

    6 rows in set (0.00 sec)



    COMMIT

    START TRANSACTION;
    DELETE FROM orderitems WHERE order_num = 20010; 

    DELETE FROM orders WHERE order_num = 20010; 

    COMMIT; 


    SAVEPOINT

    (jlive)[crashcourse]>START TRANSACTION;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>SELECT * FROM ordertotals;

    +-----------+---------+

    | order_num | total   |

    +-----------+---------+

    |     20005 |  158.86 |

    |     20006 |   58.30 |

    |     20007 | 1060.00 |

    |     20008 |  132.50 |

    |     20009 |   40.78 |

    |     20009 |   40.78 |

    +-----------+---------+

    6 rows in set (0.00 sec)


    (jlive)[crashcourse]>SAVEPOINT delete1;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>DELETE FROM ordertotals WHERE order_num = 20009;

    Query OK, 2 rows affected (0.00 sec)


    (jlive)[crashcourse]>SELECT * FROM ordertotals;

    +-----------+---------+

    | order_num | total   |

    +-----------+---------+

    |     20005 |  158.86 |

    |     20006 |   58.30 |

    |     20007 | 1060.00 |

    |     20008 |  132.50 |

    +-----------+---------+

    4 rows in set (0.01 sec)


    (jlive)[crashcourse]>SAVEPOINT delete2;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>DELETE FROM ordertotals WHERE order_num = 20005;

    Query OK, 1 row affected (0.01 sec)


    (jlive)[crashcourse]>SELECT * FROM ordertotals;

    +-----------+---------+

    | order_num | total   |

    +-----------+---------+

    |     20006 |   58.30 |

    |     20007 | 1060.00 |

    |     20008 |  132.50 |

    +-----------+---------+

    3 rows in set (0.00 sec)


    (jlive)[crashcourse]>SAVEPOINT delete3;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>DELETE FROM ordertotals WHERE order_num = 20008;

    Query OK, 1 row affected (0.00 sec)


    (jlive)[crashcourse]>SELECT * FROM ordertotals;

    +-----------+---------+

    | order_num | total   |

    +-----------+---------+

    |     20006 |   58.30 |

    |     20007 | 1060.00 |

    +-----------+---------+

    2 rows in set (0.00 sec)


    (jlive)[crashcourse]>ROLLBACK TO delete3;

    Query OK, 0 rows affected (0.01 sec)


    (jlive)[crashcourse]>SELECT * FROM ordertotals;

    +-----------+---------+

    | order_num | total   |

    +-----------+---------+

    |     20006 |   58.30 |

    |     20007 | 1060.00 |

    |     20008 |  132.50 |

    +-----------+---------+

    3 rows in set (0.00 sec)


    (jlive)[crashcourse]>ROLLBACK TO delete2;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>SELECT * FROM ordertotals;

    +-----------+---------+

    | order_num | total   |

    +-----------+---------+

    |     20005 |  158.86 |

    |     20006 |   58.30 |

    |     20007 | 1060.00 |

    |     20008 |  132.50 |

    +-----------+---------+

    4 rows in set (0.00 sec)


    (jlive)[crashcourse]>ROLLBACK TO delete1;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>SELECT * FROM ordertotals;

    +-----------+---------+

    | order_num | total   |

    +-----------+---------+

    |     20005 |  158.86 |

    |     20006 |   58.30 |

    |     20007 | 1060.00 |

    |     20008 |  132.50 |

    |     20009 |   40.78 |

    |     20009 |   40.78 |

    +-----------+---------+

     

    6 rows in set (0.00 sec)



    修改默认的COMMIT方式


    (jlive)[crashcourse]>SHOW VARIABLES LIKE 'autocommit';

    +---------------+-------+

    | Variable_name | Value |

    +---------------+-------+

    | autocommit    | ON    |

    +---------------+-------+

    1 row in set (0.00 sec)


    (jlive)[crashcourse]>SET autocommit = 0;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>SHOW VARIABLES LIKE 'autocommit';

    +---------------+-------+

    | Variable_name | Value |

    +---------------+-------+

    | autocommit    | OFF   |

    +---------------+-------+

     

    1 row in set (0.00 sec)

    • 相关阅读:
      如何在Nginx下配置PHP程序环境
      Nginx服务器不支持PATH_INFO的问题及解决办法
      php内置函数分析之str_pad()
      php常用几种设计模式的应用场景
      func_get_args()在php71与php56的区别
      Restful api 防止重复提交
      Game-Tech小游戏专场第二趴,这次帝都见
      入门系列之在Ubuntu上使用MySQL设置远程数据库优化站点性能
      入门系列之在Ubuntu上使用Netdata设置实时性能监控
      叶聪:朋友圈背后的计算机视觉技术与应用
    • 原文地址:https://www.cnblogs.com/lixuebin/p/10814164.html
    Copyright © 2011-2022 走看看