Complete the code to create a BEFORE INSERT trigger that sets the 'created_at' column to the current timestamp.
CREATE TRIGGER set_created_at BEFORE INSERT ON users FOR EACH ROW SET NEW.created_at = [1];The NOW() function returns the current date and time in MySQL, which is suitable for a timestamp column.
Complete the code to create a BEFORE INSERT trigger that prevents inserting a user with an empty username.
CREATE TRIGGER check_username BEFORE INSERT ON users FOR EACH ROW BEGIN IF NEW.username = [1] THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Username cannot be empty'; END IF; END;
Checking for an empty string '' ensures the username is not blank.
Fix the error in the trigger code that tries to set NEW.status to 'active' before insert.
CREATE TRIGGER activate_user BEFORE INSERT ON users FOR EACH ROW SET NEW.status = [1];String literals in MySQL must be enclosed in single quotes, so 'active' is correct.
Fill both blanks to create a BEFORE INSERT trigger that sets NEW.created_at to current timestamp and NEW.updated_at to NULL.
CREATE TRIGGER set_timestamps BEFORE INSERT ON users FOR EACH ROW BEGIN SET NEW.created_at = [1]; SET NEW.updated_at = [2]; END;
NOW() sets the current timestamp, and NULL sets the updated_at column to no value initially.
Fill all three blanks to create a BEFORE INSERT trigger that sets NEW.created_at to current timestamp, NEW.updated_at to NULL, and prevents inserting if NEW.email is NULL.
CREATE TRIGGER validate_user BEFORE INSERT ON users FOR EACH ROW BEGIN IF NEW.email IS [1] THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Email cannot be NULL'; END IF; SET NEW.created_at = [2]; SET NEW.updated_at = [3]; END;
The trigger checks if NEW.email IS NULL to prevent missing emails, sets created_at to current time with NOW(), and sets updated_at to NULL initially.