Introduction
COMMIT saves your changes to the database permanently. ROLLBACK undoes changes you made since the last save.
COMMIT; ROLLBACK;
INSERT INTO accounts (name, balance) VALUES ('Alice', 1000); COMMIT;
UPDATE accounts SET balance = balance - 100 WHERE name = 'Alice'; ROLLBACK;
BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE name = 'Alice'; UPDATE accounts SET balance = balance + 100 WHERE name = 'Bob'; COMMIT;
CREATE TABLE accounts (name VARCHAR(20), balance INT); INSERT INTO accounts VALUES ('Alice', 1000); INSERT INTO accounts VALUES ('Bob', 500); -- Start a transaction UPDATE accounts SET balance = balance - 200 WHERE name = 'Alice'; UPDATE accounts SET balance = balance + 200 WHERE name = 'Bob'; -- Undo the changes ROLLBACK; SELECT * FROM accounts ORDER BY name;
COMMIT command do in a database transaction?BEGIN TRANSACTION;
INSERT INTO users (id, name) VALUES (1, 'Alice');
ROLLBACK;
SELECT * FROM users WHERE id = 1;
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 5;
COMMIT;
ROLLBACK;
1. BEGIN TRANSACTION;
2. INSERT INTO orders VALUES (101, 'Book');
3. INSERT INTO orders VALUES (102, 'Pen');
4. -- If both inserts succeed, then ___;
5. -- If any insert fails, then ___;
Pass the test and this pattern moves unknown