I'm a beginner (actually complete newbie) to SQL transactions, so I may be missing something obvious.
I have this SQL code, that I'm trying to run through phpMyAdmin:
START TRANSACTION;
INSERT INTO `users` VALUES(NULL, 'User A', 'user.a@example.com', '4', 'User A');
INSERT INTO `users` VALUES(NULL, 'User B', 'user.b@example.com', '3', 'User B');
ALTER TABLE `users` CHANGE `level` `level` TINYINT(3) UNSIGNED NOT NULL;
ALTER TABLE `users` CHANGE `number` `number` INT(10) UNSIGNED NOT NULL;
ALTER TABLE `users` ADD COLUMN `number` INT(10) UNSIGNED NOT NULL AFTER `id`;
COMMIT;
As you may see, second ALTER
causes #1054 - Unknown column 'number' in 'users'
error.
But, when it happens in phpMyAdmin, I can see, that first two INSERT
s (or the entire transaction) is not rolled back. The users
table does contain two new records.
What am I missing? phpMyAdmin doesn't support transactions? Or I don't understand, how transactions actually work, and this is pretty normal, that these two INSERT
s are not rolled back in case of error?
Some statements (most notably DDL) in MySQL cause an implicit commit before they are executed and cannot be rolled back - as such this prevents the prior DML changes from being rolled back as well.
Since
ALTER TABLE
is one of the affected statements, the the SQL batch is effectively treated as:The suggested solution is to keep DDL and DML separated. The documentation says: