RelationalDesign: kreiranje.sql

File kreiranje.sql, 4.5 KB (added by 185022, 7 days ago)
Line 
1drop table if exists IN_STOCK;
2drop table if exists ARTICLE_UNIT;
3drop table if exists ORDERS;
4drop table if exists DELIVERY;
5drop table if exists PRO_FORMA;
6drop table if exists ARTICLE;
7drop table if exists DRIVER;
8drop table if exists VEHICLE;
9drop table if exists MANAGER;
10drop table if exists WAREHOUSE;
11drop table if exists CUSTOMER;
12drop table if exists USERS;
13drop table if exists CITY;
14drop table if exists CATEGORY;
15
16drop type if exists deliveryStatus;
17drop type if exists proFormaStatus;
18drop type if exists orderStatus;
19
20drop schema if exists "IND0_185022";
21
22create schema "IND0_185022";
23
24set search_path = "IND0_185022";
25
26-- ENUMERATIONS:
27
28create type orderStatus as enum (
29 'PENDING',
30 'SHIPPED',
31 'OUT_FOR_DELIVERY',
32 'DELIVERED',
33 'CANCELED',
34 'FAILED'
35);
36
37create type proFormaStatus as enum(
38 'PENDING',
39 'PAID',
40 'CANCELLED',
41 'EXPIRED'
42);
43
44create type deliveryStatus as enum(
45 'PREPARING',
46 'IN_TRANSIT',
47 'DELIVERED',
48 'DELAYED',
49 'RETURNED',
50 'CANCELLED'
51);
52
53-- ENTITIES:
54
55create table CATEGORY(
56 category_id int primary key,
57 category_name varchar(255) not null
58);
59
60create table CITY(
61 city_id int primary key,
62 city_name varchar(255) not null
63);
64
65create table USERS(
66 user_id bigint primary key,
67 user_name varchar(255) not null,
68 user_surname varchar(255) not null,
69 user_email varchar(255) not null,
70 user_mobile varchar(255) not null,
71 city_id int,
72 foreign key (city_id) references CITY(city_id)
73);
74
75create table CUSTOMER(
76 user_id bigint primary key,
77 customer_EDB int not null,
78 customer_company_name varchar(255) not null,
79 customer_address varchar(255) not null,
80 customer_open_time time not null,
81 customer_close_time time not null,
82 customer_representative_img varchar(255) not null,
83 foreign key (user_id) references USERS(user_id)
84);
85
86create table WAREHOUSE(
87 warehouse_id int primary key,
88 warehouse_address varchar(255) not null,
89 city_id int not null,
90 foreign key (city_id) references CITY(city_id)
91);
92
93create table MANAGER(
94 user_id bigint primary key,
95 warehouse_id int,
96 foreign key (warehouse_id) references WAREHOUSE(warehouse_id),
97 foreign key (user_id) references USERS(user_id)
98);
99
100create table VEHICLE(
101 vehicle_id int primary key,
102 vehicle_carry_weight int not null,
103 vehicle_service_interval smallint not null,
104 vehicle_kilometers int not null,
105 vehicle_last_service date,
106 warehouse_id int not null,
107 foreign key (warehouse_id) references WAREHOUSE(warehouse_id)
108);
109
110create table DRIVER(
111 user_id bigint primary key,
112 vehicle_id int not null,
113 foreign key (vehicle_id) references VEHICLE(vehicle_id),
114 foreign key (user_id) references USERS(user_id)
115);
116
117create table ARTICLE(
118 article_id bigint primary key,
119 article_name varchar(255) not null,
120 article_image varchar(255) not null,
121 article_weight int not null,
122 article_price decimal not null,
123 category_id int not null,
124 foreign key (category_id) references CATEGORY(category_id)
125);
126
127create table PRO_FORMA(
128 pro_forma_id bigint primary key,
129 pro_forma_status proFormaStatus not null,
130 pro_forma_deadline date not null,
131 pro_forma_date_created date not null
132);
133
134create table DELIVERY(
135 delivery_id bigint primary key,
136 delivery_status deliveryStatus not null,
137 delivery_date_created date not null,
138 delivery_date date not null,
139 vehicle_id int not null,
140 foreign key (vehicle_id) references VEHICLE(vehicle_id)
141);
142
143create table ORDERS(
144 order_id bigint primary key,
145 order_date date not null,
146 order_status orderStatus not null,
147 order_sum int not null,
148 order_fulfillment_date timestamp,
149 customer_id bigint,
150 delivery_id bigint,
151 pro_forma_id bigint,
152 foreign key (customer_id) references CUSTOMER(user_id),
153 foreign key (delivery_id) references DELIVERY(delivery_id),
154 foreign key (pro_forma_id) references PRO_FORMA(pro_forma_id)
155);
156
157create table ARTICLE_UNIT(
158 unit_id bigint primary key,
159 unit_expiration_date date not null,
160 unit_serial_number varchar(255) not null,
161 unit_batch_number varchar(255) not null,
162 unit_manufacture_date date not null,
163 unit_cost_price decimal not null,
164 article_id bigint not null,
165 order_id bigint,
166 foreign key (article_id) references ARTICLE(article_id),
167 foreign key (order_id) references ORDERS(order_id)
168);
169
170
171-- RELATIONS:
172
173create table IN_STOCK(
174 article_id bigint,
175 warehouse_id int,
176 foreign key (article_id) references ARTICLE_UNIT(unit_id),
177 foreign key (warehouse_id) references WAREHOUSE(warehouse_id),
178 primary key (article_id, warehouse_id)
179);