PRACTICAL NO 9
AIM: To implement function in GHRCE Bus Reservation Systems
THEORY:
TRIGGER:
A function is compiled and executed every time whenever it is called. A function must return a
value and cannot modify the data received as parameters. For more about functions, please refer
to the article Different types of Functions.
SYNTAX:
TRIGGER:
A trigger is a special type of stored procedure that automatically runs when an event occurs in the
database server. DML triggers run when a user tries to modify data through a data manipulation
language (DML) event. DML events are INSERT, UPDATE, or DELETE statements on a table or
view. These triggers fire when any valid event fires, whether table rows are affected or not.
SYNTAX:
OUTPUTS:
FOR FUNCTION
1. Use function to update fee details of user entered registration number.
create function fees (r_no varchar,amount integer)
returns integer as
$$
update fee
set Fee_Due=Fee_Due-amount
where Reg_No = r_no;
select 1;
$$
language sql;
select fees ('2018ACSC106',5000);
select * from fee;
2. Update entered student’s Branch.
create function branch (r_no varchar, studentb char)
returns integer as
$$
update student1
set Branch=studentb
where Reg_No=r_no;
select 1;
$$
language sql;
select branch ('2018ACSC108','CSE');
select * from student1;
3. Update student’s first name and degree.
create function s_name (r_no varchar,sn char,s_degree char)
returns integer as
$$
update student1
set F_Name=sn
where Reg_No=r_no;
update student1
set Degree=s_degree
where Reg_No=r_no;
select 1;
$$
language sql;
select s_name('2018ACSC110','Rahul','MTech');
select * from student1;
FOR TRIGGER
1. Set a trigger for adding entries in student database.
create TRIGGER stu_log before insert on student1
begin
insert into ins_entry (stu_id,del_time) values (new.Reg_No,datetime('now'));
end;
insert into student1 values
('2018ACSC116','Karan','Warankar','MTech','CSE',2,16,54785,'karan@[Link]');
select * from ins_entry;
2. Set a trigger and show message for wrong entry.
create TRIGGER email_check before insert on student1
begin
select case when new.E_Mail not LIKE('%_@_%._%') then
RAISE (ABORT,'Invalid Email Address')
end;
end;
insert into student1
values('2018ACSC116','Karan','Warankar','MTech','CSE',2,16,54785,'karan@');
select* from student1;