Академический Документы
Профессиональный Документы
Культура Документы
it is a database object.
stored permenently in the database
it just like a procedure.
is named pl/sql block
it is reusable pl/sql programe
cann't be called on its own
it is database event(dml/ddl)
ex:
Inserting Trigger
This trigger execute BEFORE to convert job
field lowercase to uppercase.
7 rows selected.
1 row created.
8 rows selected.
2) CREATE or REPLACE TRIGGER trg5
AFTER
DELETE ON employee
FOR EACH ROW
BEGIN
IF :old.empid = 1 THEN
raise_application_error(-20015, 'You can't delete this row');
END IF;
END;
/
SQL>delete from employee where empid = 1;
Error Code: 20015
Error Name: You can't delete this row
RESTRICTIONS ON TRIGGERES
Like packages, triggers must be stored as stand-alone objects in the database and
cannot be local to a block or package.
A trigger does not accept arguments.
USE OF TRIGGERS
Maintaining complex integrity constraints not possible through declarative
constraints enable at table creation.
Auditing information in a table by recording the changes made and who made them.
Automatically signaling other programs that action needs to take place when chages
are made to a table.
Perform validation on changes being made to tables.
Automate maintenance of the database.
ex:
SQL> create table test(col1 date);
DML TRIGGERS:
NO NAME MARKS
----- ------- ----------
1 a 100
2 b 200
3 c 300
4 d 400
no rows selected
1 row created.
FIRING_ORDER
--------------------------------------------------
Before Statement Level
Before Row Level
After Row Level
After Statement Level
NO NAME MARKS
---- -------- ----------
1 a 100
2 b 200
3 c 300
4 d 400
5 e 500
Output:
NO NAME MARKS
----- ------- ----------
1 a 100
2 b 200
3 c 300
4 d 400
5 e 500
no rows selected
1 row created.
4 d 400
5 e 500
6 f 600
NO OLD_MARKS NEW_MARKS
---- --------------- ---------------
600
1 row updated.
NO NAME MARKS
----- ------- ----------
1 a 100
2 b 200
3 c 300
4 d 400
5 e 555
6 f 600
NO OLD_MARKS NEW_MARKS
------ ---------------- ---------------
600
5 500 555
SQL> delete student where no = 2;
1 row deleted.
SQL> select * from student;
NO NAME MARKS
---- -------- ----------
1 a 100
3 c 300
4 d 400
5 e 555
6 f 600
NO OLD_MARKS NEW_MARKS
----- -------------- ----------------
600
5 500 555
2 200