-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable-DDL.sql
More file actions
53 lines (46 loc) · 1.55 KB
/
Table-DDL.sql
File metadata and controls
53 lines (46 loc) · 1.55 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
-- drop schema instagram;
DROP TABLE IF EXISTS comments;
DROP TABLE IF EXISTS likes;
DROP TABLE IF EXISTS followers;
DROP TABLE IF EXISTS posts;
DROP TABLE IF EXISTS users;
CREATE TABLE users (
user_id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR(40) NOT NULL,
email VARCHAR(20) NOT NULL,
phone_no VARCHAR(20) NOT NULL
);
-- select * from followers;
CREATE TABLE posts (
post_id SERIAL PRIMARY KEY NOT NULL ,
user_id INTEGER NOT NULL,
caption TEXT ,
image_url VARCHAR(200) ,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
FOREIGN KEY ( user_id ) REFERENCES users(user_id)
);
CREATE TABLE likes (
like_id SERIAL PRIMARY KEY NOT NULL,
post_id INTEGER NOT NULL ,
user_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
FOREIGN KEY ( user_id ) REFERENCES users(user_id) ,
FOREIGN KEY ( post_id ) REFERENCES posts(post_id)
);
CREATE TABLE followers (
follower_id SERIAL PRIMARY KEY NOT NULL ,
user_id INTEGER NOT NULL ,
followers_user_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY ( user_id ) REFERENCES users(user_id) ,
FOREIGN KEY ( followers_user_id ) REFERENCES users(user_id)
);
CREATE TABLE comments (
comments_id SERIAL PRIMARY KEY NOT NULL,
post_id INTEGER NOT NULL,
user_id INTEGER NOT NULL ,
comment_text VARCHAR(40),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY ( user_id ) REFERENCES users(user_id) ,
FOREIGN KEY ( post_id ) REFERENCES posts(post_id)
);