-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_db.sql
More file actions
98 lines (82 loc) · 2.68 KB
/
import_db.sql
File metadata and controls
98 lines (82 loc) · 2.68 KB
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
CREATE TABLE users (
id INTEGER PRIMARY KEY,
fname VARCHAR(255) NOT NULL,
lname VARCHAR(255) NOT NULL
);
-- Notice that tables are always named lowercase and plural. This is a
-- convention.
CREATE TABLE questions (
-- SQLite3 will automatically populate an integer primary key
-- (unless it is specifically provided). The conventional primary
-- key name is 'id'.
id INTEGER PRIMARY KEY,
-- NOT NULL specifies that the column must be provided. This is a
-- useful check of the integrity of the data.
title VARCHAR(255) NOT NULL,
body VARCHAR(255) NOT NULL,
author_id INTEGER NOT NULL,
-- Not strictly necessary, but informs the DB not to
-- (1) create a professor with an invalid department_id,
-- (2) delete a department (or change its id) if professors
-- reference it.
-- Either event would leave the database in an invalid state, with a
-- foreign key that doesn't point to a valid record. Older versions
-- of SQLite3 may not enforce this, and will just ignore the foreign
-- key constraint.
-- FOREIGN KEY (users.id) REFERENCES questions(id)
FOREIGN KEY (author_id) REFERENCES questions(id)
);
CREATE TABLE question_followers (
id INTEGER PRIMARY KEY,
question_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
FOREIGN KEY (question_id) REFERENCES question(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE replies (
id INTEGER PRIMARY KEY,
question_id INTEGER NOT NULL,
parent_reply INTEGER,
user_id INTEGER NOT NULL,
body VARCHAR(255) NOT NULL,
FOREIGN KEY (question_id) REFERENCES question(id),
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (parent_reply) REFERENCES replies(id)
);
CREATE TABLE question_likes (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL,
question_id INTEGER NOT NULL,
FOREIGN KEY (question_id) REFERENCES question(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
-- In addition to creating tables, we can seed our database with some
-- starting data.
INSERT INTO
users (fname, lname)
VALUES
('andre', 'tran'),
('taylor', 'smith');
INSERT INTO
questions (title, body, author_id)
VALUES
('Chicken vs Egg', 'Which came first the chicken or the egg?',
(SELECT
id
FROM
users
WHERE
fname = 'andre'));
INSERT INTO
question_followers(user_id, question_id)
VALUES
((SELECT id FROM users WHERE fname = 'taylor'),
(SELECT id FROM questions WHERE title = 'Chicken vs Egg'));
INSERT INTO
replies(question_id, parent_reply, user_id, body)
VALUES
(1, NULL, 2, 'The chicken!');
INSERT INTO
question_likes(user_id, question_id)
VALUES
(2, 1);