-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriggers.sql
69 lines (58 loc) · 1.95 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
58
59
60
61
62
63
64
65
66
67
68
69
-- choose database
use registration;
-- Maximum two blogs per day
DELIMITER &&
CREATE DEFINER = `john`@`localhost` TRIGGER Two_Blogs_Per_Day
BEFORE INSERT ON `registration`.`blog` FOR EACH ROW
BEGIN
DECLARE rowcount INT;
SELECT COUNT(*) INTO rowcount
FROM blog
WHERE user_id = NEW.user_id AND date = CURDATE();
IF (rowcount >= 2) THEN
signal sqlstate '45000' set message_text = 'You can not post more than two belogs a day! Please try tomorrow.';
END IF;
END &&
DELIMITER ;
-- No comments allowed to own blog
DELIMITER $$
CREATE DEFINER = `john`@`localhost` TRIGGER No_Comments_Own_Blog
BEFORE INSERT ON `registration`.`comment` FOR EACH ROW
BEGIN
DECLARE rowcount INT;
SELECT COUNT(*) INTO rowcount
FROM blog
WHERE blog_id = NEW.blog_id AND user_id = NEW.user_id;
IF (rowcount > 0) THEN
signal sqlstate '45000' set message_text = 'You can not leave comments to your own blog.';
END IF;
END $$
DELIMITER ;
-- Maximum one comment per blog
DELIMITER $$
CREATE DEFINER = `john`@`localhost` TRIGGER One_Comment_Per_Blog
BEFORE INSERT ON `registration`.`comment` FOR EACH ROW
BEGIN
DECLARE rowcount INT;
SELECT COUNT(*) INTO rowcount
FROM comment
WHERE blog_id = NEW.blog_id AND user_id = new.user_id;
IF (rowcount >= 1) THEN
signal sqlstate '45000' set message_text = 'You can not leave more than one comment per blog.';
END IF;
END $$
DELIMITER ;
-- Maximum three comments per day
DELIMITER $$
CREATE DEFINER = `john`@`localhost` TRIGGER Three_Comments_Per_Day
BEFORE INSERT ON `registration`.`comment` FOR EACH ROW
BEGIN
DECLARE rowcount INT;
SELECT COUNT(*) INTO rowcount
FROM comment
WHERE user_id = NEW.user_id AND date = CURDATE();
IF (rowcount >= 3) THEN
signal sqlstate '45000' set message_text = 'You can not leave more than 3 comments per day! Please try tomorrow.';
END IF;
END $$
DELIMITER ;