Created
January 31, 2018 12:18
-
-
Save HubertArciszewski95/83bc04580ca8af65c461bfe930aaeb2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Instagram database clone-- | |
--SCHEMA-- | |
-- users-- | |
CREATE TABLE users ( | |
id INT AUTO_INCREMENT PRIMARY KEY, | |
username VARCHAR(255) UNIQUE NOT NULL, | |
created_at TIMESTAMP DEFAULT NOW() | |
); | |
-- photos-- | |
CREATE TABLE photos ( | |
id INTEGER AUTO_INCREMENT PRIMARY KEY, | |
image_url VARCHAR(255) NOT NULL, | |
user_id INT NOT NULL, | |
created_at TIMESTAMP DEFAULT NOW(), | |
FOREIGN KEY(user_id) REFERENCES users(id) | |
); | |
--comments-- | |
CREATE TABLE comments ( | |
id INT AUTO_INCREMENT PRIMARY KEY, | |
comment_text VARCHAR(255) NOT NULL, | |
user_id INT NOT NULL, | |
photo_id INT NOT NULL, | |
created_at TIMESTAMP DEFAULT NOW(), | |
FOREIGN KEY (user_id) REFERENCES users(id), | |
FOREIGN KEY (photo_id) REFERENCES photos(id) | |
); | |
--likes-- | |
CREATE TABLE likes ( | |
user_id INT NOT NULL, | |
photo_id INT 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) | |
); | |
--folowwers-- | |
CREATE TABLE follows ( | |
follower_id INT NOT NULL, | |
followee_id INT 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) | |
); | |
--hashtags-- | |
--table storing hastags-- | |
CREATE TABLE tags ( | |
id INT AUTO_INCREMENT PRIMARY KEY, | |
tag_name VARCHAR(255) UNIQUE, | |
created_at TIMESTAMP DEFAULT NOW() | |
); | |
--table conecting tags with certain photo-- | |
CREATE TABLE photo_tags ( | |
photo_id INT NOT NULL, | |
tag_id INT NOT NULL, | |
FOREIGN KEY(photo_id) REFERENCES photos(id), | |
FOREIGN KEY(tag_id) REFERENCES tags(id), | |
PRIMARY KEY(photo_id, tag_id) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment