-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscema.sql
More file actions
49 lines (41 loc) · 1.22 KB
/
scema.sql
File metadata and controls
49 lines (41 loc) · 1.22 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
CREATE TABLE users
(
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
username varchar(50) NOT NULL UNIQUE,
password varchar(255) NOT NULL,
email varchar(320) NOT NULL UNIQUE,
created_at timestamp NOT NULL DEFAULT NOW()
);
CREATE TABLE chats
(
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name varchar(255) NOT NULL,
created_at timestamp NOT NULL DEFAULT NOW()
);
CREATE TABLE user_chat_registry
(
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
created_at timestamp NOT NULL DEFAULT NOW(),
user_id integer NOT NULL,
chat_id integer NOT NULL,
CONSTRAINT fk_user FOREIGN KEY (user_id)
REFERENCES users(id)
ON DELETE CASCADE,
CONSTRAINT fk_chat FOREIGN KEY (chat_id)
REFERENCES chats(id)
ON DELETE CASCADE
);
CREATE TABLE messages
(
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
message_text varchar(2000) NOT NULL,
sent_at timestamp NOT NULL DEFAULT NOW(),
user_id integer NOT NULL,
chat_id integer NOT NULL,
CONSTRAINT fk_user FOREIGN KEY (user_id)
REFERENCES users(id)
ON DELETE SET NULL,
CONSTRAINT fk_chat FOREIGN KEY (chat_id)
REFERENCES chats(id)
ON DELETE SET NULL
);