Professional Documents
Culture Documents
EX 10 Trigger
EX 10 Trigger
AIM:
To write trigger program to maintain the voters details.
PROCEDURE:
A database trigger is stared procedure that is fixed when an insert,update,or delete statement is issued
against the associated table.
Syntax for creating triggers:
Create or replace trigger trigger_name
(before/after)[insert/update/delete]on
Table=name[for each statement/for each row]
[when condition];
Trigger creation:
Creating table:
Inserting records:
SQL> insert into voters values('UVX123','bala',44,'15715 south street,chatrapatt
i','virudhunagar','sattur')
1 row created.
SQL> insert into voters values('UVX123','bala',44,'15715 south street,chatrapatt
i','virudhunagar','sattur');
1 row created.
SQL> insert into voters values('UVX125','sri',19,'168/5 southstreet,chatrapatti'
,'virudhunagar','sattur');
1 row created.
SQL> insert into voters values('UVX135','devi',19,'168/5 southstreet,rajapalayam
','virudhunagar','sattur');
1 row created.
SQL> insert into voters values('UVX137','lavi',19,'180/5 southstreet,rajapalayam
','virudhunagar','sattur');
1 row created.
Display all records:
SQL> select * from voters;
ADDRESS
DISTRICT CONSITIUENCY
UVX123 bala 44
15715 south street,chatrapatti
virudhunagarsattur
UVX125 sri 19
168/5 southstreet,chatrapatti
virudhunagarsattur
ADDRESS
DISTRICT CONSITIUENCY
UVX135 devi 19
168/5 southstreet,rajapalayam
virudhunagarsattur
UVX137 lavi 19
180/5 southstreet,rajapalayam
ADDRESS
DISTRICT CONSITIUENCY
virudhunagarsattur
Trigger creation:
create or replace trigger triginsert after insert or update of age on voters
for each row
declare
begin
if inserting then
if :new.age<18 then
raise_application_error(-20003,'age should not be lessthan 18');
end if;
elsif updating then
dbms_output.put_line('updating...');
if :new.age<18 then
raise_application_error(-20003,'age should not be lessthan18');
end if;
end if;
end;
Trigger created.
OUTPUT:
SQL>insert into voters values(‘UVX124’,’Suresh’,’167/5 middle
street,Rajapalayam’,’Virudhunagar’,’Rajapalayam’);
Error at line1:
ORA:Age should not be less than 18
ORA:at”system.Triginsert”,line5
ORA:error during execution of trigger ‘system.Triginsert’.
RESULT:
Thus the PL/SQL program for trigger is successfully executed.