RelationModel: kreiranje_v1.sql

File kreiranje_v1.sql, 7.9 KB (added by 201084, 2 years ago)
Line 
1drop table if exists users_table cascade;
2drop table if exists client cascade;
3drop table if exists delivery_man cascade;
4drop table if exists warehouseman cascade;
5drop table if exists administrator cascade;
6drop table if exists review cascade;
7drop table if exists repair cascade;
8drop table if exists repair_shop cascade;
9drop table if exists car_manufacturer cascade;
10drop table if exists car cascade;
11drop table if exists car_sample cascade;
12drop table if exists service_book cascade;
13drop table if exists order_table cascade;
14drop table if exists part_manufacturer cascade;
15drop table if exists part cascade;
16drop table if exists delivery cascade;
17drop table if exists warehouse cascade;
18drop table if exists price cascade;
19drop table if exists category cascade;
20drop table if exists part_is_from_category cascade;
21drop table if exists repair_shop_is_authorized_for_car_make cascade;
22drop table if exists order_contains_part cascade;
23drop table if exists part_is_in_stock_in_warehouse cascade;
24drop table if exists part_is_appropriate_for_car cascade;
25
26create table users_table(
27 ID_user serial primary key,
28 username varchar(100) unique not null,
29 email varchar(100) unique not null,
30 name_user varchar(100) not null,
31 password_user varchar(100) not null,
32 user_created_on timestamp default now(),
33 ID_administrator integer
34 -- constraint for fk_user_by_administrator added later
35
36);
37
38create table client(
39 ID_user integer primary key,
40 phone_number_client varchar(100),
41 constraint fk_client_is_user foreign key (ID_user) references users_table(ID_user)
42);
43
44create table delivery_man(
45 ID_user integer primary key,
46 employed_from date not null,
47 constraint fk_delivery_man_is_user foreign key (ID_user) references users_table(ID_user)
48);
49
50create table warehouseman(
51 ID_user integer primary key,
52 employed_from date not null,
53 ID_warehouse integer not null,
54 constraint fk_warehouseman_is_user foreign key (ID_user) references users_table(ID_user)
55 -- contraint for fk_warehouseman_warehouse added later
56);
57
58create table administrator(
59 ID_user integer primary key,
60 constraint fk_administrator_is_user foreign key (ID_user) references users_table(ID_user)
61);
62
63alter table users_table add constraint fk_user_by_administrator foreign key (ID_administrator)
64 references administrator(ID_user);
65
66create table warehouse(
67 ID_warehouse serial primary key,
68 warehouse_location varchar(100) not null
69);
70
71alter table warehouseman add constraint fk_warehouseman_warehouse foreign key (ID_warehouse)
72 references warehouse(ID_warehouse);
73
74create table car_manufacturer(
75 ID_car_manufacturer serial primary key,
76 cm_name varchar(100) not null,
77 cm_country varchar(100)
78);
79
80create table car(
81 ID_car serial primary key,
82 in_production_since date not null,
83 in_production_till date not null,
84 car_type varchar(100) not null,
85 ID_car_manufacturer integer not null,
86 constraint fk_car_car_manufacturer foreign key (ID_car_manufacturer) references car_manufacturer(ID_car_manufacturer)
87);
88
89create table car_sample(
90 vin integer primary key,
91 year_of_production integer not null,
92 engine_power integer not null,
93 displacement integer not null,
94 fuel_type varchar(100) not null,
95 km_driven integer not null,
96 ID_user integer not null,
97 ID_car integer not null,
98 constraint fk_car_sample_user foreign key (ID_user) references client(ID_user),
99 constraint fk_car_sample_car foreign key (ID_car) references car(ID_car)
100);
101
102create table service_book(
103 ID_service_book serial primary key,
104 sb_created_on timestamp default now(),
105 vin integer not null,
106 constraint fk_service_book_car_sample foreign key (vin) references car_sample(vin)
107);
108
109create table repair_shop(
110 ID_repair_shop serial primary key,
111 rs_name varchar(100) not null,
112 rs_location varchar(100) not null,
113 rs_phone_number varchar(100) not null
114);
115
116create table repair(
117 ID_repair serial primary key,
118 vin integer not null,
119 ID_repair_shop integer not null,
120 ID_service_book integer,
121 constraint fk_repair_car_sample foreign key (vin) references car_sample(vin),
122 constraint fk_repair_repair_shop foreign key (ID_repair_shop) references repair_shop(ID_repair_shop),
123 constraint fk_repair_service_book foreign key (ID_service_book) references service_book(ID_service_book)
124);
125
126create table review(
127 ID_review serial primary key,
128 review_rating integer not null,
129 review_comment varchar(300),
130 ID_repair integer not null,
131 ID_user integer not null,
132 constraint fk_review_repair foreign key (ID_repair) references repair(ID_repair),
133 constraint fk_review_user foreign key (ID_user) references client(ID_user)
134);
135
136create table order_table(
137 ID_order serial primary key,
138 order_status varchar(100) not null,
139 order_date timestamp default now(),
140 ID_user integer not null,
141 constraint fk_order_user foreign key (ID_user) references client(ID_user)
142);
143
144create table part_manufacturer(
145 ID_part_manufacturer serial primary key,
146 pm_name varchar(100) not null,
147 pm_location varchar(100)
148);
149
150create table part(
151 ID_part serial primary key,
152 part_name varchar(100) not null,
153 part_description varchar(300),
154 ID_part_manufacturer integer not null,
155 constraint fk_part_part_manufacturer foreign key(ID_part_manufacturer) references part_manufacturer(ID_part_manufacturer)
156);
157
158create table delivery(
159 ID_delivery serial primary key,
160 delivery_status varchar(100) not null,
161 delivery_address varchar(100) not null,
162 ID_user integer not null,
163 ID_order integer not null,
164 constraint fk_delivered_by_deliver_man foreign key(ID_user) references delivery_man(ID_user),
165 constraint fk_delivery_for_order foreign key(ID_order) references order_table(ID_order)
166);
167
168create table price(
169 ID_price serial primary key,
170 amount integer not null,
171 price_from date not null,
172 price_till date,
173 ID_part integer not null,
174 constraint fk_price_for_part foreign key(ID_part) references part(ID_part),
175 constraint ck_amount_gt_0 check (amount>0)
176);
177
178create table category(
179 ID_category serial primary key,
180 category_name varchar(100) not null,
181 ID_parent_category integer,
182 constraint fk_category_parentcategory foreign key (ID_parent_category) references category(ID_category)
183);
184
185create table part_is_from_category(
186 ID_part integer not null,
187 ID_category integer not null,
188 constraint fk_part_is_from_category_part foreign key (ID_part) references part(ID_part),
189 constraint fk_part_is_from_category_category foreign key (ID_category) references category(ID_category)
190
191);
192
193create table repair_shop_is_authorized_for_car_make(
194 ID_repair_shop integer not null,
195 ID_car_manufacturer integer not null,
196 constraint fk_repair_shop_is_authorized_for_car_make_repair_shop foreign key (ID_repair_shop) references repair_shop(ID_repair_shop),
197 constraint fk_repair_shop_is_authorized_for_car_make_car_manufacturer foreign key (ID_car_manufacturer) references car_manufacturer(ID_car_manufacturer)
198);
199
200create table order_contains_part(
201 ID_part integer not null,
202 ID_order integer not null,
203 quantity_order integer not null,
204 constraint fk_order_contains_part_part foreign key (ID_part) references part(ID_part),
205 constraint fk_order_contains_part_order foreign key (ID_order) references order_table(ID_order),
206 constraint ck_quantity_order check (quantity_order>0)
207);
208
209create table part_is_in_stock_in_warehouse(
210 ID_part integer not null,
211 ID_warehouse integer not null,
212 quantity_warehouse integer not null,
213 constraint fk_part_is_in_stock_in_warehouse_part foreign key (ID_part) references part(ID_part),
214 constraint fk_part_is_in_stock_in_warehouse_warehouse foreign key (ID_warehouse) references warehouse(ID_warehouse),
215 constraint ck_quantity_warehouse check (quantity_warehouse>0)
216);
217
218create table part_is_appropriate_for_car(
219 ID_part integer not null,
220 ID_car integer not null,
221 constraint fk_part_is_appropriate_for_car_part foreign key (ID_part) references part(ID_part),
222 constraint fk_part_is_appropriate_for_car_car foreign key (ID_car) references car(ID_car)
223);