In MySQL, which of the following statements correctly creates a trigger that…
2024
In MySQL, which of the following statements correctly creates a trigger that executes automatically before inserting a row into the Employee table?
- A.
CREATE TRIGGER trg_before_insert
BEFORE INSERT ON Employee
FOR EACH ROW
SET @x = 1; - B.
CREATE TABLE trg_before_insert
BEFORE INSERT ON Employee; - C.
TRIGGER trg_before_insert
ON Employee
BEFORE INSERT; - D.
CREATE VIEW trg_before_insert
BEFORE INSERT ON Employee; - E.
CREATE INDEX trg_before_insert
ON Employee(Before_Insert);
Attempted by 5 students.
Show answer & explanation
Correct answer: A
Correct answer: Option 1.
In MySQL, a trigger is created with the CREATE TRIGGER statement. The required clause order is: CREATE TRIGGER trigger_name, then the timing (BEFORE or AFTER), then the event (INSERT, UPDATE or DELETE), then ON table_name, then FOR EACH ROW, and finally the trigger body.
Option 1 follows this order exactly: CREATE TRIGGER trg_before_insert BEFORE INSERT ON Employee FOR EACH ROW SET @x = 1; — so it fires automatically before every row is inserted into the Employee table, which is what the question asks for.
The other options use the wrong DDL command (CREATE TABLE, CREATE VIEW, CREATE INDEX) or omit the mandatory CREATE keyword, so none of them defines a valid BEFORE-INSERT trigger in MySQL.