-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschema.sql
More file actions
117 lines (104 loc) · 3.03 KB
/
schema.sql
File metadata and controls
117 lines (104 loc) · 3.03 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* There are initial commands under postgres user*/
DROP DATABASE IF EXISTS buy_and_sell;
DROP ROLE IF EXISTS buy_sell;
CREATE ROLE buy_sell WITH
LOGIN
NOSUPERUSER
CREATEDB
NOCREATEROLE
INHERIT
NOREPLICATION
CONNECTION LIMIT -1
PASSWORD '';
CREATE DATABASE buy_and_sell
WITH
OWNER = buy_sell
TEMPLATE = template0
ENCODING = 'UTF8'
LC_COLLATE = 'C'
LC_CTYPE = 'C'
CONNECTION LIMIT = -1;
CREATE DATABASE test_buy_and_sell
WITH
OWNER = buy_sell
TEMPLATE = template0
ENCODING = 'UTF8'
LC_COLLATE = 'C'
LC_CTYPE = 'C'
CONNECTION LIMIT = -1;
/* There are commands under buy_sell user*/
DROP TABLE IF EXISTS comments;
DROP TABLE IF EXISTS offers_categories;
DROP TABLE IF EXISTS offers;
DROP TABLE IF EXISTS categories;
DROP TABLE IF EXISTS users;
DROP TYPE IF EXISTS offer_type;
CREATE TABLE users
(
id bigserial NOT NULL,
name character varying(50) NOT NULL,
email character varying(50) UNIQUE NOT NULL,
passwordHash character varying(50) NOT NULL CHECK (char_length(passwordHash) > 6),
avatar character varying(50),
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX email_index ON users ((lower(email)));
CREATE TYPE offer_type AS ENUM ('buy', 'offer');
SET datestyle = "ISO, DMY";
CREATE TABLE offers
(
id bigserial NOT NULL,
type offer_type NOT NULL,
title character varying(100) NOT NULL,
description character varying(1000) NOT NULL,
sum numeric NOT NULL,
picture character varying(500),
created_date DATE NOT NULL,
user_id bigint NOT NULL,
PRIMARY KEY (id),
CONSTRAINT offers_users FOREIGN KEY (user_id)
REFERENCES users (id) MATCH FULL
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE INDEX title_index ON offers ((lower(title)));
CREATE INDEX offer_created_date_index ON offers (created_date);
CREATE TABLE categories
(
id bigserial NOT NULL,
title character varying(50) NOT NULL,
picture character varying(500),
PRIMARY KEY (id)
);
CREATE TABLE comments
(
id bigserial NOT NULL,
text character varying(300) NOT NULL,
created_date DATE NOT NULL,
user_id bigint NOT NULL,
offer_id bigint NOT NULL,
PRIMARY KEY (id),
CONSTRAINT comments_users FOREIGN KEY (user_id)
REFERENCES users (id) MATCH FULL
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT comments_offers FOREIGN KEY (offer_id)
REFERENCES offers (id) MATCH FULL
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE INDEX user_id_index ON comments (user_id);
CREATE INDEX offer_id_index ON comments (offer_id);
CREATE INDEX comment_created_date_index ON comments (created_date);
CREATE TABLE offers_categories
(
offer_id bigint NOT NULL,
category_id bigint NOT NULL,
CONSTRAINT offers_categories_pk PRIMARY KEY (offer_id, category_id),
FOREIGN KEY(offer_id) REFERENCES offers (id) MATCH FULL
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY(category_id) REFERENCES categories (id) MATCH FULL
ON UPDATE CASCADE
ON DELETE CASCADE
);