-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTD-contribution.sql
121 lines (84 loc) · 2.72 KB
/
TD-contribution.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
## View of communities and there respective amount of members
DROP VIEW IF EXISTS communityMemberAmount;
CREATE VIEW communityMemberAmount as
SELECT communityID, communityName , COUNT(userID) as amountOfMembers
FROM members natural join communities
GROUP BY communityID
ORDER BY amountOfMembers DESC;
SELECT * FROM communityMemberAmount;
## View of PGRating of communities based on tags on their posts
DROP VIEW IF EXISTS postPGRating;
CREATE VIEW postPGRating as
(SELECT postID, max(PGRating) as PGRating
FROM tagged JOIN tags USING(tag)
GROUP BY postID);
SELECT * FROM postPGRating;
DROP VIEW IF EXISTS communityPGRating;
CREATE VIEW communityPGRating as
SELECT communityID, max(PGRating) as PGRating
FROM communities LEFT OUTER JOIN posts USING(communityID)
LEFT OUTER JOIN postPGRating USING (postID)
GROUP BY communityID
ORDER BY communityID;
SELECT * FROM communityPGRating;
## Updating tag to PGRating 13
SET SQL_SAFE_UPDATES = 0;
SELECT * FROM tags;
# look at change in
SELECT * FROM postPGRating;
UPDATE tags SET PGRating =
CASE
WHEN PGRating < 13
THEN 13
ELSE PGRating
END;
## Deleting all users who are younger than 13
CREATE FUNCTION Age (vDate DATE) RETURNS INTEGER
RETURN TIMESTAMPDIFF(YEAR, vDate , CURDATE());
SELECT userID, userName, Age(dateOfBirth)as Age FROM users;
SELECT *
from posts
Where userID = 4 or userID = 6;
DELETE FROM users
WHERE Age(dateOfBirth) < (SELECT min(PGRating) FROM tags);
## Procedure
DROP TABLE IF EXISTS notifications;
CREATE TABLE notifications(
notificationID INT AUTO_INCREMENT,
email VARCHAR(40),
message VARCHAR(400),
PRIMARY KEY (notificationID)
);
DROP PROCEDURE IF EXISTS notifyUser;
DELIMITER //
CREATE PROCEDURE notifyUser
(IN vuserID INT,IN vmessage VARCHAR(600))
BEGIN
DECLARE vemail VARCHAR(40);
SELECT email INTO vemail FROM users
WHERE userID = vuserID;
INSERT INTO notifications (email,message) VALUES (vemail,vmessage);
END //
DELIMITER ;
CALL notifyUser(1, 'hej');
SELECT * FROM notifications;
## trigger
DROP TRIGGER IF EXISTS comment_after_insert;
DELIMITER //
CREATE TRIGGER comment_after_insert
AFTER INSERT ON comments FOR EACH ROW
BEGIN
DECLARE vuserpostID INT;
DECLARE vpostusername VARCHAR(40);
DECLARE vcommentusername VARCHAR(40);
SELECT userID INTO vuserpostID FROM posts
WHERE postID = NEW.postID;
SELECT userName INTO vpostusername FROM posts NATURAL JOIN users
WHERE postID = NEW.postID;
SELECT userName INTO vcommentusername FROM users
WHERE userID = NEW.userID;
CALL notifyUser(New.userID, CONCAT('Hello ', vpostusername, '!!! ', vcommentusername, ' commented this: "', New.commentText, '" on your post!'));
END //
DELIMITER ;
INSERT INTO comments VALUES
(1, 3, current_timestamp(), "You look so pretty xD");