Triggers in Oracle
A trigger is a piece of code that is automatically executed in response to a certain event, such as an insert, update, or delete operation on a table. Triggers are used to enforce business rules and maintain data integrity. Here is an example:
CREATE OR REPLACE TRIGGER emp_salary_trigger
BEFORE INSERT OR UPDATE ON employees
FOR EACH ROW
BEGIN
IF :NEW.salary < 0 THEN
raise_application_error(-20001, 'Salary cannot be negative');
END IF;
END;
This creates a trigger named "emp_salary_trigger" that is executed before an insert or update operation on the "employees" table. If the new salary value is less than 0, an error is raised.
No comments:
Post a Comment