1 | set
|
---|
2 | search_path = project;
|
---|
3 | --Create DB Shema
|
---|
4 | --drop schema if exists project cascade;
|
---|
5 |
|
---|
6 | --create schema project;
|
---|
7 |
|
---|
8 | drop table if exists cities cascade;
|
---|
9 | drop table if exists vet_centers cascade;
|
---|
10 | drop table if exists jobs cascade;
|
---|
11 | drop table if exists blog_post_for_consultations cascade;
|
---|
12 | drop table if exists blog_post_answers cascade;
|
---|
13 | drop table if exists pet_cares cascade;
|
---|
14 | drop table if exists type_of_pets cascade;
|
---|
15 | drop table if exists pets cascade;
|
---|
16 | drop table if exists pet_galery cascade;
|
---|
17 | drop table if exists reports cascade;
|
---|
18 | drop table if exists diagnostics cascade;
|
---|
19 | drop table if exists manufacturers cascade;
|
---|
20 | drop table if exists medecines cascade;
|
---|
21 | drop table if exists pet_status cascade;
|
---|
22 | drop table if exists therapy cascade;
|
---|
23 | drop table if exists breeds cascade;
|
---|
24 | drop table if exists products cascade;
|
---|
25 | drop table if exists orders cascade;
|
---|
26 | drop table if exists pets_visit_vet_centers cascade;
|
---|
27 | drop table if exists products_there_are_for_typepets cascade;
|
---|
28 | drop table if exists product_are_made_orders cascade;
|
---|
29 | --drop table if exists products_available_type_of_pets cascade;
|
---|
30 | drop table if exists therapy_takes_pets cascade;
|
---|
31 | drop table if exists diagnostics_established_pets cascade;
|
---|
32 | drop table if exists users cascade;
|
---|
33 | drop table if exists roles cascade;
|
---|
34 | --Create Tables
|
---|
35 | create table cities(
|
---|
36 | ID SERIAL,
|
---|
37 | name varchar(50),
|
---|
38 | --adress varchar(50),
|
---|
39 | --phoneNumber varchar(20),
|
---|
40 | constraint pk_cities primary key (ID)
|
---|
41 | );
|
---|
42 |
|
---|
43 | create table vet_centers(
|
---|
44 | ID SERIAL,
|
---|
45 | name varchar(50),
|
---|
46 | description varchar(150),
|
---|
47 | adress varchar(50),
|
---|
48 | phoneNumber varchar(15),
|
---|
49 | latitude decimal,
|
---|
50 | longitude decimal,
|
---|
51 | workingHours varchar(50),
|
---|
52 | constraint pk_vetcen primary key (ID),
|
---|
53 | citiesID bigint not null,
|
---|
54 | constraint fk_cities foreign key (citiesID) references cities(ID)
|
---|
55 | );
|
---|
56 |
|
---|
57 | create table jobs(
|
---|
58 | ID SERIAL,
|
---|
59 | description varchar(50),
|
---|
60 | predictedSalery varchar(30),
|
---|
61 | constraint pk_jjobs primary key (ID),
|
---|
62 | vetCentersID bigint not null,
|
---|
63 | constraint fk_vetCenters foreign key ( vetCentersID) references vet_centers(ID)
|
---|
64 |
|
---|
65 | );
|
---|
66 |
|
---|
67 | create table roles(
|
---|
68 | ID SERIAL,
|
---|
69 | type varchar(15) not null unique,
|
---|
70 | constraint pk_roles primary key (ID)
|
---|
71 | );
|
---|
72 |
|
---|
73 | create table users(
|
---|
74 | ID SERIAL,
|
---|
75 | name varchar(30) not null,
|
---|
76 | lastname varchar(30) not null,
|
---|
77 | email varchar(30) not null unique,
|
---|
78 | password varchar(256) not null unique,
|
---|
79 | number varchar(15) not null unique,
|
---|
80 | role_id bigint not null,
|
---|
81 | jobs_id bigint default null,
|
---|
82 | constraint pk_users primary key (ID),
|
---|
83 | constraint fk_roles foreign key (role_id) references roles(ID),
|
---|
84 | constraint fk_jobs foreign key (jobs_id) references jobs(ID)
|
---|
85 |
|
---|
86 | );
|
---|
87 |
|
---|
88 | create table blog_post_for_consultations(
|
---|
89 | ID SERIAL,
|
---|
90 | date_askes date,
|
---|
91 | title varchar(30),
|
---|
92 | description varchar(150),
|
---|
93 | users_id bigint not null,
|
---|
94 | constraint pk_blogPFC primary key (ID),
|
---|
95 | constraint fk_users foreign key (users_id) references users(ID)
|
---|
96 | );
|
---|
97 | create table blog_post_answers(
|
---|
98 | ID SERIAL,
|
---|
99 | parent_id bigint,
|
---|
100 | reply varchar(150),
|
---|
101 | blogPostConsID bigint not null,
|
---|
102 | usersID bigint not null,
|
---|
103 | constraint pk_blogPA primary key (ID),
|
---|
104 | constraint fk_blogBPFC foreign key (blogPostConsID) references blog_post_for_consultations(ID),
|
---|
105 | constraint fk_users foreign key (usersID) references users(ID)
|
---|
106 | );
|
---|
107 |
|
---|
108 | create table pet_cares(
|
---|
109 | ID SERIAL,
|
---|
110 | title varchar(50),
|
---|
111 | description varchar(150),
|
---|
112 | dateending date not null,
|
---|
113 | constraint pk_petCares primary key (ID),
|
---|
114 | usersID bigint not null,
|
---|
115 | vetcentersID bigint not null,
|
---|
116 | constraint fk_blogBPFC foreign key (usersID) references users(ID),
|
---|
117 | constraint fk_vetCenters foreign key (vetcentersID) references vet_centers(ID)
|
---|
118 | );
|
---|
119 |
|
---|
120 | create table type_of_pets(
|
---|
121 | ID SERIAL,
|
---|
122 | description varchar(30),
|
---|
123 | name varchar(30),
|
---|
124 | constraint pk_typeOfPets primary key (ID)
|
---|
125 |
|
---|
126 | );
|
---|
127 |
|
---|
128 | create table pets(
|
---|
129 | ID SERIAL,
|
---|
130 | color varchar(20),
|
---|
131 | description varchar(50),
|
---|
132 | dateOfBirthday date,
|
---|
133 | constraint pk_pets primary key (ID),
|
---|
134 | usersID bigint not null,
|
---|
135 | typeOfPetsID bigint not null,
|
---|
136 | constraint fk_typeOfPets foreign key (typeOfPetsID) references type_of_pets(ID),
|
---|
137 | constraint fk_users foreign key (usersID) references users(ID)
|
---|
138 | );
|
---|
139 |
|
---|
140 | create table pet_galery(
|
---|
141 | ID SERIAL,
|
---|
142 | picture varchar(1000),
|
---|
143 | constraint pk_petGalery primary key (ID),
|
---|
144 | petsID bigint not null,
|
---|
145 | constraint fk_pets foreign key (petsID) references pets(ID)
|
---|
146 | );
|
---|
147 | create table reports(
|
---|
148 | ID SERIAL,
|
---|
149 | description varchar(150),
|
---|
150 | constraint pk_reports primary key (ID),
|
---|
151 | usersID bigint not null,
|
---|
152 | petsID bigint not null,
|
---|
153 | constraint fk_users foreign key (usersID) references users(ID),
|
---|
154 | constraint fk_pets foreign key (petsID) references pets(ID)
|
---|
155 | );
|
---|
156 |
|
---|
157 | create table diagnostics(
|
---|
158 | ID SERIAL,
|
---|
159 | description varchar(200),
|
---|
160 | constraint pk_diagnostics primary key (ID),
|
---|
161 | usersID bigint not null,
|
---|
162 | constraint fk_users foreign key (usersID) references users(ID)
|
---|
163 | );
|
---|
164 |
|
---|
165 | create table manufacturers(
|
---|
166 | ID SERIAL,
|
---|
167 | name varchar(50) not null,
|
---|
168 | description varchar(200),
|
---|
169 | city varchar(50),
|
---|
170 | state varchar(50),
|
---|
171 | constraint pk_manufacturers primary key (ID)
|
---|
172 |
|
---|
173 | );
|
---|
174 |
|
---|
175 | create table medecines(
|
---|
176 | ID SERIAL,
|
---|
177 | name varchar(50) not null,
|
---|
178 | description varchar(200),
|
---|
179 | constraint pk_medecines primary key (ID),
|
---|
180 | diagnosticsID bigint not null,
|
---|
181 | manufacturersID bigint not null,
|
---|
182 | constraint fk_diagnostics foreign key (diagnosticsID) references diagnostics(ID),
|
---|
183 | constraint fk_manufacturers foreign key (manufacturersID) references manufacturers(ID)
|
---|
184 | );
|
---|
185 |
|
---|
186 | create table pet_status(
|
---|
187 | ID SERIAL,
|
---|
188 | type varchar(30),
|
---|
189 | node varchar(200),
|
---|
190 | constraint pk_pet_status primary key (ID)
|
---|
191 | );
|
---|
192 |
|
---|
193 | create table therapy(
|
---|
194 | ID SERIAL,
|
---|
195 | description varchar(150),
|
---|
196 | appoitmentDate date,
|
---|
197 | constraint pk_therapy primary key (ID),
|
---|
198 | diagnosticsID bigint not null,
|
---|
199 | pet_statusID bigint not null,
|
---|
200 | constraint fk_pet_status foreign key (pet_statusID) references pet_status(ID),
|
---|
201 | constraint fk_diagnostics foreign key (diagnosticsID) references diagnostics(ID)
|
---|
202 | );
|
---|
203 |
|
---|
204 | create table breeds(
|
---|
205 | ID SERIAL,
|
---|
206 | name varchar(50),
|
---|
207 | constraint pk_breeds primary key(ID),
|
---|
208 | constraint fk_type_of_pets foreign key (ID) references type_of_pets(ID) on delete cascade
|
---|
209 | );
|
---|
210 |
|
---|
211 | create table products(
|
---|
212 | ID SERIAL,
|
---|
213 | name varchar(50),
|
---|
214 | description varchar(150),
|
---|
215 | price varchar(50),
|
---|
216 | isActive bool,
|
---|
217 | dateAdded date,
|
---|
218 | category varchar(50),
|
---|
219 | constraint pk_products primary key (ID)
|
---|
220 | );
|
---|
221 |
|
---|
222 | create table orders(
|
---|
223 | ID SERIAL,
|
---|
224 | quantity int,
|
---|
225 | constraint pk_orders primary key(ID)
|
---|
226 | );
|
---|
227 |
|
---|
228 |
|
---|
229 | create table pets_visit_vet_centers(
|
---|
230 | id_pets bigint not null,
|
---|
231 | id_vet_centers bigint not null,
|
---|
232 | constraint pk_pets_visit_vet_centers primary key (id_pets, id_vet_centers),
|
---|
233 | constraint fk_pet_visit_vet_centers_pet foreign key (id_pets) references pets(ID) on delete cascade,
|
---|
234 | constraint fk_pet_visit_vet_centers_vetcenters foreign key (id_vet_centers) references vet_centers(ID) on delete cascade
|
---|
235 | );
|
---|
236 |
|
---|
237 |
|
---|
238 | create table product_are_made_orders(
|
---|
239 | id_products bigint not null,
|
---|
240 | id_orders bigint not null,
|
---|
241 | constraint pk_product_are_made_orders primary key (id_products, id_orders),
|
---|
242 | constraint fk_product_are_made_orders_product foreign key (id_products) references products(ID) on delete cascade,
|
---|
243 | constraint fk_product_are_made_orders_orders foreign key (id_orders) references orders(ID) on delete cascade
|
---|
244 | );
|
---|
245 |
|
---|
246 |
|
---|
247 |
|
---|
248 | create table therapy_takes_pets(
|
---|
249 | id_therapy bigint not null,
|
---|
250 | id_pets bigint not null,
|
---|
251 | constraint pk_therapy_takes_pets primary key (id_therapy, id_pets),
|
---|
252 | constraint fk_therapy_takes_pets_pets foreign key (id_pets) references pets(ID) on delete cascade,
|
---|
253 | constraint fk_therapy_takes_pets_therapy foreign key (id_therapy) references therapy(ID) on delete cascade
|
---|
254 | );
|
---|
255 |
|
---|
256 | ALTER TABLE users
|
---|
257 | ALTER COLUMN number TYPE varchar(40);
|
---|
258 |
|
---|
259 | ALTER TABLE users
|
---|
260 | ALTER COLUMN password DROP NOT NULL;
|
---|
261 |
|
---|
262 | ALTER TABLE pet_cares
|
---|
263 | ALTER COLUMN dateending type timestamp;
|
---|
264 |
|
---|
265 |
|
---|
266 |
|
---|
267 | -- ПОСЛЕДНИ ИЗМЕНИ
|
---|
268 | ALTER TABLE "diagnostics"
|
---|
269 | add column vetcenterID bigint,
|
---|
270 | add constraint fk_vetcenter foreign key (vetcenterID) references vet_centers(ID);
|
---|
271 |
|
---|
272 | create table diagnostics_established_pets(
|
---|
273 | id_diagnostics bigint not null,
|
---|
274 | id_pets bigint not null,
|
---|
275 | constraint pk_diagnostics_established_pets primary key (id_diagnostics, id_pets),
|
---|
276 | constraint fk_diagnostics_established_pets foreign key (id_pets) references pets(ID) on delete cascade,
|
---|
277 | constraint fk_diagnostics_established_therapy foreign key (id_diagnostics) references diagnostics(ID) on delete cascade
|
---|
278 | );
|
---|
279 |
|
---|
280 |
|
---|
281 | ALTER TABLE pets
|
---|
282 | add column petstatusid bigint,
|
---|
283 | add constraint fk_petstatus foreign key (petstatusid) references pet_status(ID);
|
---|
284 |
|
---|
285 | create table products_there_are_for_typepets(
|
---|
286 | id_typeofpets bigint not null,
|
---|
287 | id_products bigint not null,
|
---|
288 | constraint pk_products_there_are_for_typepets primary key (id_typeofpets, id_products),
|
---|
289 | constraint fk_products_there_are_for_type foreign key (id_typeofpets) references type_of_pets(ID) on delete cascade,
|
---|
290 | constraint fk_products_there_are_for_products foreign key (id_products) references products(ID) on delete cascade
|
---|
291 | );
|
---|
292 |
|
---|
293 | ALTER TABLE blog_post_answers
|
---|
294 | ADD COLUMN date_answers TIMESTAMP DEFAULT NOW();
|
---|
295 |
|
---|
296 |
|
---|
297 |
|
---|
298 | alter table pets
|
---|
299 | add column name varchar(50);
|
---|
300 |
|
---|
301 | ALTER TABLE type_of_pets
|
---|
302 | RENAME COLUMN name TO kind_of_pet;
|
---|
303 |
|
---|
304 | alter table pet_cares
|
---|
305 | add column start_date timestamp;
|
---|
306 |
|
---|
307 | alter table products
|
---|
308 | add column available_quantity integer;
|
---|
309 |
|
---|
310 | ALTER TABLE orders
|
---|
311 | add column userid bigint,
|
---|
312 | add constraint fk_user foreign key (userid) references users(ID);
|
---|
313 |
|
---|
314 | ALTER TABLE pets
|
---|
315 | add column petcares bigint,
|
---|
316 | add constraint petcares foreign key (petcares) references pet_cares(ID);
|
---|
317 |
|
---|
318 |
|
---|