You are on page 1of 4

EX NO:10

TRIGGER PAGE NO:


DATE:

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:

SQL> create table voters(voteridvarchar(15),name varchar(25),age number(3),addr


essvarchar(100),district varchar(25),consitiuencyvarchar(25));
Table created.

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;

VOTERID NAME AGE

ADDRESS

DISTRICT CONSITIUENCY
UVX123 bala 44
15715 south street,chatrapatti
virudhunagarsattur

UVX125 sri 19
168/5 southstreet,chatrapatti
virudhunagarsattur

VOTERID NAME AGE

ADDRESS

DISTRICT CONSITIUENCY

UVX135 devi 19
168/5 southstreet,rajapalayam
virudhunagarsattur

UVX137 lavi 19
180/5 southstreet,rajapalayam

VOTERID NAME AGE

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.

You might also like