diff --git a/TeamRocket_TRIGGER.sql b/TeamRocket_TRIGGER.sql new file mode 100644 index 0000000000000000000000000000000000000000..30dbcb68367ab433eaf8a4dd1464321942070d1e --- /dev/null +++ b/TeamRocket_TRIGGER.sql @@ -0,0 +1,94 @@ +-- TeamRocket_TRIGGER.sql + +-- Whenever a user provides a tip for a business +-- update that business's num_tips and that users +-- tips_count +CREATE OR REPLACE FUNCTION updateTipCounts() RETURNS trigger AS ' +BEGIN + -- update Users tip_count attr + -- to the count of tips with + -- matching userids + UPDATE Users AS U + SET tip_count = ( + SELECT COUNT(*) + FROM Tips AS T + WHERE T.userID = U.userID + ); + + -- Update the business tables num_tips attr + -- by setting it to the count of tips for that + -- business + -- Step 1 + CREATE TABLE temp ( + businessid CHAR(22) PRIMARY KEY, + tipCount INTEGER + ); + + INSERT INTO temp ( + SELECT T.businessid, COUNT(*) as tipCount + FROM Tips as T ,Business as B + WHERE B.businessid = T.businessid + GROUP BY T.businessid + ); + + -- Step 2 + UPDATE Business AS B + SET num_tips = ( + SELECT tipCount + FROM temp + WHERE temp.businessid = B.businessid + ); + + -- Step 3, delete the temp table + DROP TABLE temp; + RETURN NEW; +END +' LANGUAGE plpgsql; + +CREATE TRIGGER updateTipCounts +AFTER INSERT ON Tips +FOR EACH ROW +EXECUTE PROCEDURE updateTipCounts(); + + +-- Whenever a user checkins at a business +-- update the business's checkins +CREATE OR REPLACE FUNCTION updateBusinessCheckins() RETURNS trigger AS ' +BEGIN + UPDATE Business AS B + SET num_checkins = ( + SELECT COUNT(*) + FROM Checkins AS C + WHERE C.businessid = B.businessid + ); + RETURN NEW; +END +' LANGUAGE plpgsql; + +CREATE TRIGGER updateBusinessCheckins +AFTER INSERT ON Checkins +FOR EACH ROW +EXECUTE PROCEDURE updateBusinessCheckins(); + + +-- Whenever a user likes a tip +-- update the tip's poster (user) +-- total_like count +CREATE OR REPLACE FUNCTION updateTotalLikes() RETURNS trigger AS ' +BEGIN + UPDATE Users AS U + SET total_likes = ( + SELECT SUM(num_likes) + FROM Tips AS T + WHERE T.userid = NEW.userid + GROUP BY userid + ); + RETURN NEW; +END +' LANGUAGE plpgsql; + +CREATE TRIGGER updateTotalLikes +AFTER UPDATE OF num_likes ON Tips +FOR EACH ROW +EXECUTE PROCEDURE updateTotalLikes(); + diff --git a/TeamRocket_UPDATE.sql b/TeamRocket_UPDATE.sql new file mode 100644 index 0000000000000000000000000000000000000000..e9780661564550d1221e920de0a2555ebc238c72 --- /dev/null +++ b/TeamRocket_UPDATE.sql @@ -0,0 +1,73 @@ +-- TeamRocket_UPDATE.sql + +-- Update the business table's num_checkins attr +-- by setting it to the count of checkins for that +-- business +UPDATE Business AS B +SET num_checkins = ( + SELECT COUNT(*) + FROM Checkins AS C + WHERE C.businessid = B.businessid +); + + +-- Update the business table's num_tips attr +-- by setting it to the count of tips for that +-- business + +-- The following function did not work. The current +-- implementation we recieved help from Sakire with. +-- UPDATE Business as B +-- SET num_tips = ( +-- SELECT COUNT(*) +-- FROM Tips AS T +-- WHERE B.businessid = T.businessid +-- ); + +-- Step 1 +CREATE TABLE temp ( + businessid CHAR(22) PRIMARY KEY, + tipCount INTEGER +); + +INSERT INTO temp ( + SELECT T.businessid, COUNT(*) as tipCount + FROM Tips as T ,Business as B + WHERE B.businessid = T.businessid + GROUP BY T.businessid +); + +-- Step 2 +UPDATE Business AS B +SET num_tips = ( + SELECT tipCount + FROM temp + WHERE temp.businessid = B.businessid +); + +-- Step 3, delete the temp table +DROP TABLE temp; + + +-- update the users total_likes attr +-- to the sum of the number of likes +-- from the tips table where userid +-- matches +UPDATE Users AS U +SET total_likes = ( + SELECT SUM(num_likes) + FROM Tips AS T + WHERE T.userID = U.userID + GROUP BY userid +); + + +-- update Users tip_count attr +-- to the count of tips with +-- matching userids +UPDATE Users AS U +SET tip_count = ( + SELECT COUNT(*) + FROM Tips AS T + WHERE T.userID = U.userID +); \ No newline at end of file diff --git a/backup_1.sql b/backup_1.sql index f786314584881757371fd96aaa008502e4f73076..0125c4216e701e0764f89ae5f412704bb9dc4701 100644 Binary files a/backup_1.sql and b/backup_1.sql differ