-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtriggers.sql
57 lines (51 loc) · 1.39 KB
/
triggers.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
-- CREATE or Replace Function updateCityScore()
-- returns trigger as
-- $$
-- begin
-- UPDATE cities
-- SET "cityScore" = (
-- SELECT ROUND(AVG("locationScore"),1)
-- FROM locations
-- WHERE "locationCityID" = NEW."locationCityID"
-- )
-- WHERE "cityID" = NEW."locationCityID";
-- return new;
-- end;
-- $$
-- language plpgsql;
-- CREATE TRIGGER updateCityScore_trigger
-- AFTER UPDATE OF "locationScore" ON locations
-- FOR EACH ROW EXECUTE FUNCTION updateCityScore();
-- CREATE or Replace Function updateLocationScore()
-- returns trigger as
-- $$
-- begin
-- UPDATE locations
-- SET "locationScore" = (
-- SELECT ROUND(AVG("commentScore"),1)
-- FROM comments
-- WHERE "locationID" = NEW."locationID"
-- )
-- WHERE "locationID" = NEW."locationID";
-- return new;
-- end;
-- $$
-- language plpgsql;
-- CREATE TRIGGER updateLocationScore_trigger
-- AFTER INSERT ON comments
-- FOR EACH ROW EXECUTE FUNCTION updateLocationScore();
-- DROP TRIGGER updateLocationScore_trigger ON comments;
-- CREATE or Replace Function updateUserCommentCount()
-- returns trigger as
-- $$
-- begin
-- UPDATE users
-- SET "userCommentCount" = "userCommentCount" + 1
-- WHERE "userID" = NEW."userID";
-- return new;
-- end;
-- $$
-- language plpgsql;
-- CREATE TRIGGER updateUserCommentCount_trigger
-- AFTER INSERT ON comments
-- FOR EACH ROW EXECUTE FUNCTION updateUserCommentCount();