0% found this document useful (0 votes)
31 views4 pages

EX 10 Trigger

The document describes creating a trigger in Oracle SQL to validate voter data upon insert or update. The trigger checks that any new or updated age value is not less than 18 years old, and raises an error if so. Sample data is inserted into the voters table. The trigger is then created to run after insert or update of the age field, and checks the new age value for each row. Testing confirms the trigger works as intended by raising an error for an attempted insert of an underage voter.

Uploaded by

Aparna Aparna
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)
31 views4 pages

EX 10 Trigger

The document describes creating a trigger in Oracle SQL to validate voter data upon insert or update. The trigger checks that any new or updated age value is not less than 18 years old, and raises an error if so. Sample data is inserted into the voters table. The trigger is then created to run after insert or update of the age field, and checks the new age value for each row. Testing confirms the trigger works as intended by raising an error for an attempted insert of an underage voter.

Uploaded by

Aparna Aparna
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/ 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