-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_instagram_data.sql
143 lines (94 loc) · 2.33 KB
/
12_instagram_data.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
CREATE DATABASE instagram_data;
USE instagram_data;
CREATE TABLE users(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) UNIQUE,
created_at TIMESTAMP DEFAULT NOW()
)
CREATE TABLE photos(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
image_url VARCHAR(255) NOT NULL,
user_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
FOREIGN KEY(user_id) REFERENCES users(id)
);
CREATE TABLE comments(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
comment_text VARCHAR(255) NOT NULL,
user_id INTEGER NOT NULL,
photo_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(photo_id) REFERENCES photos(id)
)
CREATE TABLE likes(
user_id INTEGER NOT NULL,
photo_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(photo_id) REFERENCES photos(id),
PRIMARY KEY (user_id,photo_id)
);
CREATE TABLE follows(
follower_id INTEGER NOT NULL,
followee_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
FOREIGN KEY (follower_id) REFERENCES users(id),
FOREIGN KEY (followee_id) REFERENCES users(id),
PRIMARY KEY (follower_id,followee_id)
);
CREATE TABLE tags(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
tag_name VARCHAR(255) UNIQUE,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE TABLE photo_tags (
photo_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
FOREIGN KEY(photo_id) REFERENCES photos(id),
FOREIGN KEY(tag_id) REFERENCES tags(id),
PRIMARY KEY(photo_id,tag_id)
);
INSERT INTO tags(tag_name) VALUES
('adorable'),
('cutes'),
('sunrise');
INSERT INTO photo_tag(photo_id,tag_id) VALUES
(1,1),
(1,2),
(2,3),
(3,2);
SELECT * FROM tags;
INSERT INTO follows(follower_id,followee_id) VALUES
(1,2),
(1,3),
(3,1),
(2,3);
INSERT INTO follows(follower_id,followee_id) VALUES
(1,2);
SELECT * FROM follows;
INSERT INTO likes(user_id,photo_id)VALUES
(2,1),
(3,1),
(1,2),
(1,3);
INSERT INTO comments(comment_text,user_id,photo_id) VALUES
('Meaow',1,2),
('Amazing Shot',3,2),
('I <3 This',2,1);
SELECT * FROM comments;
SHOW TABLES;
INSERT INTO photos(image_url,user_id) VALUES
('/whowho',1),
('/whowhowho',2),
('/whowhoiam',3);
SELECT photos.image_url,users.username
FROM photos
JOIN users
ON photos.user_id = users.id;
SELECT * FROM photos;
INSERT INTO users (username) VALUES
('BlueTheCat'),
('CharlieBrown'),
('NoamWatts');
SELECT * FROM users;