-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathforumdb.sql
485 lines (398 loc) · 12.4 KB
/
forumdb.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
CREATE DATABASE test;
CREATE TABLE users (
user_id BIGSERIAL NOT NULL PRIMARY KEY,
username VARCHAR(50) NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(150) NOT NULL,
password CHAR(60) NOT NULL,
dob DATE NOT NULL,
profile_image_name text
UNIQUE(email),
UNIQUE (username)
);
CREATE TABLE user_about(
about TEXT,
user_id BIGINT REFERENCES users(user_id),
);
CREATE TABLE user_interest (
interest TEXT,
user_id BIGINT REFERENCES users(user_id)
);
CREATE TABLE user_qualification (
qualification TEXT,
user_id BIGINT REFERENCES users(user_id)
);
CREATE TABLE feedback (
feedback_id BIGSERIAL NOT NULL PRIMARY KEY,
content TEXT NOT NULL,
time_of_feedback TIMESTAMP,
user_id BIGINT REFERENCES users(user_id)
);
CREATE TABLE subforum (
subforum_id BIGSERIAL NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
time_of_creation TIMESTAMP,
creator_id BIGINT REFERENCES users(user_id),
UNIQUE(name)
);
CREATE TABLE community (
community_id BIGSERIAL NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
time_of_creation TIMESTAMP,
creator_id BIGINT REFERENCES users(user_id),
UNIQUE(name)
);
--to display followed subforums
CREATE TABLE user_subforum (
user_id BIGINT REFERENCES users(user_id),
subforum_id BIGINT REFERENCES subforum(subforum_id)
);
--to display followed community
CREATE TABLE user_community (
user_id BIGINT REFERENCES users(user_id),
community_id BIGINT REFERENCES community(community_id)
);
CREATE TABLE pending_requests (
user_id BIGINT REFERENCES users(user_id),
community_id BIGINT REFERENCES community(community_id)
);
CREATE TABLE post (
post_id BIGSERIAL NOT NULL PRIMARY KEY,
title TEXT NOT NULL,
content TEXT NOT NULL,
time_of_creation TIMESTAMP,
upvotes INT DEFAULT 0,
downvotes INT DEFAULT 0,
author_id BIGINT REFERENCES users(user_id),
subforum_id BIGINT REFERENCES subforum(subforum_id),
community_id BIGINT REFERENCES community(community_id),
);
CREATE TABLE post_file (
file_name text NOT NULL,
post_id BIGINT REFERENCES post(post_id)
)
CREATE TABLE comment (
comment_id BIGSERIAL NOT NULL PRIMARY KEY,
content TEXT NOT NULL,
time_of_creation TIMESTAMP,
upvotes INT DEFAULT 0,
downvotes INT DEFAULT 0,
author_id BIGINT REFERENCES users(user_id),
post_id BIGINT REFERENCES post(post_id)
);
CREATE TABLE child_comment (
comment_id BIGSERIAL NOT NULL PRIMARY KEY,
content TEXT NOT NULL,
time_of_creation TIMESTAMP,
upvotes INT DEFAULT 0,
downvotes INT DEFAULT 0,
author_id BIGINT REFERENCES users(user_id),
parent_comment_id BIGINT REFERENCES comment(comment_id) DEFAULT null
)
CREATE TABLE post_category (
category_name TEXT NOT NULL,
post_id BIGINT REFERENCES post(post_id),
subforum_id BIGINT REFERENCES subforum(subforum_id)
);
CREATE TABLE chat (
chat_id BIGSERIAL NOT NULL PRIMARY KEY,
time_of_creation TIMESTAMP,
user1 VARCHAR(50) NOT NULL REFERENCES users(username),
user2 VARCHAR(50) NOT NULL REFERENCES users(username)
);
CREATE TABLE message (
message_id BIGSERIAL NOT NULL PRIMARY KEY,
content TEXT NOT NULL,
message_timestamp TIMESTAMP,
sender VARCHAR(50) REFERENCES users(username),
receiver VARCHAR(50) REFERENCES users(username),
chat_id BIGINT REFERENCES chat(chat_id)
);
--all functions of forum as queries
/*
logging in users with
username, first_name, last_name, email, password, dob
user_id is PK and is auto-incremented
*/
INSERT INTO users
(username,first_name,last_name,email,password,dob)
VALUES
('ram123', 'ram', 'shah', 'ram@sita.com', 'qwerty', '2000-01-01'),
('rahul123', 'rahul', 'shah', 'rahul@sita.com', 'qwerty', '2000-02-01'),
('karan123', 'karan', 'shah', 'karan@sita.com', 'qwerty', '2000-03-01');
/*
users filling in profile with
about
image
user_id is the FK and is present as PK in users table
each user can have only one profile
*/
INSERT INTO user_about
(about,user_id)
VALUES
('about me', 1),
('about me', 2),
('about me', 3);
/*
users filling in interests with
interest
user_id is the FK and is present as PK in users table
*/
INSERT INTO user_interest
(interest,user_id)
VALUES
('reading', 1),
('writing', 2),
('sleeping', 3);
/*
users filling in qualifications with
qualifications
user_id is the FK and is present as PK in users table
*/
INSERT INTO user_qualification
(qualification,user_id)
VALUES
('BCOM', 1),
('BTECH', 2),
('BAF', 3);
/*
users filling in feedback with
content
time_of_feedback is added automatically
feedback_id is PK and is auto-incremented
user_id is the FK and is present as PK in users table
each user can have multiple feedbacks
*/
INSERT INTO feedback
(content,time_of_feedback,user_id)
VALUES
('its good', CURRENT_TIMESTAMP, 1),
('its okay', CURRENT_TIMESTAMP, 2),
('its bad', CURRENT_TIMESTAMP, 3);
/*
users can create subforum with
name(unique)
description
time_of_creation is added automatically
subforum_id is PK and is auto-incremented
creator_id is the FK and is present as PK in users table as users_id
each user can create multiple subforums
users also have to enter category (multiple values allowed)
category_name is PK and has to be entered by user
category has a subforum_id which will be found automatically by name of subforum(as it is unique)
category has a post_id which will be null for this operation
*/
INSERT INTO subforum
(name,description,time_of_creation,creator_id)
VALUES
('dark', 'dark is the best show ever', CURRENT_TIMESTAMP, 1);
INSERT INTO category
(category_name, subforum_id)
(SELECT 'netflix', subforum_id
FROM subforum
WHERE name = 'dark'
);
/*
users can create community with
name(unique)
description
time_of_creation is added automatically
community_id is PK and is auto-incremented
creator_id is the FK and is present as PK in users table as users_id
each user can create multiple communities
*/
INSERT INTO community
(name,description,time_of_creation,creator_id)
VALUES
('harry potter', 'ron hermoine', CURRENT_TIMESTAMP, 2);
/*
users can follow a subforum, shown in table user_subforum
on following
user_id is added automatically
subforum_id will be added automatically by finding it using name of subforum(as it is unique)
*/
INSERT INTO user_subforum
(user_id,subforum_id)
(SELECT 3, subforum_id
FROM subforum
WHERE name='dark'
);
/*
user can be added in a community, shown in table user_community
on accepting invition, etc.
user_id is added automatically
community_id will be added automatically by finding it using name of community(as it is unique)
*/
INSERT INTO user_community
(user_id,community_id)
(SELECT 3, community_id
FROM community
WHERE name='harry potter'
);
/*
user can create a post with
title
content
time_of_creation is added automatically
post_id is PK and is auto-incremented
author_id is FK and is present as PK in users table
subforum_id is FK and will not be null if post is added in subforum(PK IN subforum table)
community_id is FK and will not be null if post is added in community(PK IN community table)
users also have to enter category (multiple values allowed)
category_name is PK and has to be entered by user
category has a post_id which will be found automatically by name of post(as it is unique)
category has a subforum_id which will be null for this operation
*/
--in this example, post is part of a subforum, so community_id = null
INSERT INTO post
(title,content,time_of_creation,author_id,subforum_id)
VALUES
('my post 2', 'content 2', CURRENT_TIMESTAMP, 1, 1);
INSERT INTO category
(category_name,post_id)
(SELECT 'new category 2', post_id
FROM post
WHERE title = 'my post 2' AND content = 'content 2' AND subforum_id = 1 AND author_id = '1'
);
/*
user can comment on posts with
content
time_of_creation is added automatically
comment_id is PK and is auto-incremented
author_id is FK and is present as PK in users table
post_id is FK and is present as PK in post table
parent_comment_id is FK and is present as PK in comment table
*/
-- primary insert(comment on post)
-- here, parent_comment_id is null
INSERT INTO comment
(content,time_of_creation,author_id,post_id)
VALUES
('this is a parent comment', CURRENT_TIMESTAMP, 1, 1);
-- secondary comment(comment on comment)
-- here, parent_comment_id is primary comment's comment_id
INSERT INTO child_comment
(content,time_of_creation,author_id,post_id, parent_comment_id)
VALUES
('this is a child comment', CURRENT_TIMESTAMP, 1, 1, 1);
/*
user can upvote or downvote posts or comments
*/
UPDATE post
SET upvotes = upvotes + 1
WHERE post_id = 1;
UPDATE comment
SET downvotes = downvotes + 1
WHERE comment_id = 1;
/*
user can delete a post
if user is creator of post
delete all comments corresponding to given post_id
make all post_id null in category table
delete row from post table
*/
DELETE FROM comment
WHERE post_id IN
(SELECT post_id
FROM post
WHERE post_id = 1 AND author_id = 1
);
UPDATE category
SET post_id = NULL
WHERE post_id IN
(SELECT post_id
FROM post
WHERE post_id = 1 AND author_id = 1
);
DELETE FROM post
WHERE post_id = 1 AND author_id = 1;
/*
user can delete a subforum/community
if user is creator of subforum/community
delete all posts corresponding to subforum/community(do process of deleting post)
delete all rows in user_subforum/community corresponding to subforum/community
make all subforum_id null in category table(not for community)
delete row from subforum/community
*/
DELETE FROM comment
WHERE post_id IN
(SELECT post_id
FROM post
WHERE subforum_id IN
(SELECT subforum_id
FROM subforum
WHERE subforum_id = 1 AND creator_id = 1
)
);
UPDATE category
SET post_id = NULL
WHERE post_id IN
(SELECT post_id
FROM post
WHERE subforum_id IN
(SELECT subforum_id
FROM subforum
WHERE subforum_id = 1 AND creator_id = 1
)
);
DELETE FROM post
WHERE post_id IN
(SELECT post_id
FROM post
WHERE subforum_id IN
(SELECT subforum_id
FROM subforum
WHERE subforum_id = 1 AND creator_id = 1
)
);
--similarly for community
DELETE FROM user_subforum
WHERE subforum_id IN
(SELECT subforum_id
FROM subforum
WHERE subforum_id = 1 AND creator_id = 1
);
--similarly for community
UPDATE category
SET subforum_id = NULL
WHERE subforum_id IN
(SELECT subforum_id
FROM subforum
WHERE subforum_id = 1 AND creator_id = 1
);
--only for subforum
DELETE FROM subforum
WHERE subforum_id = 1 AND creator_id = 1;
--similarly for community
/*
user can delete a comment
if user is creator of post
delete row from comment table for corresponding comment_id
*/
DELETE FROM comment
WHERE comment_id = 1;
/*
users can chat with each other with
user_id for creator(FK, is PK in users table)
user_id for other user in chat(FK, is PK in users table)
timestamp is added automatically
chat_id is PK and is auto-incremented
INSERT INTO chat
(user1_id,user2_id,time_of_creation)
VALUES
(1,2,CURRENT_TIMESTAMP);
users can send messages in the chat with
content
sender_id for creator(FK, is PK in users table)
reciever_id for other user in chat(FK, is PK in users table)
timestamp is added automatically
message_id is PK and is autp-incremented
chat_id is FK and is PK in chat table
INSERT INTO message
(content,sender_id,reciever_id,message_timestamp,chat_id)
VALUES
('how are you?', 1, 2, CURRENT_TIMESTAMP, 1);
*/