0% found this document useful (0 votes)
94 views7 pages

Type 1-Trigger After Update

The document describes different types of triggers created on tables in Oracle SQL. It includes examples of triggers that: 1) Print log messages for insert, update, and delete statements on a table. 2) Perform the same logging before and after changes are made to a table. 3) Validate data and raise errors for invalid values. 4) Automatically update totals in one table when rows are inserted into another table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views7 pages

Type 1-Trigger After Update

The document describes different types of triggers created on tables in Oracle SQL. It includes examples of triggers that: 1) Print log messages for insert, update, and delete statements on a table. 2) Perform the same logging before and after changes are made to a table. 3) Validate data and raise errors for invalid values. 4) Automatically update totals in one table when rows are inserted into another table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL> create table empa(id number(3),name varchar2(10),income number(4),expense

number (3),savings number(3));


Table created.
SQL> insert into empa values(2,'kumar',2500,150,650);
1 row created.
SQL> insert into empa values(3,'venky',5000,900,950);
1 row created.
SQL> insert into empa values(4,'anish',9999,999,999);
1 row created.
SQL> select * from empa;
ID NAME
INCOME EXPENSE SAVINGS
---------- ---------- ---------- ---------- ---------2 kumar
2500
150
650
3 venky
5000
900
950
4 anish
9999
999
999

TYPE 1- TRIGGER AFTER UPDATE


------------------------------------------------SQL> CREATE OR REPLACE TRIGGER CSE
AFTER UPDATE OR INSERT OR DELETE ON EMPA
FOR EACH ROW
BEGIN
IF UPDATING THEN
DBMS_OUTPUT.PUT_LINE('TABLE IS UPDATED');
ELSIF INSERTING THEN
DBMS_OUTPUT.PUT_LINE('TABLE IS INSERTED');
ELSIF DELETING THEN
DBMS_OUTPUT.PUT_LINE('TABLE IS DELETED');
END IF;
END;
/
OUTPUT:
Trigger created.
SQL> set serveroutput on
SQL> update empa set income=900 where name='kumar';
TABLE IS UPDATED
1 row updated.

SQL> insert into empa values(4,'chandru',700,250,80);


TABLE IS INSERTED
1 row created.
SQL> delete from empa where id=4;
TABLE IS DELETED
1 row deleted.
SQL> select * from empa;
ID NAME
INCOME EXPENSE
---------- ---------- ---------- ---------- ---------2 kumar
900
150
650
3 venky
5000
900
950

SAVINGS

TYPE 2- TRIGGER BEFORE UPDATE


-------------------------------------------------

SQL> select * from empl;


ID NAME
INCOME EXPENSE
---------- ---------- ---------- ---------- ---------2 kumar
900
150
650
3 venky
5000
900
950

SAVINGS

SQL>CREATE OR REPLACE TRIGGER UNICS


BEFORE UPDATE OR INSERT OR DELETE ON EMPL
FOR EACH ROW
BEGIN
IF UPDATING THEN
DBMS_OUTPUT.PUT_LINE('TABLE IS UPDATED');
ELSIF INSERTING THEN
DBMS_OUTPUT.PUT_LINE('TABLE IS INSERTED');
ELSIF DELETING THEN
DBMS_OUTPUT.PUT_LINE('TABLE IS DELETED');
END IF;
END;
/

OUTPUT:
Trigger created.
SQL> insert into empl values(4,'MAHESH',700,250,80);
TABLE IS INSERTED
1 row created.
SQL> delete from empl where id=4;
TABLE IS DELETED
1 row deleted.
SQL> update empl set income=9000 where name='venky';
TABLE IS UPDATED
1 row updated.
SQL> select * from empl;
ID NAME
INCOME EXPENSE
---------- ---------- ---------- ---------- ---------2 kumar
900
150
650
3 venky
9000
900
950

SAVINGS

TRIGGER USING MESSAGE ALERT


SQL> create table msg(name char(10),age number(3));
Table created.
SQL> desc msg;
Name
Null? Type
----------------------------------------- -------- ---------------------------NAME
CHAR(10)
AGE
NUMBER(3)
SQL> set serveroutput on;
SQL> create trigger trignew
after insert or update of age on msg
for each row
begin
if(:new.age<0)then
dbms_output.put_line('INVALID AGE');
ELSE
dbms_output.put_line('valid age');
end if;
end;
/

OUTPUT:
Trigger created.
SQL> insert into msg values('abc',15);
valid age
1 row created.
SQL> insert into msg values('xyz',-7);
INVALID AGE
1 row created.
TRIGGER TO RAISE APPROPRIATE ERROR CODE AND ERROR MESSAGE:
SQL> desc data;
Name
Null? Type
----------------------------------------- -------- ---------------------------NAME
CHAR(10)
AGE
NUMBER(3)

SQL> create trigger checkdata


after insert or update of age on data
for each row
begin
if(:new.age<0)then
raise_application_error(-20000,'No Negative Age Allowed');
end if;
end;
/
OUTPUT:
Trigger created.
SQL> insert into data values('mani',10);
1 row created.
SQL> insert into data values('ganesh',-34);
insert into data values('ganesh',-34)
*
ERROR at line 1:
ORA-20000: No Negative Age Allowed
ORA-06512: at "SYSTEM.CHECKDATA", line 3
ORA-04088: error during execution of trigger 'SYSTEM.CHECKDATA'
SQL> select * from data;
NAME
AGE
---------- ---------abc
15
xyz
-7
mani
10

TRIGGER FOR EMP TABLE TO UPDATE ANOTHER TABLE:


SQL> create table srm_emp2(iname varchar2(10),iid number(5),salary number(10));
Table created.
SQL> desc srm_emp2;
Name
Null? Type
----------------------------------------- -------- ---------------------------INAME
IID
SALARY

VARCHAR2(10)
NUMBER(5)
NUMBER(10)

SQL> create table srm_sal2(iname varchar2(10),totalemp number(5),totalsal number(10));


Table created.
SQL> desc srm_sal2;
Name
Null? Type
----------------------------------------- -------- ---------------------------INAME
TOTALEMP
TOTALSAL

VARCHAR2(10)
NUMBER(5)
NUMBER(10)

SQL> create or replace trigger emptrig after insert on srm_emp2


for each row
declare
A varchar2(10);
begin
A:=:NEW.iname;
update srm_sal2 set
totalsal=totalsal+:new.salary,totalemp=totalemp+1 where
iname=A;
end;
/
OUTPUT:
Trigger created.
SQL> insert into srm_sal2 values('vec',0,0);
1 row created.
SQL> insert into srm_sal2 values('srm',0,0);
1 row created.
SQL> select * from srm_sal2;

INAME
TOTALEMP TOTALSAL
---------- ---------- ---------vec
0
0
srm
0
0
SQL> insert into srm_emp2 values('srm',200,3000);
1 row created.
SQL> select * from srm_sal2;
INAME
TOTALEMP TOTALSAL
---------- ---------- ---------vec
0
0
srm
1
3000
SQL> insert into srm_emp2 values('vec',100,1000);
1 row created.
SQL> select * from srm_sal2;
INAME
TOTALEMP TOTALSAL
---------- ---------- ---------vec
1
1000
srm
1
3000
SQL> insert into srm_emp2 values('vec',100,5000);
1 row created.
SQL> select * from srm_sal2;
INAME
TOTALEMP TOTALSAL
---------- ---------- ---------vec
2
6000
srm
1
3000
SQL> insert into srm_emp2 values('vec',100,2000);
1 row created.
SQL> select * from srm_sal2;
INAME
TOTALEMP TOTALSAL
---------- ---------- ---------vec
3
8000
srm
1
3000
SQL> insert into srm_emp2 values('srm',200,8000);
1 row created.

SQL> select * from srm_sal2;


INAME
TOTALEMP TOTALSAL
---------- ---------- ---------vec
3
8000
srm
2 11000

You might also like