简体   繁体   中英

MySQL start transaction not working with innoDB engine

I have searched for answer with no success. I am trying to understand/learn Transaction. Something is not working. I am doing the following from command line. Here is the scenario:

MySQL [bank]> select * from account;
+---------+---------+---------+------+
| acctnum | name    | balance | id   |
+---------+---------+---------+------+
|     100 | Andy    |    0.00 |   10 |
|     200 | Allison |   50.00 |   20 |
|     300 | Alex    |    0.00 |   30 |
|     400 | Joe     |    0.00 |   40 |
+---------+---------+---------+------+
4 rows in set (0.00 sec)

MySQL [bank]> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)

MySQL [bank]> start transaction;
Query OK, 0 rows affected (0.00 sec)

MySQL [bank]> update account set id=10 where acctnum=200;
ERROR 1062 (23000): Duplicate entry '10' for key 'id'
MySQL [bank]> update account set balance=10 where acctnum=200;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MySQL [bank]> commit;
Query OK, 0 rows affected (0.00 sec)

MySQL [bank]> select * from account;
+---------+---------+---------+------+
| acctnum | name    | balance | id   |
+---------+---------+---------+------+
|     100 | Andy    |    0.00 |   10 |
|     200 | Allison |   10.00 |   20 |
|     300 | Alex    |    0.00 |   30 |
|     400 | Joe     |    0.00 |   40 |
+---------+---------+---------+------+
4 rows in set (0.00 sec)

as you see, I intentionally made the first update wrong and expect the second update not permitted and balance should not have been updated to 10. Any idea or I am missing something? Thanks in advance.

MySQL does not invalidate the transaction if there is an error in one of the statements. The erroneous statement will of course not change the data, but any other statements that do not cause errors will still take effect when you COMMIT .

If you try this on PostgreSQL I believe you will have to ROLLBACK and the second UPDATE statement will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM