我一直试图了解MySQL事务几天。我写了一小段执行交易的代码。我的事务包含一系列更新语句,如下所示:
START TRANSACTION;
UPDATE ORDERS
SET AMOUNT =1 WHERE AMOUNT > 5000;
UPDE ORDERS
SET AMOUNT =2 WHERE AMOUNT > 5000 AND AMOUNT < 10000;
UPDATE ORDERS
SET AMOUNT =3 WHERE AMOUNT > 10000 AND AMOUNT < 15000;
UPDATE ORDERS
SET AMOUNT =4 WHERE AMOUNT > 15000;
COMMIT;
注意我故意在第二个查询中拼错UPDATE以测试事务的原子属性。它确实有效。但是当我用一系列INSERT语句替换这些更新语句时,其中一个语句拼写错误,我发现拼写错误之前的所有插入语句都已提交。 所以我想知道事务只适用于UPDATE或任何DML语句吗?如果它支持所有DML语句,你能告诉我为什么它的insert语句失败了吗?我使用支持交易的InnoDB存储引擎。
这是我的插入语句事务代码:(注意第5次INSERT故意拼写错误)
START TRANSACTION;
INSERT INTO ORDERS VALUES (1111,2007-12-17,2099,999,'ABC','ADI11',987,88888);
INSERT INTO ORDERS VALUES (1112,2007-12-17,2099,999,'ABC','MHXYZ22',987,88888);
INSERT INTO ORDERS VALUES (1113,2007-12-17,2099,999,'ABC','MHXYZ33',987,88888);
INSERT INTO ORDERS VALUES (1114,2007-12-17,2099,999,'ABC','MHXYZ44',987,88888);
ISERT INTO ORDERS VALUES (1115,2007-12-17,2099,999,'ABC','MHXYZ55',987,88888);
INSERT INTO ORDERS VALUES (1116,2007-12-17,2099,999,'ABC','MHXYZ66',987,88888);
INSERT INTO ORDERS VALUES (1117,2007-12-17,2099,999,'ABC','MHXYZ77',987,88888);
INSERT INTO ORDERS VALUES (1118,2007-12-17,2099,999,'ABC','MHXYZ88',987,88888);
INSERT INTO ORDERS VALUES (1119,2007-12-17,2099,999,'ABC','MHXYZ99',987,88888);
INSERT INTO ORDERS VALUES (1120,2007-12-17,2099,999,'ABC','MHXYZ1111',987,88888);
INSERT INTO ORDERS VALUES (1121,2007-12-17,2099,999,'ABC','MHXYZ2222',987,88888);
COMMIT;