-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddl.sql
196 lines (194 loc) · 7.56 KB
/
ddl.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
CREATE TABLE IF NOT EXISTS providers (
provider_id serial primary key,
provider_name varchar(255) NOT NULL UNIQUE,
contact_phone_num char(12) NOT NULL
CONSTRAINT ch_tel_num CHECK ( char_length(contact_phone_num) =12 )
);
CREATE TABLE IF NOT EXISTS canvases_providers (
canvas_provider_id serial primary key,
provider_id int NOT NULL ,
canvas_id int NOT NULL ,
quantity int NOT NULL ,
canvas_provider_date date NOT NULL,
CONSTRAINT ch_date CHECK ( canvas_provider_date > '1900-01-01' ),
CONSTRAINT ch_positive CHECK ( quantity>0 )
);
CREATE TABLE IF NOT EXISTS canvases (
canvas_id serial primary key ,
canvas_name varchar(255) NOT NULL ,
width int NOT NULL ,
height int NOT NULL ,
canvas_type_id int NOT NULL
CONSTRAINT ch_positive CHECK ( width>0 AND height>0)
);
CREATE TABLE IF NOT EXISTS canvas_types (
canvas_type_id serial primary key ,
canvas_type_name varchar(255) NOT NULL UNIQUE ,
material_of_manufacture varchar(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS paints_providers (
paint_provider_id serial primary key ,
provider_id int NOT NULL ,
paint_id int NOT NULL ,
quantity int NOT NULL ,
paint_provider_date date NOT NULL ,
CONSTRAINT ch_date CHECK ( paint_provider_date > '1900-01-01' ),
CONSTRAINT ch_positive CHECK ( quantity>0 )
);
CREATE TABLE IF NOT EXISTS genres_paintings (
genre_painting_id serial primary key ,
genre_id int NOT NULL ,
painting_id int NOT NULL
);
CREATE TABLE IF NOT EXISTS paintings (
painting_id serial primary key ,
canvas_id int NOT NULL ,
painting_name varchar(255) NOT NULL UNIQUE ,
is_reproduction boolean NOT NULL
);
CREATE TABLE IF NOT EXISTS painting_paints (
painting_paint_id serial primary key ,
painting_id int NOT NULL ,
paint_id int NOT NULL ,
quantity int NOT NULL,
CONSTRAINT ch_positive CHECK ( quantity>0 )
);
CREATE TABLE IF NOT EXISTS paints(
paint_id serial primary key ,
paint_name varchar(255) NOT NULL UNIQUE ,
paint_color varchar(255) NOT NULL ,
paint_type_id int NOT NULL
);
CREATE TABLE IF NOT EXISTS paints_types (
paint_type_id serial primary key ,
paint_type_name varchar(255) NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS genres (
genre_id serial primary key ,
genre_name varchar(255) NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS genres_painters (
genre_painter_id serial primary key ,
genre_id int NOT NULL ,
painter_id int NOT NULL
);
CREATE TABLE IF NOT EXISTS painters (
painter_id serial primary key ,
painter_first_name varchar(255) NOT NULL ,
painter_last_name varchar(255) NOT NULL ,
painter_middle_name varchar(255) ,
gender boolean NOT NULL ,
birthday date NOT NULL ,
experience date NOT NULL,
CONSTRAINT ch_date CHECK ( birthday > '1900-01-01' AND experience>'1900-01-01' )
);
CREATE TABLE IF NOT EXISTS orders (
order_id serial primary key ,
painter_id int NOT NULL ,
client_order_id int NOT NULL ,
painting_id int NOT NULL ,
deadline date NOT NULL ,
price numeric(8,2) NOT NULL,
CONSTRAINT ch_date CHECK ( deadline > '1900-01-01' ),
CONSTRAINT ch_not_negative CHECK ( price>=0 )
);
CREATE TABLE IF NOT EXISTS accounts (
account_id serial primary key ,
login varchar(60) NOT NULL UNIQUE ,
email varchar(255) NOT NULL ,
password varchar(60) NOT NULL,
CONSTRAINT ch_email CHECK ( email ~ '^[\w\.-]+@([\w-]+\.)+[\w-]{2,4}$')
);
CREATE TABLE IF NOT EXISTS clients(
client_id serial primary key ,
account_id int,
client_first_name varchar(255) NOT NULL ,
client_last_name varchar(255) NOT NULL ,
client_middle_name varchar(255) ,
gender boolean NOT NULL ,
birthday date NOT NULL,
CONSTRAINT ch_date CHECK ( birthday > '1900-01-01' )
);
CREATE TABLE IF NOT EXISTS client_orders (
client_order_id serial primary key ,
order_num int NOT NULL UNIQUE ,
client_id int NOT NULL ,
discount_id int ,
price numeric(8,2) NOT NULL DEFAULT 0,
CONSTRAINT ch_positive CHECK ( order_num>0 ),
CONSTRAINT ch_not_negative CHECK ( price>=0 )
);
CREATE TABLE IF NOT EXISTS discounts(
discount_id serial primary key ,
discount_name varchar(255) NOT NULL UNIQUE ,
percent numeric(5,2) NOT NULL,
CONSTRAINT ch_percent CHECK ( percent BETWEEN 0 AND 100)
);
ALTER TABLE canvases_providers
ADD CONSTRAINT fk_canvases_providers$provider_id
FOREIGN KEY (provider_id)
REFERENCES providers (provider_id) ON UPDATE CASCADE ON DELETE CASCADE,
ADD CONSTRAINT fk_canvases_providers$canvas_id
FOREIGN KEY (canvas_id)
REFERENCES canvases (canvas_id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE paints_providers
ADD CONSTRAINT fk_paints_providers$provider_id
FOREIGN KEY (provider_id)
REFERENCES providers (provider_id) ON UPDATE CASCADE ON DELETE CASCADE,
ADD CONSTRAINT fk_paints_providers$paint_id
FOREIGN KEY (paint_id)
REFERENCES paints (paint_id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE canvases
ADD CONSTRAINT fk_canvases$canvas_type_id
FOREIGN KEY (canvas_type_id)
REFERENCES canvas_types (canvas_type_id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE genres_paintings
ADD CONSTRAINT fk_genres_paintings$genre_id
FOREIGN KEY (genre_id)
REFERENCES genres(genre_id) ON UPDATE CASCADE ON DELETE CASCADE,
ADD CONSTRAINT fk_genres_paintings$painting_id
FOREIGN KEY (painting_id)
REFERENCES paintings(painting_id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE paintings
ADD CONSTRAINT fk_paintings$canvas_id
FOREIGN KEY (canvas_id)
REFERENCES canvases(canvas_id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE painting_paints
ADD CONSTRAINT fk_painting_paints$painting_id
FOREIGN KEY (painting_id)
REFERENCES paintings (painting_id) ON UPDATE CASCADE ON DELETE CASCADE,
ADD CONSTRAINT fk_painting_paints$paint_id
FOREIGN KEY (paint_id)
REFERENCES paints (paint_id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE paints
ADD CONSTRAINT fk_paints$paint_type_id
FOREIGN KEY (paint_type_id)
REFERENCES paints_types (paint_type_id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE genres_painters
ADD CONSTRAINT fk_genres_painters$genre_id
FOREIGN KEY (genre_id)
REFERENCES genres (genre_id) ON UPDATE CASCADE ON DELETE CASCADE,
ADD CONSTRAINT fk_genres_painters$painter_id
FOREIGN KEY (painter_id)
REFERENCES painters (painter_id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE orders
ADD CONSTRAINT fk_orders$painter_id
FOREIGN KEY (painter_id)
REFERENCES painters (painter_id) ON UPDATE CASCADE ON DELETE CASCADE,
ADD CONSTRAINT fk_orders$client_order_id
FOREIGN KEY (client_order_id)
REFERENCES client_orders (client_order_id) ON UPDATE CASCADE ON DELETE CASCADE,
ADD CONSTRAINT fk_orders$painting_id
FOREIGN KEY (painting_id)
REFERENCES paintings (painting_id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE client_orders
ADD CONSTRAINT fk_client_orders$client_id
FOREIGN KEY (client_id)
REFERENCES clients (client_id) ON UPDATE CASCADE ON DELETE CASCADE,
ADD CONSTRAINT fk_client_orders$discount_id
FOREIGN KEY (discount_id)
REFERENCES discounts (discount_id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE clients
ADD CONSTRAINT fk_clients$account_id
FOREIGN KEY (account_id)
REFERENCES accounts(account_id) ON UPDATE CASCADE ON DELETE CASCADE;