-
Notifications
You must be signed in to change notification settings - Fork 5
/
cavern.sql
104 lines (86 loc) · 3.77 KB
/
cavern.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
SET NAMES utf8;
SET time_zone = '+08:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
SET NAMES utf8mb4;
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`pid` int(11) unsigned NOT NULL,
`content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`username` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`time` datetime NOT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `pid` (`pid`),
KEY `username` (`username`),
CONSTRAINT `comment_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `post` (`pid`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `comment_ibfk_3` FOREIGN KEY (`username`) REFERENCES `user` (`username`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DELIMITER ;;
CREATE TRIGGER `comment_add` AFTER INSERT ON `comment` FOR EACH ROW
BEGIN
UPDATE `post` SET `comment` = `comment` +1 WHERE `pid` = NEW.pid;
END;;
CREATE TRIGGER `comment_del` AFTER DELETE ON `comment` FOR EACH ROW
BEGIN
UPDATE `post` SET `comment` = `comment` -1 WHERE `pid` = OLD.pid;
END;;
DELIMITER ;
DROP TABLE IF EXISTS `like`;
CREATE TABLE `like` (
`username` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`pid` int(11) unsigned NOT NULL,
KEY `pid` (`pid`),
KEY `username` (`username`),
CONSTRAINT `like_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `post` (`pid`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `like_ibfk_2` FOREIGN KEY (`username`) REFERENCES `user` (`username`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DELIMITER ;;
CREATE TRIGGER `like_add` AFTER INSERT ON `like` FOR EACH ROW
BEGIN
UPDATE `post` SET `like` = `like` +1 WHERE `pid` = NEW.pid;
END;;
CREATE TRIGGER `like_del` AFTER DELETE ON `like` FOR EACH ROW
BEGIN
UPDATE `post` SET `like` = `like` -1 WHERE `pid` = OLD.pid;
END;;
DELIMITER ;
DROP TABLE IF EXISTS `notification`;
CREATE TABLE `notification` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`message` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`type` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`time` datetime NOT NULL,
`read` tinyint(1) NOT NULL DEFAULT '0',
`url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `username` (`username`),
CONSTRAINT `notification_ibfk_1` FOREIGN KEY (`username`) REFERENCES `user` (`username`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DROP TABLE IF EXISTS `post`;
CREATE TABLE `post` (
`pid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` text COLLATE utf8mb4_unicode_ci NOT NULL,
`content` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`time` datetime NOT NULL,
`username` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`like` int(11) unsigned NOT NULL DEFAULT '0',
`comment` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`pid`),
KEY `username` (`username`),
CONSTRAINT `post_ibfk_1` FOREIGN KEY (`username`) REFERENCES `user` (`username`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`muted` tinyint(1) NOT NULL DEFAULT '0',
`level` tinyint(1) NOT NULL DEFAULT '0',
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`pwd` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;