-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_tables_and_indices.sql
43 lines (33 loc) · 1.16 KB
/
create_tables_and_indices.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
-- Table `users`
CREATE TABLE IF NOT EXISTS users
(
id SERIAL PRIMARY KEY,
email VARCHAR(30) UNIQUE NOT NULL,
-- oauth_provider VARCHAR(25),
-- oauth_uid VARCHAR(50),
hashed_password TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Indices for `users` table (not creating as UNIQUE idx is automatically created)
-- CREATE INDEX idx_users_email_key ON users USING HASH (email);
------------------------------------------------------------
-- Table `urls`
CREATE TABLE IF NOT EXISTS urls
(
short_id VARCHAR(7) PRIMARY KEY,
long_url VARCHAR NOT NULL,
user_id INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
-- Indices for `urls` table
CREATE INDEX idx_urls_userid_key ON urls (user_id); --USING HASH (user_id);
------------------------------------------------------------
-- Table `click_analytics`
CREATE TABLE click_analytics (
short_id TEXT PRIMARY KEY,
count INT DEFAULT 0,
countries JSONB DEFAULT '{}'::jsonb,
referers JSONB DEFAULT '{}'::jsonb
);