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?

  1. A.

    CREATE TRIGGER trg_before_insert
    BEFORE INSERT ON Employee
    FOR EACH ROW
    SET @x = 1;

  2. B.

    CREATE TABLE trg_before_insert
    BEFORE INSERT ON Employee;

  3. C.

    TRIGGER trg_before_insert
    ON Employee
    BEFORE INSERT;

  4. D.

    CREATE VIEW trg_before_insert
    BEFORE INSERT ON Employee;

  5. 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.

Explore the full course: Niacl Ao It Specialist