RelationalDesign: kreiranje4.sql

File kreiranje4.sql, 9.4 KB (added by 184006, 7 months ago)
Line 
1set
2search_path = project;
3--Create DB Shema
4--drop schema if exists project cascade;
5
6--create schema project;
7
8drop table if exists cities cascade;
9drop table if exists vet_centers cascade;
10drop table if exists jobs cascade;
11drop table if exists blog_post_for_consultations cascade;
12drop table if exists blog_post_answers cascade;
13drop table if exists pet_cares cascade;
14drop table if exists type_of_pets cascade;
15drop table if exists pets cascade;
16drop table if exists pet_galery cascade;
17drop table if exists reports cascade;
18drop table if exists diagnostics cascade;
19drop table if exists manufacturers cascade;
20drop table if exists medecines cascade;
21drop table if exists pet_status cascade;
22drop table if exists therapy cascade;
23drop table if exists breeds cascade;
24drop table if exists products cascade;
25drop table if exists orders cascade;
26drop table if exists pets_visit_vet_centers cascade;
27drop table if exists products_there_are_for_typepets cascade;
28drop table if exists product_are_made_orders cascade;
29--drop table if exists products_available_type_of_pets cascade;
30drop table if exists therapy_takes_pets cascade;
31drop table if exists diagnostics_established_pets cascade;
32drop table if exists users cascade;
33drop table if exists roles cascade;
34--Create Tables
35create 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
43create 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,
54constraint fk_cities foreign key (citiesID) references cities(ID)
55);
56
57create 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,
63constraint fk_vetCenters foreign key ( vetCentersID) references vet_centers(ID)
64
65);
66
67create 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(
89ID SERIAL,
90date_askes date,
91title varchar(30),
92description varchar(150),
93users_id bigint not null,
94constraint pk_blogPFC primary key (ID),
95constraint fk_users foreign key (users_id) references users(ID)
96);
97create table blog_post_answers(
98ID SERIAL,
99parent_id bigint,
100reply varchar(150),
101blogPostConsID bigint not null,
102usersID bigint not null,
103constraint pk_blogPA primary key (ID),
104constraint fk_blogBPFC foreign key (blogPostConsID) references blog_post_for_consultations(ID),
105constraint fk_users foreign key (usersID) references users(ID)
106);
107
108create table pet_cares(
109ID SERIAL,
110title varchar(50),
111description varchar(150),
112dateending date not null,
113constraint pk_petCares primary key (ID),
114usersID bigint not null,
115vetcentersID bigint not null,
116constraint fk_blogBPFC foreign key (usersID) references users(ID),
117constraint fk_vetCenters foreign key (vetcentersID) references vet_centers(ID)
118);
119
120create 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
128create 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
140create 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);
147create 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
157create 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
165create 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
175create 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
186create table pet_status(
187 ID SERIAL,
188 type varchar(30),
189 node varchar(200),
190 constraint pk_pet_status primary key (ID)
191);
192
193create 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
204create table breeds(
205ID SERIAL,
206name varchar(50),
207constraint pk_breeds primary key(ID),
208constraint fk_type_of_pets foreign key (ID) references type_of_pets(ID) on delete cascade
209);
210
211create 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
222create table orders(
223ID SERIAL,
224quantity int,
225constraint pk_orders primary key(ID)
226);
227
228
229create table pets_visit_vet_centers(
230id_pets bigint not null,
231id_vet_centers bigint not null,
232constraint pk_pets_visit_vet_centers primary key (id_pets, id_vet_centers),
233constraint fk_pet_visit_vet_centers_pet foreign key (id_pets) references pets(ID) on delete cascade,
234constraint fk_pet_visit_vet_centers_vetcenters foreign key (id_vet_centers) references vet_centers(ID) on delete cascade
235);
236
237
238create table product_are_made_orders(
239id_products bigint not null,
240id_orders bigint not null,
241constraint pk_product_are_made_orders primary key (id_products, id_orders),
242constraint fk_product_are_made_orders_product foreign key (id_products) references products(ID) on delete cascade,
243constraint fk_product_are_made_orders_orders foreign key (id_orders) references orders(ID) on delete cascade
244);
245
246
247
248create table therapy_takes_pets(
249id_therapy bigint not null,
250id_pets bigint not null,
251constraint pk_therapy_takes_pets primary key (id_therapy, id_pets),
252constraint fk_therapy_takes_pets_pets foreign key (id_pets) references pets(ID) on delete cascade,
253constraint fk_therapy_takes_pets_therapy foreign key (id_therapy) references therapy(ID) on delete cascade
254);
255
256ALTER TABLE users
257ALTER COLUMN number TYPE varchar(40);
258
259ALTER TABLE users
260ALTER COLUMN password DROP NOT NULL;
261
262ALTER TABLE pet_cares
263ALTER COLUMN dateending type timestamp;
264
265
266
267-- ПОСЛЕДНИ ИЗМЕНИ
268ALTER TABLE "diagnostics"
269add column vetcenterID bigint,
270add constraint fk_vetcenter foreign key (vetcenterID) references vet_centers(ID);
271
272create table diagnostics_established_pets(
273id_diagnostics bigint not null,
274id_pets bigint not null,
275constraint pk_diagnostics_established_pets primary key (id_diagnostics, id_pets),
276constraint fk_diagnostics_established_pets foreign key (id_pets) references pets(ID) on delete cascade,
277constraint fk_diagnostics_established_therapy foreign key (id_diagnostics) references diagnostics(ID) on delete cascade
278);
279
280
281ALTER TABLE pets
282add column petstatusid bigint,
283add constraint fk_petstatus foreign key (petstatusid) references pet_status(ID);
284
285create table products_there_are_for_typepets(
286id_typeofpets bigint not null,
287id_products bigint not null,
288constraint pk_products_there_are_for_typepets primary key (id_typeofpets, id_products),
289constraint fk_products_there_are_for_type foreign key (id_typeofpets) references type_of_pets(ID) on delete cascade,
290constraint fk_products_there_are_for_products foreign key (id_products) references products(ID) on delete cascade
291);
292
293ALTER TABLE blog_post_answers
294ADD COLUMN date_answers TIMESTAMP DEFAULT NOW();
295
296
297
298alter table pets
299add column name varchar(50);
300
301ALTER TABLE type_of_pets
302RENAME COLUMN name TO kind_of_pet;
303
304alter table pet_cares
305add column start_date timestamp;
306
307alter table products
308add column available_quantity integer;
309
310ALTER TABLE orders
311add column userid bigint,
312add constraint fk_user foreign key (userid) references users(ID);
313
314ALTER TABLE pets
315add column petcares bigint,
316add constraint petcares foreign key (petcares) references pet_cares(ID);
317
318