-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_create_comments_table.sql
More file actions
33 lines (31 loc) · 1.11 KB
/
03_create_comments_table.sql
File metadata and controls
33 lines (31 loc) · 1.11 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
CREATE TABLE users (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
username VARCHAR(30) NOT NULL,
bio VARCHAR(400),
avatar VARCHAR(200),
phone VARCHAR(25),
email VARCHAR(40),
password VARCHAR(50),
status VARCHAR(15),
CHECK(COALESCE(phone, email) IS NOT NULL)
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
url VARCHAR(200) NOT NULL,
caption VARCHAR(240),
lat REAL CHECK(lat IS NULL OR (lat >= -90 AND lat <= 90)),
lng REAL CHECK(lng IS NULL OR (lng >= -180 AND lng <= 180)),
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE comments (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
contents VARCHAR(240) NOT NULL,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE
);