RelationalDesign: kreiranje_izveshtai.sql

File kreiranje_izveshtai.sql, 8.1 KB (added by 185022, 6 days ago)
Line 
1set search_path = "IND0_185022_reports";
2
3drop table if exists token_;
4drop table if exists customer_weekday;
5drop table if exists unit_price;
6drop table if exists article_unit;
7drop table if exists orders;
8drop table if exists delivery;
9drop table if exists pro_forma;
10drop table if exists price;
11drop table if exists article;
12drop table if exists manufacturer;
13drop table if exists driver;
14drop table if exists manager;
15drop table if exists customer;
16drop table if exists users;
17drop table if exists vehicle;
18drop table if exists warehouse;
19drop table if exists city;
20drop table if exists region;
21drop table if exists category;
22drop table if exists weekday;
23drop table if exists pro_forma_status;
24drop table if exists delivery_status;
25drop table if exists order_status;
26
27drop schema if exists "IND0_185022_reports"
28
29create schema "IND0_185022_reports";
30
31set search_path = "IND0_185022_reports";
32
33-- ENTITIES:
34
35create table order_status
36(
37 o_status_id smallint generated by default as identity primary key,
38 o_status_name varchar(255) not null unique,
39 o_status_desc text not null
40);
41
42create table delivery_status
43(
44 d_status_id smallint generated by default as identity primary key,
45 d_status_name varchar(255) not null unique,
46 d_status_desc text not null
47);
48
49create table pro_forma_status
50(
51 pf_status_id smallint generated by default as identity primary key,
52 pf_status_name varchar(255) not null unique,
53 pf_status_desc text not null
54);
55
56create table weekday
57(
58 day_id smallint generated by default as identity primary key,
59 day_name varchar(20) not null
60);
61
62create table category
63(
64 ctg_id int generated by default as identity primary key,
65 ctg_name varchar(255) not null
66);
67
68create table region
69(
70 region_id int generated by default as identity primary key,
71 region_name varchar(255) not null
72);
73
74create table city
75(
76 city_id int generated by default as identity primary key,
77 city_name varchar(255) not null,
78 region_id int not null,
79 foreign key (region_id) references region (region_id)
80);
81
82create table warehouse
83(
84 wh_id int generated by default as identity primary key,
85 wh_adr varchar(255) not null,
86 city_id int not null,
87 foreign key (city_id) references city (city_id)
88);
89
90create table vehicle
91(
92 veh_id int generated by default as identity primary key,
93 veh_carry_weight int not null,
94 veh_service_interval smallint not null,
95 veh_kilometers int not null,
96 veh_last_service date,
97 veh_last_service_km int,
98 veh_plate varchar(8) not null,
99 veh_vin varchar(17) not null unique,
100 veh_reg_date date not null,
101 wh_id int not null,
102 foreign key (wh_id) references warehouse (wh_id)
103);
104
105create table users
106(
107 user_id bigint generated by default as identity primary key,
108 user_name varchar(255) not null,
109 user_surname varchar(255) not null,
110 user_pass varchar(255) not null,
111 user_salt varchar(255) not null,
112 user_email varchar(255) not null,
113 user_mobile varchar(255) not null,
114 user_active boolean not null,
115 user_image varchar(255),
116 clazz_ varchar(255) not null,
117 user_role varchar(255) not null,
118
119 city_id int not null,
120 foreign key (city_id) references city (city_id)
121);
122
123create table customer
124(
125 user_id bigint primary key,
126 cust_EDB varchar(13) not null,
127 cust_company_name varchar(255) not null,
128 cust_adr varchar(255) not null,
129 cust_representative_img varchar(255),
130 foreign key (user_id) references users (user_id)
131);
132
133create table manager
134(
135 user_id bigint primary key,
136 wh_id int not null,
137 foreign key (wh_id) references warehouse (wh_id),
138 foreign key (user_id) references users (user_id)
139);
140
141create table driver
142(
143 user_id bigint primary key,
144 veh_id int not null,
145 foreign key (veh_id) references vehicle (veh_id),
146 foreign key (user_id) references users (user_id)
147);
148
149create table manufacturer
150(
151 man_id bigint generated by default as identity primary key,
152 man_name varchar(255) not null,
153 man_adr varchar(255) not null,
154 man_mobile varchar(255) not null,
155 man_email varchar(255) not null
156);
157
158create table article
159(
160 art_id bigint generated by default as identity primary key,
161 art_name varchar(255) not null,
162 art_image varchar(255) not null,
163 art_weight int not null,
164 ctg_id int not null,
165 man_id bigint not null,
166 foreign key (ctg_id) references category (ctg_id),
167 foreign key (man_id) references manufacturer (man_id)
168);
169
170create table price
171(
172 price_id int generated by default as identity primary key,
173 price decimal not null,
174 price_eff_date timestamp not null default current_timestamp,
175 art_id bigint not null,
176 foreign key (art_id) references article (art_id)
177);
178
179create table pro_forma
180(
181 pf_id bigint generated by default as identity primary key,
182 pf_deadline date not null,
183 pf_date_created date not null,
184 pf_status_id smallint not null,
185 foreign key (pf_status_id) references pro_forma_status (pf_status_id)
186);
187
188create table delivery
189(
190 del_id bigint generated by default as identity primary key,
191 del_date_created date not null,
192 del_date date not null,
193 del_start_km int,
194 del_end_km int,
195 del_start_time time,
196 del_end_time time,
197 d_status_id smallint not null,
198 veh_id int not null,
199 foreign key (d_status_id) references delivery_status (d_status_id),
200 foreign key (veh_id) references vehicle (veh_id)
201);
202
203create table orders
204(
205 ord_id bigint generated by default as identity primary key,
206 ord_date date not null,
207 ord_sum int not null,
208 ord_fulfillment_date timestamp,
209 ord_comment text,
210 o_status_id smallint not null,
211 cust_id bigint not null,
212 del_id bigint,
213 pf_id bigint,
214 foreign key (o_status_id) references order_status (o_status_id),
215 foreign key (cust_id) references customer (user_id),
216 foreign key (del_id) references delivery (del_id),
217 foreign key (pf_id) references pro_forma (pf_id)
218);
219
220create table article_unit
221(
222 unit_id bigint generated by default as identity primary key,
223 unit_expiration_date date not null,
224 unit_serial_number varchar(255) not null,
225 unit_batch_number varchar(255) not null,
226 unit_manufacture_date date not null,
227 unit_cost_price decimal not null,
228 wh_id int not null,
229 ord_id bigint,
230 foreign key (wh_id) references warehouse (wh_id),
231 foreign key (ord_id) references orders (ord_id)
232);
233
234create table unit_price
235(
236 unit_id bigint not null,
237 price_id bigint not null,
238 primary key (unit_id, price_id),
239 foreign key (unit_id) references article_unit (unit_id),
240 foreign key (price_id) references price (price_id)
241);
242
243create table customer_weekday
244(
245 cust_day_id bigint generated by default as identity primary key,
246 cust_id bigint not null,
247 day_id smallint not null,
248 start_time time not null,
249 end_time time not null,
250 foreign key (cust_id) references customer (user_id),
251 foreign key (day_id) references weekday (day_id)
252);
253
254create table token_
255(
256 t_id bigint generated by default as identity primary key,
257 t_value text not null,
258 t_date timestamp not null default current_timestamp,
259 t_type varchar(255) not null,
260 t_expiry timestamp,
261 t_validated_at timestamp,
262 t_user bigint not null,
263 foreign key (t_user) references users (user_id)
264)