RelationalDesign: kreiranje3.sql

File kreiranje3.sql, 8.5 KB (added by 184006, 9 months ago)
Line 
1set
2search_path = project;
3--Create DB Shema
4drop schema if exists project cascade;
5
6create 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_pets cascade;
28drop table if exists product_are_made_orders cascade;
29drop table if exists products_available_type_of_pets cascade;
30drop table if exists therapy_takes_pets cascade;
31drop table if exists users cascade;
32drop table if exists roles cascade;
33--Create Tables
34create table cities(
35 ID SERIAL,
36 name varchar(50),
37 --adress varchar(50),
38 --phoneNumber varchar(20),
39 constraint pk_cities primary key (ID)
40);
41
42create table vet_centers(
43 ID SERIAL,
44 name varchar(50),
45 description varchar(150),
46 adress varchar(50),
47 phoneNumber varchar(15),
48 latitude decimal,
49 longitude decimal,
50 workingHours varchar(50),
51 constraint pk_vetcen primary key (ID),
52 citiesID bigint not null,
53constraint fk_cities foreign key (citiesID) references cities(ID)
54);
55
56create table jobs(
57 ID SERIAL,
58 description varchar(50),
59 predictedSalery varchar(30),
60 constraint pk_jjobs primary key (ID),
61 vetCentersID bigint not null,
62constraint fk_vetCenters foreign key ( vetCentersID) references vet_centers(ID)
63
64);
65
66create table roles(
67 ID SERIAL,
68 type varchar(15) not null unique,
69 constraint pk_roles primary key (ID)
70);
71
72 create table users(
73 ID SERIAL,
74 name varchar(30) not null,
75 lastname varchar(30) not null,
76 email varchar(30) not null unique,
77 password varchar(256) not null unique,
78 number varchar(15) not null unique,
79 role_id bigint not null,
80 jobs_id bigint default null,
81 constraint pk_users primary key (ID),
82 constraint fk_roles foreign key (role_id) references roles(ID),
83 constraint fk_jobs foreign key (jobs_id) references jobs(ID)
84
85);
86
87 create table blog_post_for_consultations(
88ID SERIAL,
89date_askes date,
90title varchar(30),
91description varchar(150),
92users_id bigint not null,
93constraint pk_blogPFC primary key (ID),
94constraint fk_users foreign key (users_id) references users(ID)
95);
96create table blog_post_answers(
97ID SERIAL,
98parent_id bigint,
99reply varchar(150),
100blogPostConsID bigint not null,
101usersID bigint not null,
102constraint pk_blogPA primary key (ID),
103constraint fk_blogBPFC foreign key (blogPostConsID) references blog_post_for_consultations(ID),
104constraint fk_users foreign key (usersID) references users(ID)
105);
106
107create table pet_cares(
108ID SERIAL,
109title varchar(50),
110description varchar(150),
111dateending date not null,
112constraint pk_petCares primary key (ID),
113usersID bigint not null,
114vetcentersID bigint not null,
115constraint fk_blogBPFC foreign key (usersID) references users(ID),
116constraint fk_vetCenters foreign key (vetcentersID) references vet_centers(ID)
117);
118
119create table type_of_pets(
120 ID SERIAL,
121 description varchar(30),
122 name varchar(30),
123 constraint pk_typeOfPets primary key (ID)
124
125);
126
127create table pets(
128 ID SERIAL,
129 color varchar(20),
130 description varchar(50),
131 dateOfBirthday date,
132 constraint pk_pets primary key (ID),
133 usersID bigint not null,
134 typeOfPetsID bigint not null,
135 constraint fk_typeOfPets foreign key (typeOfPetsID) references type_of_pets(ID),
136 constraint fk_users foreign key (usersID) references users(ID)
137);
138
139create table pet_galery(
140 ID SERIAL,
141 picture varchar(1000),
142 constraint pk_petGalery primary key (ID),
143 petsID bigint not null,
144 constraint fk_pets foreign key (petsID) references pets(ID)
145);
146create table reports(
147 ID SERIAL,
148 description varchar(150),
149 constraint pk_reports primary key (ID),
150 usersID bigint not null,
151 petsID bigint not null,
152 constraint fk_users foreign key (usersID) references users(ID),
153 constraint fk_pets foreign key (petsID) references pets(ID)
154);
155
156create table diagnostics(
157 ID SERIAL,
158 description varchar(200),
159 constraint pk_diagnostics primary key (ID),
160 usersID bigint not null,
161 constraint fk_users foreign key (usersID) references users(ID)
162);
163
164create table manufacturers(
165 ID SERIAL,
166 name varchar(50) not null,
167 description varchar(200),
168 city varchar(50),
169 state varchar(50),
170 constraint pk_manufacturers primary key (ID)
171
172);
173
174create table medecines(
175 ID SERIAL,
176 name varchar(50) not null,
177 description varchar(200),
178 constraint pk_medecines primary key (ID),
179 diagnosticsID bigint not null,
180 manufacturersID bigint not null,
181 constraint fk_diagnostics foreign key (diagnosticsID) references diagnostics(ID),
182 constraint fk_manufacturers foreign key (manufacturersID) references manufacturers(ID)
183);
184
185create table pet_status(
186 ID SERIAL,
187 type varchar(30),
188 node varchar(200),
189 constraint pk_pet_status primary key (ID)
190);
191
192create table therapy(
193 ID SERIAL,
194 description varchar(150),
195 appoitmentDate date,
196 constraint pk_therapy primary key (ID),
197 diagnosticsID bigint not null,
198 pet_statusID bigint not null,
199 constraint fk_pet_status foreign key (pet_statusID) references pet_status(ID),
200 constraint fk_diagnostics foreign key (diagnosticsID) references diagnostics(ID)
201);
202
203create table breeds(
204ID SERIAL,
205name varchar(50),
206constraint pk_breeds primary key(ID),
207constraint fk_type_of_pets foreign key (ID) references type_of_pets(ID) on delete cascade
208);
209
210create table products(
211 ID SERIAL,
212 name varchar(50),
213 description varchar(150),
214 price varchar(50),
215 isActive bool,
216 dateAdded date,
217 category varchar(50),
218 constraint pk_products primary key (ID)
219);
220
221create table orders(
222ID SERIAL,
223quantity int,
224constraint pk_orders primary key(ID)
225);
226
227
228create table pets_visit_vet_centers(
229id_pets bigint not null,
230id_vet_centers bigint not null,
231constraint pk_pets_visit_vet_centers primary key (id_pets, id_vet_centers),
232constraint fk_pet_visit_vet_centers_pet foreign key (id_pets) references pets(ID) on delete cascade,
233constraint fk_pet_visit_vet_centers_vetcenters foreign key (id_vet_centers) references vet_centers(ID) on delete cascade
234);
235
236create table products_there_are_for_pets(
237id_products bigint not null,
238id_pets bigint not null,
239constraint pk_products_there_are_for_pets primary key (id_products, id_pets),
240constraint fk_products_there_are_for_pets_pets foreign key (id_pets) references pets(ID) on delete cascade,
241constraint fk_products_there_are_for_pets_products foreign key (id_products) references products(ID) on delete cascade
242);
243
244create table product_are_made_orders(
245id_products bigint not null,
246id_orders bigint not null,
247constraint pk_product_are_made_orders primary key (id_products, id_orders),
248constraint fk_product_are_made_orders_product foreign key (id_products) references products(ID) on delete cascade,
249constraint fk_product_are_made_orders_orders foreign key (id_orders) references orders(ID) on delete cascade
250);
251
252create table products_available_type_of_pets(
253id_products bigint not null,
254id_type_of_pets bigint not null,
255constraint pk_products_available_type_of_pets primary key (id_products, id_type_of_pets),
256constraint fk_products_available_type_of_pets_type_of_pets foreign key (id_type_of_pets) references type_of_pets(ID) on delete cascade,
257constraint fk_products_available_type_of_pets_products foreign key (id_products) references products(ID) on delete cascade
258);
259
260create table therapy_takes_pets(
261id_therapy bigint not null,
262id_pets bigint not null,
263constraint pk_therapy_takes_pets primary key (id_therapy, id_pets),
264constraint fk_therapy_takes_pets_pets foreign key (id_pets) references pets(ID) on delete cascade,
265constraint fk_therapy_takes_pets_therapy foreign key (id_therapy) references therapy(ID) on delete cascade
266);
267
268ALTER TABLE users
269ALTER COLUMN number TYPE varchar(40);
270
271ALTER TABLE users
272ALTER COLUMN password DROP NOT NULL;
273
274ALTER TABLE pet_cares
275ALTER COLUMN dateending type timestamp;