RelationalDesign: kreiranje_v1.sql

File kreiranje_v1.sql, 5.6 KB (added by 201171, 8 months ago)
Line 
1drop table if exists user_table cascade;
2drop table if exists administrator cascade;
3drop table if exists delivery_man cascade;
4drop table if exists service_man cascade;
5drop table if exists customer cascade;
6drop table if exists warehouseman cascade;
7drop table if exists warehouse cascade;
8drop table if exists vehicle cascade;
9drop table if exists delivery cascade;
10drop table if exists review cascade;
11drop table if exists order_table cascade;
12drop table if exists manufacturer cascade;
13drop table if exists category cascade;
14drop table if exists subcategory cascade;
15drop table if exists product cascade;
16drop table if exists order_table_contains_product cascade;
17drop table if exists product_is_in_category cascade;
18drop table if exists product_is_in_stock_in_warehouse cascade;
19drop table if exists vehicle_is_used_for_delivery cascade;
20
21
22create table user_table (
23 user_id serial primary key,
24 username varchar(100) unique not null,
25 name_user varchar(100) not null,
26 email varchar(100) unique not null,
27 password varchar(100) not null,
28 phone_number varchar(100),
29 administrator_id integer
30);
31
32create table administrator(
33 user_id serial primary key,
34
35 constraint user_id_fk foreign key(user_id)
36 references user_table(user_id)
37);
38
39alter table user_table add
40 constraint administrator_id_fk foreign key(administrator_id)
41 references administrator(user_id);
42
43create table delivery_man(
44 user_id serial primary key,
45
46 constraint user_id_fk foreign key(user_id)
47 references user_table(user_id)
48);
49
50create table service_man(
51 user_id serial primary key,
52
53 constraint user_id_fk foreign key(user_id)
54 references user_table(user_id)
55);
56
57create table customer(
58 user_id serial primary key,
59
60 constraint user_id_fk foreign key(user_id)
61 references user_table(user_id)
62);
63
64create table warehouse(
65 warehouse_id serial primary key,
66 warehouse_location varchar(100) not null
67);
68
69create table warehouseman(
70 user_id serial primary key,
71 warehouse_id integer not null,
72
73 constraint user_id_fk foreign key(user_id)
74 references user_table(user_id),
75 constraint warehouse_id_fk foreign key(warehouse_id)
76 references warehouse(warehouse_id)
77);
78
79
80create table order_table (
81 order_id serial primary key,
82 order_status varchar(100) not null,
83 order_date Date not null,
84 customer_id integer not null,
85
86 constraint customer_id_fk foreign key(customer_id)
87 references customer(user_id)
88);
89
90
91create table vehicle (
92 vehicle_id serial primary key,
93 vehicle_model varchar(100),
94 vehicle_service Date,
95 delivery_man_id integer not null,
96
97 constraint delivery_man_id_fk foreign key(delivery_man_id)
98 references delivery_man(user_id)
99);
100
101create table delivery (
102 delivery_id serial primary key,
103 delivery_status varchar(100) not null,
104 delivery_address varchar(100) not null,
105 delivery_man_id integer not null,
106 order_id integer not null,
107
108 constraint delivery_man_id_fk foreign key(delivery_man_id)
109 references delivery_man(user_id),
110 constraint order_id_fk foreign key(order_id)
111 references order_table(order_id)
112);
113
114
115
116create table category (
117 category_id serial primary key,
118 category_name varchar(100) not null
119);
120
121
122create table subcategory (
123 subcategory_id serial primary key,
124 subcategory_name varchar(100) not null,
125 category_id integer not null,
126
127 constraint category_id_fk foreign key(category_id)
128 references category(category_id)
129);
130
131create table manufacturer (
132 manufacturer_id serial primary key,
133 manufacturer_name varchar(100)
134);
135
136create table product (
137 product_id serial primary key,
138 product_name varchar(100) not null,
139 product_description varchar(100) not null,
140 product_price integer not null,
141 product_warranty integer not null,
142 product_image varchar not null,
143 manufacturer_id integer not null,
144 service_man_id integer not null,
145
146
147 constraint manufacturer_id_fk foreign key(manufacturer_id)
148 references manufacturer(manufacturer_id),
149 constraint service_man_fk foreign key(service_man_id)
150 references service_man(user_id)
151);
152
153create table review (
154 review_id serial primary key,
155 review_rating integer not null,
156 review_description varchar(100) not null,
157 customer_id integer not null,
158 product_id integer not null,
159
160 constraint customer_id_fk foreign key(customer_id)
161 references customer(user_id),
162 constraint product_id_fk foreign key(product_id)
163 references product(product_id)
164
165);
166
167create table vehicle_is_used_for_delivery (
168 vehicle_id integer not null,
169 delivery_id integer not null,
170
171 constraint vehicle_id_fk foreign key(vehicle_id)
172 references vehicle(vehicle_id),
173 constraint delivery_id_fk foreign key(delivery_id)
174 references delivery(delivery_id)
175);
176
177create table product_is_in_stock_in_warehouse (
178 product_id integer not null,
179 warehouse_id integer not null,
180 quantity integer not null,
181
182 constraint product_id_fk foreign key(product_id)
183 references product(product_id),
184 constraint warehouse_id_fk foreign key(warehouse_id)
185 references warehouse(warehouse_id)
186);
187
188create table order_table_contains_product (
189 order_id integer not null,
190 product_id integer not null,
191 quantity integer not null,
192
193 constraint order_id_fk foreign key(order_id)
194 references order_table(order_id),
195 constraint product_id_fk foreign key(product_id)
196 references product(product_id)
197);
198
199create table product_is_in_category (
200 product_id integer not null,
201 category_id integer not null,
202
203 constraint product_id_fk foreign key(product_id)
204 references product(product_id),
205 constraint category_id_fk foreign key(category_id)
206 references category(category_id)
207);
208