Write a trigger to ensure that no employee of age less than 25 can be inserted in the database.

Program Code:-

create table people(name varchar(13),age number);
DESC people;


create or replace trigger tg
before insert or update
of age 
on people
REFERENCING OLD AS o NEW AS n
for each row
when(n.age < 25)
declare
age_out_of_range exception;
begin
raise age_out_of_range;
EXCEPTION
 WHEN age_out_of_range THEN
  Raise_application_error(-20324,'AGE MUST BE ATLEAST 25 YEARS!');
end;


Output when inserting age above 25

insert into people values('one',26);


Output when inserting age below 25:-

insert into people values('one',24);


When we are inputing the data which is smaller than 25 then the program is raising the desired error. And when we are inputing the data which is greater than 25 then the data is sucessfully inserted in the table.

Comments