MySQL Database Quiz Questions

Course Name:MySQL
Chapter Name:Chapter 9 - Advanced Topics
Lesson Content Link:DB Trigger
Current Quiz Count:30
Progress
0%
Q1
True / False

trigger in MySQL can be created to automatically update a table before an INSERT operation is performed.

Q2
True / False

You can only have one trigger for each type of operation (before/after insert/update/delete) on a single table in MySQL.

Q3
True / False

Triggers in MySQL can be used to enforce data integrity by validating data before it is committed to the database.

Q4
True / False

Triggers in MySQL can directly call other triggers.

Q5
True / False

MySQL, a BEFORE UPDATE trigger can modify the values of the row being updated.

Q6
True / False

Triggers in MySQL can be used to automatically log changes made to a table by inserting records into an audit table.

Q7
True / False

MySQL triggers can only be used to manipulate data within the same table where the trigger is defined.

Q8
True / False

MySQL, you can create triggers that run after an INSERT operation on a view.

Q9
True / False

The OLD and NEW keywords in MySQL triggers refer to the values of the rows before and after an UPDATE operation, respectively

Q10
True / False

MySQL, a trigger created with the BEFORE DELETE timing cannot prevent the deletion of rows from a table.

Q11
Single Choice

What is a MySQL trigger?

Q12
Single Choice

Which of the following events can MySQL triggers be associated with

Q13
Single Choice

What keyword is used to create a MySQL trigger?

Q14
Single Choice

When creating a MySQL trigger, which clause specifies the timing of the trigger?

Q15
Single Choice

Which of the following is not a valid MySQL trigger timing?

Q16
Single Choice

How many triggers can be created on a single table for the same event and timing in MySQL?

Q17
Single Choice

Which of the following is true about row-level triggers in MySQL?

Q18
Single Choice

What are the NEW and OLD keywords used for in MySQL triggers?

Q19
Single Choice

How can you prevent a trigger from causing a loop in MySQL?

Q20
Single Choice

Which MySQL statement would correctly create a trigger that sets the created_at field to the current timestamp whenever a new row is inserted into the users table?

Q21
Multiple Choice

Identify the correct SQL statement to create a trigger that logs changes to a table after an update.

SQL Code
CREATE TRIGGER log_update
AFTER UPDATE ON employees
FOR EACH ROW
INSERT INTO employee_log (employee_id, old_salary, new_salary)
VALUES (OLD.employee_id, OLD.salary, NEW.salary);
Q22
Multiple Choice

Choose the correct SQL statement to create a BEFORE INSERT trigger that checks for duplicate entries.

SQL Code
CREATE TRIGGER prevent_duplicates
BEFORE INSERT ON customers
FOR EACH ROW
BEGIN
IF EXISTS (SELECT 1 FROM customers WHERE email = NEW.email) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Duplicate entry';
END IF;
END;
Q23
Multiple Choice

Determine the correct SQL statement to create a trigger that updates a summary table after a DELETE operation.

SQL Code
CREATE TRIGGER update_summary
AFTER DELETE ON sales
FOR EACH ROW
UPDATE sales_summary
SET total_sales = total_sales - OLD.amount;
Q24
Multiple Choice

Which SQL statement correctly creates a BEFORE DELETE trigger that checks for dependent records?

SQL Code
CREATE TRIGGER check_dependents
BEFORE DELETE ON products
FOR EACH ROW
BEGIN
IF EXISTS (SELECT 1 FROM order_details WHERE product_id = OLD.product_id) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Cannot delete product with orders';
END IF;
END;
Q25
Multiple Choice

Select the correct SQL statement to create an AFTER INSERT trigger that automatically updates a status column.

SQL Code
CREATE TRIGGER update_status
AFTER INSERT ON orders
FOR EACH ROW
UPDATE orders
SET status = 'Processing'
WHERE order_id = NEW.order_id;
Q26
Multiple Choice

Determine the correct SQL statement to create a trigger that prevents deletion of records based on a complex condition.

SQL Code
CREATE TRIGGER prevent_deletion
BEFORE DELETE ON employees
FOR EACH ROW
BEGIN
IF OLD.role = 'Manager' AND OLD.years_of_service > 10 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Cannot delete long-serving managers';
END IF;
END;
Q27
Multiple Choice

Which SQL statement correctly creates a BEFORE UPDATE trigger that ensures data integrity across related tables?

SQL Code
CREATE TRIGGER ensure_integrity
BEFORE UPDATE ON orders
FOR EACH ROW
BEGIN
IF NEW.customer_id <> OLD.customer_id THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Customer ID cannot be changed';
END IF;
END;
Q28
Multiple Choice

Identify the correct SQL statement to create a trigger that logs changes to multiple columns in a table.

SQL Code
CREATE TRIGGER log_changes
AFTER UPDATE ON inventory
FOR EACH ROW
BEGIN
IF OLD.price <> NEW.price THEN
INSERT INTO inventory_log (item_id, old_price, new_price)
VALUES (OLD.item_id, OLD.price, NEW.price);
END IF;
IF OLD.stock <> NEW.stock THEN
INSERT INTO inventory_log (item_id, old_stock, new_stock)
VALUES (OLD.item_id, OLD.stock, NEW.stock);
END IF;
END;
Q29
Multiple Choice

Select the correct SQL statement to create a trigger that synchronizes data between two related tables.

SQL Code
CREATE TRIGGER sync_data
AFTER INSERT ON orders
FOR EACH ROW
INSERT INTO order_summary (order_id, order_total)
VALUES (NEW.order_id, NEW.total);
Q30
Multiple Choice

Determine the correct SQL statement to create a trigger that prevents unauthorized changes to a critical column.

SQL Code
CREATE TRIGGER prevent_unauthorized_change
BEFORE UPDATE ON accounts
FOR EACH ROW
BEGIN
IF NEW.balance <> OLD.balance THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Unauthorized balance change';
END IF;
END;