NEW
Font size
WorksheetsSQL Transaction and Trigger Quiz
Total questions: 40
Worksheet time: 40mins
Which SQL command is used to start a transaction?
START TRANSACTION
BEGIN
BEGIN TRANSACTION
INITIATE
What does the following SQL snippet do? BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 500 WHERE id = 101; UPDATE accounts SET balance = balance + 500 WHERE id = 102; COMMIT;
Performs a rollback
Transfers money from one account to another
Locks the entire table
Fails due to syntax error
Which SQL keyword undoes all changes made during the transaction?
ROLLBACK
COMMIT
UNDO
CANCEL
Consider this code: What happens if the second update fails? BEGIN TRANSACTION; UPDATE inventory SET stock = stock - 10 WHERE item_id = 5; UPDATE sales SET quantity = quantity + 10 WHERE item_id = 5; COMMIT;
The first update is committed
Both updates are rolled back
Both updates are committed
Error is ignored
What does this code ensure? BEGIN; UPDATE table1 SET col = 'value'; COMMIT;
Durability only
Isolation only
Atomicity
None of the above
What is missing to make this transaction atomic? UPDATE orders SET status = 'shipped' WHERE id = 123; UPDATE inventory SET stock = stock - 1 WHERE product_id = 456;
ROLLBACK
BEGIN and COMMIT
SAVEPOINT
LOCK TABLE
Which of the following is used to define a point to which you can rollback later?
CHECKPOINT
SAVEPOINT
MARKPOINT
PAUSEPOINT
What will be the result of this code? BEGIN; SAVEPOINT sp1; UPDATE users SET balance = balance - 200 WHERE id = 1; ROLLBACK TO sp1; COMMIT;
Balance will decrease by 200
Balance will remain the same
Transaction will be canceled
Syntax error
Which property of transaction does this SQL snippet violate if not executed in a transaction block? UPDATE bank SET balance = balance - 100 WHERE id = 1; UPDATE bank SET balance = balance + 100 WHERE id = 2;
Consistency
Atomicity
Durability
Isolation
What does this command do? ROLLBACK TO SAVEPOINT sp1;
Rolls back to the beginning
Rolls back to the last COMMIT
Rolls back to the savepoint named sp1
Commits the savepoint
Which of the following options shows a correct transaction that ensures both statements execute or none?
UPDATE a SET val = 1; COMMIT; UPDATE b SET val = 2;
BEGIN; UPDATE a SET val = 1; UPDATE b SET val = 2; COMMIT;
UPDATE a SET val = 1; ROLLBACK; UPDATE b SET val = 2;
BEGIN; COMMIT; UPDATE a SET val = 1; UPDATE b SET val = 2;
Which isolation level prevents dirty reads?
READ UNCOMMITTED
READ COMMITTED
REPEATABLE READ
SERIALIZABLE
What will happen if COMMIT is not issued after a transaction? BEGIN TRANSACTION; UPDATE employees SET salary = salary + 1000 WHERE id = 10;
Change is permanent
Change is lost if connection closes
Change is immediately visible
Error occurs
What is the result of executing the following? BEGIN; UPDATE customer SET points = points + 10 WHERE id = 1; ROLLBACK;
Customer's points increase by 10
Transaction ends successfully
No change is made
Error occurs
What is true about COMMIT?
It undoes all changes
It makes changes visible to other users
It saves a rollback point
It creates a backup**
What is the default behavior of most DBMS when executing SQL statements outside a transaction block?
COMMIT automatically
ROLLBACK automatically
Waits for user input
Error is thrown
Which of these statements is true about a transaction?
It is a single SQL statement
It is always committed automatically
It can include multiple SQL statements
It cannot use savepoints
Which of the following represents a consistent transaction?
BEGIN; UPDATE x SET val = val + 1; COMMIT;
UPDATE x SET val = val + 1
BEGIN; UPDATE x SET val = val + 1;
UPDATE x SET val = val + 1; ROLLBACK;
What will this do? BEGIN; UPDATE items SET quantity = quantity - 5 WHERE id = 10; SAVEPOINT before_discount; UPDATE items SET price = price * 0.9 WHERE id = 10; ROLLBACK TO before_discount; COMMIT;
Updates quantity and price
Only updates quantity
No updates are committed
Rolls back everything
Which command releases a savepoint?
COMMIT
RELEASE SAVEPOINT
DROP SAVEPOINT
END SAVEPOINT
What does the following trigger do? CREATE TRIGGER trg_before_insert BEFORE INSERT ON employees FOR EACH ROW SET NEW.hire_date = NOW();
Updates all employees' hire dates
Sets hire_date to current time on insert
Deletes the new row if hire_date is null
Triggers after insertion
Which event is used in this trigger? CREATE TRIGGER trg_after_delete AFTER DELETE ON orders FOR EACH ROW BEGIN INSERT INTO audit_log(action) VALUES ('Order deleted'); END;
AFTER INSERT
BEFORE DELETE
AFTER DELETE
ON UPDATE
What will be the effect of this trigger? CREATE TRIGGER trg_prevent_salary_update BEFORE UPDATE ON employees FOR EACH ROW BEGIN IF OLD.salary <> NEW.salary THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Salary update not allowed'; END IF; END;
Allows salary updates
Prevents any row update
Prevents salary updates only
Updates salary to default
What type of trigger is this? CREATE TRIGGER trg_update_timestamp BEFORE UPDATE ON products FOR EACH ROW SET NEW.updated_at = NOW();
AFTER INSERT
BEFORE UPDATE
BEFORE INSERT
AFTER DELETE
Which keyword ensures the trigger is run for each affected row?
ON EACH
FOR ALL ROWS
PER ROW
FOR EACH ROW
Which table does the trigger below affect? CREATE TRIGGER trg_check_quantity BEFORE INSERT ON order_items FOR EACH ROW BEGIN IF NEW.quantity > 100 THEN SET NEW.quantity = 100; END IF; END;
orders
products
order_items
inventory
Identify the error in this trigger. CREATE TRIGGER trg_error_example AFTER INSERT ON users BEGIN UPDATE stats SET count = count + 1; END;
Missing FOR EACH ROW
No table specified
Wrong trigger event
Syntax is perfect
Which of the following can a trigger NOT do?
Modify another table
Call a stored procedure
Return values to client
Use conditional logic
What does OLD refer to? IF OLD.status = 'active' THEN -- some logic END IF;
New inserted data
Column's original value before change
Temporary table
Invalid keyword
What happens if a trigger has a syntax error?
It runs with warning
It auto-corrects
It fails to create
It logs error and continues
Which clause defines the timing of this trigger? CREATE TRIGGER log_change AFTER UPDATE ON logs
UPDATE
AFTER
ON logs
log_change
What is the purpose of NEW in this trigger? CREATE TRIGGER trg_before_insert_customer BEFORE INSERT ON customers FOR EACH ROW SET NEW.created_at = NOW();
Refers to the table name
Refers to new column value being inserted
Refers to updated value of another table
Refers to the column name only
What happens if you try to insert NULL for NOT NULL field but use trigger to set value? CREATE TRIGGER set_default BEFORE INSERT ON test FOR EACH ROW SET NEW.name = 'Default';
NULL will be inserted
Error will occur
'Default' will be inserted
OLD.name will be inserted
Which part specifies the trigger name? CREATE TRIGGER trg_update_log AFTER UPDATE ON table_name ...
CREATE TRIGGER
AFTER UPDATE
trg_update_log
table_name
What will this trigger do? CREATE TRIGGER prevent_delete BEFORE DELETE ON employees FOR EACH ROW BEGIN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Cannot delete employee'; END;
Logs deletion
Prevents deletion
Allows deletion
Deletes related data
What is the trigger scope here? FOR EACH ROW
All databases
Single column
Each row affected by event
Whole table
Which trigger runs only once per operation, not per row?
FOR EACH ROW trigger
FOR EACH STATEMENT trigger
AFTER EACH ROW trigger
BEFORE EACH COLUMN trigger
Which of these is a valid trigger event?
ON SELECT
AFTER COMMIT
BEFORE INSERT
AFTER CREATE
Which command deletes a trigger?
DROP TRIGGER trigger_name;
REMOVE TRIGGER trigger_name;
DELETE TRIGGER trigger_name;
ERASE TRIGGER trigger_name;
What does this trigger do? CREATE TRIGGER trg_bonus AFTER INSERT ON sales FOR EACH ROW BEGIN UPDATE employees SET bonus = bonus + 100 WHERE id = NEW.emp_id; END;
Inserts new sales data
Gives bonus to every employee
Gives bonus to one employee based on emp_id
Prevents insert
