95.6k views
5 votes
How do you implement a trigger that ensures that salary in an employee table is not less than zero?

A. Use a CHECK constraint on the salary column.
B. Use a stored procedure to validate salary before updating.
C. Create a BEFORE UPDATE trigger to check the salary value.
D. Set the salary column as NOT NULL.

1 Answer

2 votes

Final answer:

You can implement a BEFORE UPDATE trigger to check the salary value and prevent it from being less than zero.

Step-by-step explanation:

To implement a trigger that ensures the salary in an employee table is not less than zero, you can use a BEFORE UPDATE trigger to check the salary value. In the trigger, you can create a conditional statement that checks if the new salary value is less than zero. If it is, you can raise an error or rollback the transaction to prevent the update.

Here's an example of how the trigger can be implemented in SQL:

CREATE TRIGGER salary_check BEFORE UPDATE ON employee
FOR EACH ROW
BEGIN
IF NEW.salary < 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Salary cannot be less than zero';
END IF;
END;
User Kendrick Taylor
by
7.9k points