-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
57 lines (48 loc) · 1.38 KB
/
schema.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE USER_NT (
user_id UUID PRIMARY KEY,
name VARCHAR(150) NOT NULL,
surname VARCHAR(150) NOT NULL,
email VARCHAR(250) NOT NULL,
password VARCHAR(250) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE NOTE (
note_id SERIAL PRIMARY KEY,
title VARCHAR(255),
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
user_id UUID,
FOREIGN KEY (user_id) REFERENCES USER_NT(user_id)
);
CREATE TABLE TAG (
tag_id SERIAL PRIMARY KEY,
name VARCHAR(50),
color VARCHAR(10),
user_id UUID,
FOREIGN KEY (user_id) REFERENCES USER_NT(user_id)
);
CREATE TABLE NOTE_TAG (
note_id INTEGER,
tag_id INTEGER,
PRIMARY KEY (note_id, tag_id),
FOREIGN KEY (note_id) REFERENCES NOTE(note_id),
FOREIGN KEY (tag_id) REFERENCES TAG(tag_id)
);
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER user_update
BEFORE UPDATE ON USER_NT
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER note_update
BEFORE UPDATE ON NOTE
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();