Professional Documents
Culture Documents
Trigger
Trigger
Trigger
A database trigger is procedural code that is automatically executed in response to certain events on a particular table in a database. Triggers can restrict access to specific data, perform logging, or audit data modifications. Triggers cannot be used to audit data retrieval via SELECT statements There are two classes of triggers, 1. Row triggers - Row triggers define an action for every row of a table, 2. Statement triggers- statement triggers occur only once per INSERT, UPDATE, or DELETE statement.
Each class can be of several types. 1. BEFORE triggers 2. AFTER triggers which identifies the time of execution of the trigger. 3. an "INSTEAD OF trigger" which is a trigger that will execute instead of the triggering statement. There are typically three triggering events that cause triggers to 'fire': 1. INSERT event (as a new record is being inserted into the database). 2. UPDATE event (as a record is being changed). 3. DELETE event (as a record is being deleted). The trigger is used to automate DML condition process.
Major features of database triggers: Do not accept parameters or arguments (but may store affected-data in temporary tables) cannot perform commit or rollback operations because they are part of the triggering SQL statement (only through autonomous transactions) can cause mutating table errors, if they are poorly written.
Trigger Syntax
To create a trigger or drop a trigger, use the CREATE TRIGGER or DROP TRIGGER statement. mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));
Query OK, 0 rows affected (0.03 sec)
mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account FOR EACH ROW SET @sum = @sum + NEW.amount;
Query OK, 0 rows affected (0.06 sec)
Triggers in MySQL
1. 2. 3. MySQL 5.0.2 introduced support for triggers. Some of the triggers INSERT Trigger UPDATE Trigger DELETE Trigger The SQL:2003 standard mandates that triggers give programmers access to record variables by means of a syntax such as REFERENCING NEW AS n. For example, if a trigger is monitoring for changes to a salary column one could write a trigger like the following: CREATE TRIGGER salary_trigger BEFORE UPDATE ON employee_table REFERENCING NEW ROW AS n, OLD ROW AS o FOR EACH ROW IF n.salary <> o.salary THEN -----op-END IF;
Write a database trigger which inserts the value 1000 as the BatchQTY if the Batch_qty is not given ( null). CREATE OR Replace TRIGGER batch-qty-t1 Before insert on stock for each row Begin If new.batch_qyt is null Then New.batch_qty = 1000; Endif end
Write a database trigger on stock which allows a new record to be inserted only if the manu_dt < exp_dt is not six months before the system date.
CREATE OR Replace TRIGGER dt_check-t2 Before insert on stock for each row Begin If (new. manu_dt > new.exp_dt ) or ( new.manu_dt< Add_months ( sysdate, -6) Then RAISE_APPLICATION_ERROR ( -20001, invalid date); Endif end
Deletes every record from the result table if a student record is deleted
CREATE OR Replace TRIGGER del-t6 After delete on student for each row Begin Delete from result where rollno= :old.rollno; end