wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

SQL Transaction and Trigger Quiz

Total questions: 40

Worksheet time: 40mins

Name
Class
Date
1.

Which SQL command is used to start a transaction?

a)

START TRANSACTION

b)

BEGIN

c)

BEGIN TRANSACTION

d)

INITIATE

2.

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;

a)

Performs a rollback

b)

Transfers money from one account to another

c)

Locks the entire table

d)

Fails due to syntax error

3.

Which SQL keyword undoes all changes made during the transaction?

a)

ROLLBACK

b)

COMMIT

c)

UNDO

d)

CANCEL

4.

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;

a)

The first update is committed

b)

Both updates are rolled back

c)

Both updates are committed

d)

Error is ignored

5.

What does this code ensure? BEGIN; UPDATE table1 SET col = 'value'; COMMIT;

a)

Durability only

b)

Isolation only

c)

Atomicity

d)

None of the above

6.

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;

a)

ROLLBACK

b)

BEGIN and COMMIT

c)

SAVEPOINT

d)

LOCK TABLE

7.

Which of the following is used to define a point to which you can rollback later?

a)

CHECKPOINT

b)

SAVEPOINT

c)

MARKPOINT

d)

PAUSEPOINT

8.

What will be the result of this code? BEGIN; SAVEPOINT sp1; UPDATE users SET balance = balance - 200 WHERE id = 1; ROLLBACK TO sp1; COMMIT;

a)

Balance will decrease by 200

b)

Balance will remain the same

c)

Transaction will be canceled

d)

Syntax error

9.

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;

a)

Consistency

b)

Atomicity

c)

Durability

d)

Isolation

10.

What does this command do? ROLLBACK TO SAVEPOINT sp1;

a)

Rolls back to the beginning

b)

Rolls back to the last COMMIT

c)

Rolls back to the savepoint named sp1

d)

Commits the savepoint

11.

Which of the following options shows a correct transaction that ensures both statements execute or none?

a)

UPDATE a SET val = 1; COMMIT; UPDATE b SET val = 2;

b)

BEGIN; UPDATE a SET val = 1; UPDATE b SET val = 2; COMMIT;

c)

UPDATE a SET val = 1; ROLLBACK; UPDATE b SET val = 2;

d)

BEGIN; COMMIT; UPDATE a SET val = 1; UPDATE b SET val = 2;

12.

Which isolation level prevents dirty reads?

a)

READ UNCOMMITTED

b)

READ COMMITTED

c)

REPEATABLE READ

d)

SERIALIZABLE

13.

What will happen if COMMIT is not issued after a transaction? BEGIN TRANSACTION; UPDATE employees SET salary = salary + 1000 WHERE id = 10;

a)

Change is permanent

b)

Change is lost if connection closes

c)

Change is immediately visible

d)

Error occurs

14.

What is the result of executing the following? BEGIN; UPDATE customer SET points = points + 10 WHERE id = 1; ROLLBACK;

a)

Customer's points increase by 10

b)

Transaction ends successfully

c)

No change is made

d)

Error occurs

15.

What is true about COMMIT?

a)

It undoes all changes

b)

It makes changes visible to other users

c)

It saves a rollback point

d)

It creates a backup**

16.

What is the default behavior of most DBMS when executing SQL statements outside a transaction block?

a)

COMMIT automatically

b)

ROLLBACK automatically

c)

Waits for user input

d)

Error is thrown

17.

Which of these statements is true about a transaction?

a)

It is a single SQL statement

b)

It is always committed automatically

c)

It can include multiple SQL statements

d)

It cannot use savepoints

18.

Which of the following represents a consistent transaction?

a)

BEGIN; UPDATE x SET val = val + 1; COMMIT;

b)

UPDATE x SET val = val + 1

c)

BEGIN; UPDATE x SET val = val + 1;

d)

UPDATE x SET val = val + 1; ROLLBACK;

19.

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;

a)

Updates quantity and price

b)

Only updates quantity

c)

No updates are committed

d)

Rolls back everything

20.

Which command releases a savepoint?

a)

COMMIT

b)

RELEASE SAVEPOINT

c)

DROP SAVEPOINT

d)

END SAVEPOINT

21.

What does the following trigger do? CREATE TRIGGER trg_before_insert BEFORE INSERT ON employees FOR EACH ROW SET NEW.hire_date = NOW();

a)

Updates all employees' hire dates

b)

Sets hire_date to current time on insert

c)

Deletes the new row if hire_date is null

d)

Triggers after insertion

22.

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;

a)

AFTER INSERT

b)

BEFORE DELETE

c)

AFTER DELETE

d)

ON UPDATE

23.

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;

a)

Allows salary updates

b)

Prevents any row update

c)

Prevents salary updates only

d)

Updates salary to default

24.

What type of trigger is this? CREATE TRIGGER trg_update_timestamp BEFORE UPDATE ON products FOR EACH ROW SET NEW.updated_at = NOW();

a)

AFTER INSERT

b)

BEFORE UPDATE

c)

BEFORE INSERT

d)

AFTER DELETE

25.

Which keyword ensures the trigger is run for each affected row?

a)

ON EACH

b)

FOR ALL ROWS

c)

PER ROW

d)

FOR EACH ROW

26.

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;

a)

orders

b)

products

c)

order_items

d)

inventory

27.

Identify the error in this trigger. CREATE TRIGGER trg_error_example AFTER INSERT ON users BEGIN UPDATE stats SET count = count + 1; END;

a)

Missing FOR EACH ROW

b)

No table specified

c)

Wrong trigger event

d)

Syntax is perfect

28.

Which of the following can a trigger NOT do?

a)

Modify another table

b)

Call a stored procedure

c)

Return values to client

d)

Use conditional logic

29.

What does OLD refer to? IF OLD.status = 'active' THEN -- some logic END IF;

a)

New inserted data

b)

Column's original value before change

c)

Temporary table

d)

Invalid keyword

30.

What happens if a trigger has a syntax error?

a)

It runs with warning

b)

It auto-corrects

c)

It fails to create

d)

It logs error and continues

31.

Which clause defines the timing of this trigger? CREATE TRIGGER log_change AFTER UPDATE ON logs

a)

UPDATE

b)

AFTER

c)

ON logs

d)

log_change

32.

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();

a)

Refers to the table name

b)

Refers to new column value being inserted

c)

Refers to updated value of another table

d)

Refers to the column name only

33.

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';

a)

NULL will be inserted

b)

Error will occur

c)

'Default' will be inserted

d)

OLD.name will be inserted

34.

Which part specifies the trigger name? CREATE TRIGGER trg_update_log AFTER UPDATE ON table_name ...

a)

CREATE TRIGGER

b)

AFTER UPDATE

c)

trg_update_log

d)

table_name

35.

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;

a)

Logs deletion

b)

Prevents deletion

c)

Allows deletion

d)

Deletes related data

36.

What is the trigger scope here? FOR EACH ROW

a)

All databases

b)

Single column

c)

Each row affected by event

d)

Whole table

37.

Which trigger runs only once per operation, not per row?

a)

FOR EACH ROW trigger

b)

FOR EACH STATEMENT trigger

c)

AFTER EACH ROW trigger

d)

BEFORE EACH COLUMN trigger

38.

Which of these is a valid trigger event?

a)

ON SELECT

b)

AFTER COMMIT

c)

BEFORE INSERT

d)

AFTER CREATE

39.

Which command deletes a trigger?

a)

DROP TRIGGER trigger_name;

b)

REMOVE TRIGGER trigger_name;

c)

DELETE TRIGGER trigger_name;

d)

ERASE TRIGGER trigger_name;

40.

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;

a)

Inserts new sales data

b)

Gives bonus to every employee

c)

Gives bonus to one employee based on emp_id

d)

Prevents insert