Professional Documents
Culture Documents
IT214 - Database Management System Lab - 3: Name: Nilay Ghodasara ID: 201901169
IT214 - Database Management System Lab - 3: Name: Nilay Ghodasara ID: 201901169
LAB – 3
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.
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$;
(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….”.
BEGIN
select m_id into x from membership
where m_id=new.m_id;
return null;
else
raise notice 'valid m_id inserted';
return new;
end if;
END;
$BODY$;
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
else
raise notice 'Possible';
return new;
end if;
END;
$BODY$;