Skip to content

Commit 4bf8fea

Browse files
Create triggers-cursors-procedures-functions.txt
1 parent 85904b7 commit 4bf8fea

File tree

1 file changed

+250
-0
lines changed

1 file changed

+250
-0
lines changed
+250
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
-- !!! CYCLE 6
2+
3+
-- ?? TRIGGER Q1
4+
5+
create table "DELETED_STUDENT_SCHEDULES" (
6+
"ClassID" int,
7+
"StudentID" int,
8+
"ClassStatus" varchar(50),
9+
"Grade" varchar(5),
10+
primary key("ClassID", "StudentID"),
11+
foreign key("ClassStatus") references "STUDENT_CLASS_STATUS"("CategoryID")
12+
);
13+
14+
create function moveDeleted() returns trigger as $$
15+
begin
16+
insert into "DELETED_STUDENT_SCHEDULES" values(old."ClassID", old."StudentID", old."ClassStatus", (old."Grade")::int);
17+
return old;
18+
end;
19+
$$ language plpgsql;
20+
create trigger moveDeleted
21+
before delete on "STUDENT_SCHEDULES"
22+
for each row
23+
execute procedure moveDeleted();
24+
25+
select * from "DELETED_STUDENT_SCHEDULES";
26+
27+
delete from "STUDENT_SCHEDULES" where "ClassID"=3005 and "StudentID"=7004;
28+
29+
select * from "DELETED_STUDENT_SCHEDULES";
30+
31+
-- ??END of Q1
32+
33+
-- ?? Q2
34+
35+
create function stopData() returns trigger as $$
36+
begin
37+
if (tg_op='INSERT' and extract(dow from current_date)!=5) then
38+
raise exception 'Operation only allowed on friday';
39+
elseif ((tg_op='UPDATE' or tg_op='DELETE') and extract(dow from current_date)=0) then
40+
raise exception 'Operation not allowed on sunday';
41+
end if;
42+
return new;
43+
end;
44+
$$ language plpgsql;
45+
create trigger stopData before update or insert or delete on "STUDENTS"
46+
for each row execute procedure stopData();
47+
select * from extract(dow from current_date);
48+
update "STUDENTS" set "StudAreaCode"=543345 where "StudentID"=7002;
49+
50+
-- ?? END
51+
52+
53+
54+
-- ?? Q3
55+
56+
create function stopUpdate() returns trigger as $$
57+
begin
58+
if new."Salary" > old."Salary" then
59+
raise exception 'not allowed';
60+
end if;
61+
return new;
62+
end;
63+
$$ language plpgsql;
64+
create trigger stopUpdate
65+
before update on "STAFF"
66+
for each row
67+
execute procedure stopUpdate();
68+
select "StaffID", "StfFirstName", "StfLastName", "Salary" from "STAFF";
69+
update "STAFF" set "Salary"=20000.00 where "StaffID"=9;
70+
71+
72+
-- ??? END
73+
74+
75+
76+
-- ?? CURSOR Q1
77+
78+
79+
declare city_cursor cursor for select * from "STAFF" where "StfCity"='Liverpool';
80+
begin;
81+
declare city_cursor cursor for select * from "STAFF" where "StfCity"='Liverpool';
82+
fetch 10 from city_cursor;
83+
end;
84+
85+
86+
-- ?? END
87+
88+
89+
-- ?? C2
90+
91+
create function test(input varchar)
92+
returns void
93+
language plpgsql
94+
as $$
95+
declare
96+
tmp record;
97+
dept_cursor cursor(key varchar) for select stf."StfFirstName", stf."StfLastName" from "STAFF" stf, "CATEGORIES" cat, "FACULTY_CATEGORIES" fcat where stf."StaffID"=fcat."StaffID" and fcat."CategoryID"=cat."CategoryID" and cat."DepartmentID" = (select cat."DepartmentID" from "STAFF" stf, "CATEGORIES" cat, "FACULTY_CATEGORIES" fcat where stf."StaffID"=fcat."StaffID" and fcat."CategoryID"= cat."CategoryID" and stf."StfFirstName"=key);
98+
begin
99+
open dept_cursor(input);
100+
loop
101+
fetch from dept_cursor into tmp;
102+
raise notice '% %',tmp."StfFirstName",tmp."StfLastName";
103+
exit when not found;
104+
end loop;
105+
close dept_cursor;
106+
end;
107+
$$;
108+
select test('Rick');
109+
110+
111+
-- ?? END
112+
113+
114+
-- ?? C3
115+
116+
begin;
117+
declare salary_cursor cursor for select "StaffID", "Salary" from "STAFF" order by "Salary" DESC; fetch 2 from salary_cursor;
118+
commit;
119+
120+
-- ?? END
121+
122+
123+
124+
-- ?? P1
125+
126+
create procedure facultySearch(faculty_id int)
127+
language plpgsql
128+
as $$
129+
declare
130+
rec record;
131+
begin
132+
for rec in select f."StaffID",s."StfFirstName",s."StfLastName",s."Salary",c."DepartmentID" from "FACULTY" f, "STAFF" s, "CATEGORIES" c, "FACULTY_CATEGORIES" fcat where f."StaffID"=faculty_id and f."StaffID"=s ."StaffID" and f."StaffID"=fcat."StaffID" and fcat."CategoryID"=c."CategoryID"
133+
loop
134+
raise notice '% % % % %', rec."StaffID", rec."StfFirstName", rec."StfLastName" ,rec."Salary", rec."DepartmentID";
135+
end loop;
136+
end;
137+
$$;
138+
call facultySearch(5001);
139+
140+
141+
142+
143+
-- ?? END
144+
145+
146+
-- ?? P2
147+
148+
149+
create procedure departmentSearch()
150+
language plpgsql
151+
as $$
152+
declare rec record;
153+
begin
154+
for rec in select "DepartmentID" from "CATEGORIES" where "DepartmentID" = (select c."DepartmentID" from "CATEGORIES" c, "FACULTY_CATEGORIES" fcat, "FACULTY" f where f."StaffID"=fcat."StaffID" and fcat."CategoryID"=c."CategoryID" group by c."DepartmentID" having count(f."StaffID")>4)
155+
loop
156+
raise notice '%',rec."DepartmentID";
157+
end loop;
158+
end;
159+
$$;
160+
call departmentSearch();
161+
162+
163+
164+
-- ?? END
165+
166+
-- ?? P3
167+
168+
create procedure salaryHike(faculty_id int)
169+
language plpgsql
170+
as $$
171+
declare
172+
rec interval;
173+
begin
174+
select age(current_date,"JoiningDate") into rec from "STAFF" where "StaffID"=faculty_id;
175+
if rec>interval '2 years' then
176+
update "STAFF" set "Salary" = 1.05*"Salary" where "StaffID"=faculty_id;
177+
else
178+
raise notice 'No increment';
179+
end if;
180+
end;
181+
$$;
182+
call salaryHike(5008);
183+
select "StaffID", "Salary", "JoiningDate" from "STAFF";
184+
185+
-- ?? END
186+
187+
188+
-- ?? P4
189+
190+
select * from "SUBJECTS";
191+
192+
create function deleteSub()
193+
returns void
194+
language plpgsql
195+
as $$
196+
declare
197+
rec record;
198+
begin
199+
delete from "SUBJECTS" where "SubjectID" not in (select sub."SubjectID" from "SUBJECTS" sub ,"CLASSES" cls,
200+
"STUDENT_SCHEDULES" st where sub."SubjectID"=cls."SubjectID" and cls."ClassID"=st."ClassID" group by
201+
sub."SubjectID" having count(st."StudentID")>0);
202+
end;
203+
$$;
204+
select deleteSub();
205+
206+
select * from "SUBJECTS";
207+
208+
-- ?? END
209+
210+
211+
212+
213+
-- ?? P5
214+
215+
create function lessThanAvgSalary()
216+
returns record
217+
language plpgsql
218+
as $$
219+
declare rec record;
220+
begin
221+
select "StfFirstName", "StfLastName" into rec from "STAFF" where "Salary" < (select avg("Salary")/4 from "STAFF");
222+
return rec;
223+
end;
224+
$$;
225+
select "StfFirstName", "StfLastName", "Salary" from "STAFF";
226+
select lessThanAvgSalary();
227+
228+
229+
-- ?? END
230+
231+
232+
-- ?? P6
233+
234+
select "StfFirstName", "StfLastName", "Salary" from "STAFF";
235+
236+
create function updateSalary()
237+
returns void
238+
language plpgsql
239+
as $$
240+
begin
241+
update "STAFF" set "Salary" = 1.25*"Salary" where "Salary"<10000;
242+
end;
243+
$$;
244+
select updateSalary();
245+
246+
select "StfFirstName", "StfLastName", "Salary" from "STAFF";
247+
248+
249+
250+
-- ?? END

0 commit comments

Comments
 (0)