码迷,mamicode.com
首页 > 其他好文 > 详细

控制事务

时间:2015-05-29 21:40:59      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

SELECT * FROM PRODUCTS_TMP;
+---------+-------------------------+-------+
| PROD_ID | PROD_DESC               | COST  |
+---------+-------------------------+-------+
| 11235   | WITCH COSTUME           | 29.99 |
| 119     | ASSORTED MASKS          |  4.95 |
| 13      | FALSE PARAFFIN TEETH    |  1.10 |
| 15      | ASSORTED COSTUMES       | 10.00 |
| 222     | PLASTIC PUMPKIN 18 INCH |  7.75 |
| 6       | PUMPKIN CANDY           |  1.45 |
| 87      | PLASTIC SPIDERS         |  1.05 |
| 9       | CANDY CORN              |  1.35 |
| 90      | LIGHTED LANTERNS        | 14.50 |
+---------+-------------------------+-------+

START TRANSACTION;
DELETE FROM PRODUCTS_TMP WHERE COST < 14;
COMMIT; SELECT * FROM PRODUCTS_TMP; +---------+------------------+-------+ | PROD_ID | PROD_DESC | COST | +---------+------------------+-------+ | 11235 | WITCH COSTUME | 29.99 | | 90 | LIGHTED LANTERNS | 14.50 | +---------+------------------+-------+

 

START TRANSACTION;

UPDATE PRODUCTS_TMP
SET COST = 39.99
WHERE PROD_ID = 11235;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 39.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
+---------+------------------+-------+

ROLLBACK;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
+---------+------------------+-------+

 

START TRANSACTION;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

SAVEPOINT sp1;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = 11235;

SAVEPOINT sp2;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = 90;

SAVEPOINT sp3;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = 2345;

SELECT * FROM PRODUCTS_TMP;
Empty set (0.00 sec)

ROLLBACK TO sp2;
SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

-- 回滚到SAVEPOINT sp2后,sp3失效(=RELEASE SAVEPOINT sp3;)
ROLLBACK TO sp3;
ERROR 1305 (42000): SAVEPOINT sp3 does not exist

-- ROLLBACK回滚到上一个COMMIT或ROLLBACK语句()。
-- 由于之前没有执行过COMMIT,所以这时执行ROLLBACK会撤销全部删除命令
ROLLBACK;
SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

COMMIT;


START TRANSACTION;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

SAVEPOINT sp1;
DELETE FROM PRODUCTS_TMP WHERE PROD_ID = 11235;
SAVEPOINT sp2;
DELETE FROM PRODUCTS_TMP WHERE PROD_ID = 90;
SAVEPOINT sp3;
DELETE FROM PRODUCTS_TMP WHERE PROD_ID = 2345;

ROLLBACK TO sp3;
SELECT * FROM PRODUCTS_TMP;
+---------+---------------+-------+
| PROD_ID | PROD_DESC     | COST  |
+---------+---------------+-------+
| 2345    | OAK BOOKSHELF | 59.99 |
+---------+---------------+-------+

ROLLBACK;
SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

COMMIT;

 

START TRANSACTION;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

SAVEPOINT sp1;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = 11235;

SAVEPOINT sp2;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = 90;

SAVEPOINT sp3;

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = 2345;

ROLLBACK WORK TO SAVEPOINT sp3;

SELECT * FROM PRODUCTS_TMP;
+---------+---------------+-------+
| PROD_ID | PROD_DESC     | COST  |
+---------+---------------+-------+
| 2345    | OAK BOOKSHELF | 59.99 |
+---------+---------------+-------+

ROLLBACK WORK TO SAVEPOINT sp2;

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

DELETE FROM PRODUCTS_TMP WHERE PROD_ID = 2345;
-- 回滚到指定SAVEPOINT之后,该SAVEPOINT失效(= RELEASE SAVEPOINT sp3;)
ROLLBACK TO sp3;
ERROR 1305 (42000): SAVEPOINT sp3 does not exist

SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 90      | LIGHTED LANTERNS | 14.50 |
+---------+------------------+-------+

-- 回滚到第一个SAVEPOINT,恢复包括sp1/sp2/sp3在内的所有数据
ROLLBACK TO sp1;
SELECT * FROM PRODUCTS_TMP;
+---------+------------------+-------+
| PROD_ID | PROD_DESC        | COST  |
+---------+------------------+-------+
| 11235   | WITCH COSTUME    | 29.99 |
| 90      | LIGHTED LANTERNS | 14.50 |
| 2345    | OAK BOOKSHELF    | 59.99 |
+---------+------------------+-------+

 

控制事务

标签:

原文地址:http://www.cnblogs.com/fatoland/p/4539366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!