-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema.sql
275 lines (258 loc) · 14.4 KB
/
schema.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
-- MySQL dump 10.16 Distrib 10.1.28-MariaDB, for Linux (x86_64)
--
-- Host: localhost Database: stackledge
-- ------------------------------------------------------
-- Server version 10.1.28-MariaDB
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `Category`
--
DROP TABLE IF EXISTS `Category`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Category` (
`name` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL,
`subscriptions` int(11) NOT NULL,
`creation_date` date DEFAULT NULL,
PRIMARY KEY (`name`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `Comment`
--
DROP TABLE IF EXISTS `Comment`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Comment` (
`comment_id` int(11) NOT NULL AUTO_INCREMENT,
`text` varchar(1024) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`submission_time` datetime DEFAULT NULL,
`owner` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`post_id` int(11) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
`upvotes` int(11) NOT NULL DEFAULT '0',
`downvotes` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`comment_id`),
UNIQUE KEY `comment_id` (`comment_id`),
KEY `owner` (`owner`),
KEY `post_id` (`post_id`),
KEY `parent_id` (`parent_id`),
CONSTRAINT `Comment_ibfk_1` FOREIGN KEY (`owner`) REFERENCES `User` (`username`),
CONSTRAINT `Comment_ibfk_2` FOREIGN KEY (`post_id`) REFERENCES `Post` (`post_id`),
CONSTRAINT `Comment_ibfk_3` FOREIGN KEY (`parent_id`) REFERENCES `Comment` (`comment_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 trigger NewComment after insert on Comment for each row
begin
update Post set num_comments = num_comments + 1 where Post.post_id = new.post_id;
end */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;
--
-- Table structure for table `CommentVotes`
--
DROP TABLE IF EXISTS `CommentVotes`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `CommentVotes` (
`username` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`comment_id` int(11) NOT NULL,
`weight` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`username`,`comment_id`),
KEY `comment_id` (`comment_id`),
CONSTRAINT `CommentVotes_ibfk_1` FOREIGN KEY (`username`) REFERENCES `User` (`username`),
CONSTRAINT `CommentVotes_ibfk_2` FOREIGN KEY (`comment_id`) REFERENCES `Comment` (`comment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 trigger InsertCommentVote after insert on CommentVotes for each row begin if(new.weight = 1) then update Comment set upvotes = upvotes + 1 where new.comment_id = Comment.comment_id; elseif(new.weight = -1) then update Comment set downvotes = downvotes +1 where new.comment_id = Comment.comment_id; end if;
end */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 trigger CommentVotesUpdate after update on CommentVotes for each row begin if old.weight <> new.weight then if new.weight = 1 then update Comment set downvotes = downvotes - 1, upvotes = upvotes + 1 where old.comment_id = Comment.comment_id; else update Comment set upvotes = upvotes - 1, downvotes = downvotes + 1 where old.comment_id = Comment.comment_id; end if; end if; end */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 trigger DeleteCommentVote after delete on CommentVotes for each row begin if(old.weight = 1) then update Comment set upvotes = upvotes - 1 where old.comment_id = Comment.comment_id; elseif(old.weight = -1) then update Comment set downvotes = downvotes - 1 where old.comment_id = Comment.comment_id; end if; end */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;
--
-- Table structure for table `Post`
--
DROP TABLE IF EXISTS `Post`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Post` (
`post_id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`submission_time` datetime DEFAULT NULL,
`upvotes` int(11) NOT NULL DEFAULT '0',
`downvotes` int(11) NOT NULL DEFAULT '0',
`owner` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`category` varchar(16) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`title` varchar(256) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`num_comments` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`post_id`),
UNIQUE KEY `post_id` (`post_id`),
KEY `owner` (`owner`),
KEY `category` (`category`),
CONSTRAINT `Post_ibfk_1` FOREIGN KEY (`owner`) REFERENCES `User` (`username`),
CONSTRAINT `Post_ibfk_2` FOREIGN KEY (`category`) REFERENCES `Category` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `PostVotes`
--
DROP TABLE IF EXISTS `PostVotes`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `PostVotes` (
`username` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`post_id` int(11) NOT NULL,
`weight` int(11) DEFAULT NULL,
PRIMARY KEY (`username`,`post_id`),
KEY `post_id` (`post_id`),
CONSTRAINT `PostVotes_ibfk_1` FOREIGN KEY (`username`) REFERENCES `User` (`username`),
CONSTRAINT `PostVotes_ibfk_2` FOREIGN KEY (`post_id`) REFERENCES `Post` (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 trigger UpdateVotesins after insert on PostVotes for each row begin if(new.weight = 1) then update Post set upvotes = upvotes + 1 where new.post_id = Post.post_id; elseif(new.weight = -1) then update Post set downvotes = downvotes +1 where new.post_id = Post.post_id; end if; end */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 trigger PostVotesUpdate after update on PostVotes for each row begin if old.weight <> new.weight then if new.weight = 1 then update Post set downvotes = downvotes - 1, upvotes = upvotes + 1 where old.post_id = Post.post_id; else update Post set upvotes = upvotes - 1, downvotes = downvotes + 1 where old.post_id = Post.post_id; end if; end if; end */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 trigger UpdateVotesdel after delete on PostVotes for each row begin if(old.weight = 1) then update Post set upvotes = upvotes - 1 where old.post_id = Post.post_id; elseif(old.weight = -1) then update Post set downvotes = downvotes - 1 where old.post_id = Post.post_id; end if; end */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;
--
-- Table structure for table `Subscription`
--
DROP TABLE IF EXISTS `Subscription`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Subscription` (
`username` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`category_name` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`username`,`category_name`),
KEY `category_name` (`category_name`),
CONSTRAINT `Subscription_ibfk_1` FOREIGN KEY (`username`) REFERENCES `User` (`username`),
CONSTRAINT `Subscription_ibfk_2` FOREIGN KEY (`category_name`) REFERENCES `Category` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `User`
--
DROP TABLE IF EXISTS `User`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `User` (
`username` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`join_date` date DEFAULT NULL,
`passhash` varchar(256) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`username`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2017-11-12 18:33:27