Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

IT214 – Database Management System

LAB – 3

Name : Nilay Ghodasara


ID : 201901169

Q1.

(a). Create a trigger on Table ground_information to check if the Primary key ID already
exists or not before inserting a new record. And send a custom reply instead of an error
message.

CREATE OR REPLACE FUNCTION scm_db.trig_id()


RETURNS trigger
LANGUAGE 'plpgsql'
VOLATILE
COST 100
AS $BODY$
DECLARE
x ground_information.g_id%type;

BEGIN
select g_id into x from ground_information
where g_id=new.g_id;

if(x=new.g_id) then
raise notice 'given g_id already exists';
return null;
else
raise notice 'valid g_id inserted';
return new;
end if;
END;
$BODY$;

create trigger "trigger_insert"


before insert
on ground_information
for each row
execute procedure "scm_db".trig_id()

(b). Create a trigger on Table of membership where you have to check in new inserted
data your new id is null or not . If null than raise custom error message like that “ID
already exist….”.

Declare CREATE OR REPLACE FUNCTION scm_db.trig_membership()


RETURNS trigger
LANGUAGE 'plpgsql'
VOLATILE
COST 100
AS $BODY$
DECLARE
x membership.m_id%type;

BEGIN
select m_id into x from membership
where m_id=new.m_id;

if(x is null) then


raise notice 'ID already exists';

return null;
else
raise notice 'valid m_id inserted';
return new;
end if;
END;
$BODY$;

CREATE TRIGGER trigger_membership


BEFORE INSERT
ON scm_db.membership
FOR EACH ROW
EXECUTE FUNCTION scm_db.trig_membership();

Q2.
(a) Create a function to show names of players which have specific age. You have to use
one argument in function for specific age number. Result should be only names.
CREATE OR REPLACE FUNCTION scm_db.fun_age(IN p_age bigint)
RETURNS TABLE(a character varying)
LANGUAGE 'plpgsql'
VOLATILE
PARALLEL UNSAFE
COST 100 ROWS 1000

AS $BODY$
BEGIN
RETURN QUERY
SELECT p_name FROM scm_db.player_detail where age=p_age;
END;
$BODY$;
(b)
CREATE OR REPLACE FUNCTION scm_db.fun_cricket()
RETURNS TABLE(a coach_details.c_name%type,b coach_details.salary%type)
LANGUAGE 'plpgsql'
VOLATILE
PARALLEL UNSAFE
COST 100 ROWS 1000

AS $BODY$
BEGIN
RETURN QUERY
SELECT c_name,salary FROM coach_details,sports_detail where s_name='Cricket' and
s_id=sports_belong_to;
END;
$BODY$;
Q3.
(a) Create trigger for following situation: Here we recruited new coach for cricket.So for
the related information you have to change in related table with coach_detail table by
using trigger.
CREATE OR REPLACE FUNCTION scm_db.trig_sports()
RETURNS trigger
LANGUAGE 'plpgsql'
VOLATILE
COST 100
AS $BODY$

BEGIN
update sports_detail
set no_of_coach=no_of_coach+1
where s_name='Cricket';
return null;
END;
$BODY$;
CREATE TRIGGER trigger_coach
AFTER INSERT
ON scm_db.coach_details
FOR EACH ROW
EXECUTE FUNCTION scm_db.trig_sports();
(b) Above table creation is for event record save perpose.Here when we want to
organize a one event for any sports with audience possibility for attending the event , so
by using trigger for before insert event records you need to tell us this event is possible
with these audienece possibility or not and custom message should be print for both
situation(possible or not possible)
CREATE OR REPLACE FUNCTION scm_db.trig_ground()
RETURNS trigger
LANGUAGE 'plpgsql'
VOLATILE
COST 100
AS $BODY$
DECLARE
x integer;

BEGIN

select sport_to_belong into x from ground_information


where sport_to_belong=new.sports_to_belong and
audience_capacity>=new.audience_possibility;

if(x is null) then


raise notice 'Not Possible';
return null;

else
raise notice 'Possible';
return new;
end if;
END;
$BODY$;

CREATE TRIGGER trigger_event


BEFORE INSERT
ON scm_db.event_record
FOR EACH ROW
EXECUTE FUNCTION scm_db.trig_ground();

You might also like