做一个简单的交易:
START TRANSACTION;
INSERT INTO users_trips VALUES ('1', '1');
INSERT INTO users_trips VALUES ('1', '41'); <-- this fails due to foreign key contraints
COMMIT;
但是,第一条记录仍然存在。我正在使用InnoDB引擎。
mysql> show create table users_trips;
+-------------+--------------------------------------------------------------------------------
| Table | Create Table
+-------------+--------------------------------------------------------------------------------
| users_trips | CREATE TABLE `users_trips` (
`user_id` int(11) NOT NULL DEFAULT '0',
`trip_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`,`trip_id`),
KEY `users_trips_trip_id_fk` (`trip_id`),
CONSTRAINT `users_trips_trip_id_fk` FOREIGN KEY (`trip_id`) REFERENCES `trips` (`trip_id`),
CONSTRAINT `users_trips_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------------+--------------------------------------------------------------------------------
1 row in set (0.00 sec)
为什么不回滚?
感谢
答案 0 :(得分:1)
您正在对您的交易进行COMMIT。即使其中一个事务在提交之前失败,其他事务也将被提交。在这种情况下,您需要发出ROLLBACK。