星空网 > 软件开发 > 数据库

MySQL transaction

MySQL事务操作

 

  1. 事务rollback

    mysql> start transaction;

    Query OK, 0 rows affected (0.00 sec)

     

    mysql> insert into tbtest(stu_ID,stu_Name) values(201,'stu_1');

    Query OK, 1 row affected (0.04 sec)

     

    mysql> select * from tbtest;

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

    | stu_ID | stu_Name | stu_test |

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

    | 123 | xiaoMing | NULL |

    | 124 | xiaoZhang | NULL |

    | 125 | xiaoLi | NULL |

    | 201 | stu_1 | NULL |

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

    4 rows in set (0.00 sec)

     

    mysql> rollback;

    Query OK, 0 rows affected (0.06 sec)

     

    mysql> select * from tbtest;

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

    | stu_ID | stu_Name | stu_test |

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

    | 123 | xiaoMing | NULL |

    | 124 | xiaoZhang | NULL |

    | 125 | xiaoLi | NULL |

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

    3 rows in set (0.00 sec)

     

    mysql> rollback;

    Query OK, 0 rows affected (0.00 sec)

     

  2. 事务commit

    mysql> start transaction;

    Query OK, 0 rows affected (0.00 sec)

     

    mysql> insert into tbtest(stu_ID,stu_Name) values(201,'stu_1');

    Query OK, 1 row affected (0.00 sec)

     

    mysql> select * from tbtest;

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

    | stu_ID | stu_Name | stu_test |

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

    | 123 | xiaoMing | NULL |

    | 124 | xiaoZhang | NULL |

    | 125 | xiaoLi | NULL |

    | 201 | stu_1 | NULL |

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

    4 rows in set (0.00 sec)

     

    mysql> commit;

    Query OK, 0 rows affected (0.02 sec)

     

    mysql> select * from tbtest;

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

    | stu_ID | stu_Name | stu_test |

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

    | 123 | xiaoMing | NULL |

    | 124 | xiaoZhang | NULL |

    | 125 | xiaoLi | NULL |

    | 201 | stu_1 | NULL |

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

    4 rows in set (0.00 sec)

     

  3. 计算上一条语句影响了多少行的两个相关函数

 

MySQL 有两个函数来计算上一条语句影响了多少行

  1. found_rows()

判断Select得到的行数用 found_rows();

 

mysql> select * from tbtest;

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

| stu_ID | stu_Name | stu_test |

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

| 123 | xiaoMing | NULL |

| 124 | xiaoZhang | NULL |

| 125 | xiaoLi | NULL |

| 201 | stu_1 | NULL |

| 202 | stu_2 | NULL |

| 203 | stu_3 | NULL |

| 204 | stu_4 | NULL |

| 205 | stu_4 | NULL |

| 206 | stu_4 | NULL |

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

9 rows in set (0.00 sec)

 

mysql> select found_rows();

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

| found_rows() |

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

| 9 |

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

1 row in set (0.00 sec)

 

  1. row_count()

判断Update Delete影响的行数用row_count(); 注:MySQL中Update前后的值如果一样,row_count()为0,不像在SqlServer中的@@rowcount/Oracle中的rowcount,只要update到行,影响的行数就会大于0(无论前后字段的值是否发生了变化)

 

mysql> insert into tbtest(stu_id, stu_name) values(207, 'stu_46');

Query OK, 1 row affected (0.02 sec)

 

mysql> select row_count();

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

| row_count() |

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

| 1 |

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

1 row in set (0.00 sec)




原标题:MySQL transaction

关键词:MYSQL

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

shopee客单价:https://www.goluckyvip.com/tag/7788.html
shopee移动端:https://www.goluckyvip.com/tag/7789.html
STAR:https://www.goluckyvip.com/tag/779.html
虾叔说Shopee:https://www.goluckyvip.com/tag/7790.html
shopee开车:https://www.goluckyvip.com/tag/7791.html
藏龙荟跨境电:https://www.goluckyvip.com/tag/7792.html
咸阳机场到临潼景点 西安咸阳机场到临潼有多远:https://www.vstour.cn/a/411236.html
合肥有什么约会的地方好玩的地方 合肥哪里适合情侣好玩的地方:https://www.vstour.cn/a/411237.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流