Complete the code to create a trigger function that raises an exception if the salary is less than 0.
CREATE FUNCTION check_salary() RETURNS trigger AS $$ BEGIN IF NEW.salary [1] 0 THEN RAISE EXCEPTION 'Salary cannot be negative'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql;
The trigger function checks if the new salary is less than 0 to raise an exception.
Complete the code to create a trigger that calls the check_salary function before insert on employees table.
CREATE TRIGGER salary_check BEFORE INSERT ON employees FOR EACH ROW EXECUTE FUNCTION [1]();The trigger must call the function named check_salary to validate the salary.
Fix the error in the trigger function to correctly check if NEW.age is less than 18.
CREATE FUNCTION check_age() RETURNS trigger AS $$ BEGIN IF NEW.age [1] 18 THEN RAISE EXCEPTION 'Age must be at least 18'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql;
The condition should check if age is less than 18 to raise an exception.
Fill both blanks to create a trigger that fires before update on the users table and calls the check_age function.
CREATE TRIGGER [1] BEFORE [2] ON users FOR EACH ROW EXECUTE FUNCTION check_age();
The trigger name is age_check and it fires before UPDATE on the users table.
Fill all three blanks to create a trigger function that prevents inserting a user with an empty username or NULL email.
CREATE FUNCTION validate_user() RETURNS trigger AS $$ BEGIN IF NEW.username = [1] OR NEW.email IS [2] THEN RAISE EXCEPTION [3]; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql;
The function checks if username is empty string '' or email is NULL, then raises an exception with the message 'Username and email cannot be empty or null'.