Логички и физички дизајн: kreiranje.sql

File kreiranje.sql, 8.3 KB (added by 186102, 3 years ago)
Line 
1-- DROP SCHEMA project;
2
3CREATE SCHEMA project AUTHORIZATION db_202122z_va_prj_pcpicco_owner;
4
5CREATE TABLE project."Configuration" (
6 config_id int4 NOT NULL,
7 "name" varchar(64) NOT NULL,
8 price int4 NOT NULL,
9 "type" varchar(64) NOT NULL,
10 CONSTRAINT configuration_pk PRIMARY KEY (config_id)
11);
12
13CREATE TABLE project."User" (
14 user_id int4 NOT NULL,
15 "Password" varchar(64) NOT NULL,
16 CONSTRAINT user_pk PRIMARY KEY (user_id)
17);
18
19CREATE TABLE project.store (
20 store_id varchar(64) NOT NULL,
21 locations varchar(64) NOT NULL,
22 contact varchar(64) NOT NULL,
23 store_name varchar NULL,
24 CONSTRAINT store_pk PRIMARY KEY (store_id)
25);
26
27
28CREATE TABLE project."admin" (
29 admin_id int4 NOT NULL,
30 CONSTRAINT admin_pk PRIMARY KEY (admin_id),
31 CONSTRAINT admin_fk FOREIGN KEY (admin_id) REFERENCES project."User"(user_id)
32);
33
34
35CREATE TABLE project.client (
36 client_id int4 NOT NULL,
37 address varchar(64) NOT NULL,
38 "name" varchar(64) NOT NULL,
39 "e-mail" varchar(64) NOT NULL,
40 CONSTRAINT client_pk PRIMARY KEY (client_id),
41 CONSTRAINT client_fk FOREIGN KEY (client_id) REFERENCES project."User"(user_id)
42);
43
44CREATE TABLE project.distributor (
45 distributor_id int4 NOT NULL,
46 "name" varchar(64) NOT NULL,
47 surname varchar(64) NOT NULL,
48 contact_number int4 NOT NULL,
49 CONSTRAINT distributor_pk PRIMARY KEY (distributor_id),
50 CONSTRAINT distributor_fk FOREIGN KEY (distributor_id) REFERENCES project."User"(user_id)
51);
52
53
54CREATE TABLE project.manages_configuration (
55 config_id int4 NOT NULL,
56 admin_id int4 NOT NULL,
57 CONSTRAINT manages_configuration_pk PRIMARY KEY (config_id, admin_id),
58 CONSTRAINT manages_configuration_fk FOREIGN KEY (admin_id) REFERENCES project.admin(admin_id),
59 CONSTRAINT manages_configuration_fk_1 FOREIGN KEY (config_id) REFERENCES project."Configuration"(config_id)
60);
61
62CREATE TABLE project.product (
63 prod_id int4 NOT NULL,
64 availability bool NOT NULL,
65 "name" varchar(64) NOT NULL,
66 description varchar(64) NOT NULL,
67 price int4 NOT NULL,
68 "type" varchar(64) NOT NULL,
69 quantity int4 NOT NULL,
70 store_name varchar(64) NOT NULL,
71 CONSTRAINT product_pk PRIMARY KEY (prod_id),
72 CONSTRAINT product_fk FOREIGN KEY (store_name) REFERENCES project.store(store_id)
73);
74
75CREATE TABLE project.seller_admin (
76 admin_id int4 NOT NULL,
77 seller_id int4 NOT NULL,
78 store_name varchar(64) NOT NULL,
79 "name" varchar(64) NOT NULL,
80 surname varchar(64) NOT NULL,
81 "e-mail" varchar(64) NOT NULL,
82 CONSTRAINT seller_admin_pk PRIMARY KEY (seller_id),
83 CONSTRAINT seller_admin_fk FOREIGN KEY (admin_id) REFERENCES project.admin(admin_id),
84 CONSTRAINT seller_admin_fk_1 FOREIGN KEY (seller_id) REFERENCES project."User"(user_id),
85 CONSTRAINT seller_admin_fk_2 FOREIGN KEY (store_name) REFERENCES project.store(store_id)
86);
87
88
89CREATE TABLE project.updates (
90 seller_id int4 NOT NULL,
91 product_id int4 NOT NULL,
92 time_stamp date NOT NULL,
93 CONSTRAINT updates_pk PRIMARY KEY (seller_id, product_id),
94 CONSTRAINT updates_fk FOREIGN KEY (seller_id) REFERENCES project.seller_admin(seller_id),
95 CONSTRAINT updates_fk_1 FOREIGN KEY (product_id) REFERENCES project.product(prod_id)
96);
97
98CREATE TABLE project.view_product (
99 client_id int4 NOT NULL,
100 prod_id int4 NOT NULL,
101 time_stamp date NOT NULL,
102 CONSTRAINT view_product_pk PRIMARY KEY (prod_id, client_id),
103 CONSTRAINT view_product_fk FOREIGN KEY (client_id) REFERENCES project.client(client_id),
104 CONSTRAINT view_product_fk_1 FOREIGN KEY (prod_id) REFERENCES project.product(prod_id)
105);
106
107
108CREATE TABLE project.wishlist (
109 wishlist_id int4 NOT NULL,
110 client_id int4 NOT NULL,
111 CONSTRAINT wishlist_pk PRIMARY KEY (wishlist_id, client_id),
112 CONSTRAINT wishlist_fk FOREIGN KEY (client_id) REFERENCES project.client(client_id)
113);
114
115CREATE TABLE project.cart (
116 cart_id int4 NOT NULL,
117 client_id int4 NOT NULL,
118 CONSTRAINT cart_pk PRIMARY KEY (cart_id),
119 CONSTRAINT cart_fk FOREIGN KEY (client_id) REFERENCES project.client(client_id)
120);
121
122CREATE TABLE project.config_contains_product (
123 config_id int4 NOT NULL,
124 prod_id int4 NOT NULL,
125 num_products_config int4 NOT NULL,
126 CONSTRAINT config_contains_product_pk PRIMARY KEY (config_id, prod_id),
127 CONSTRAINT config_contains_product_fk FOREIGN KEY (config_id) REFERENCES project."Configuration"(config_id),
128 CONSTRAINT config_contains_product_fk_1 FOREIGN KEY (prod_id) REFERENCES project.product(prod_id)
129);
130
131CREATE TABLE project.config_to_wishlist (
132 config_id int4 NOT NULL,
133 wishlist_id int4 NOT NULL,
134 CONSTRAINT config_to_wishlist_pk PRIMARY KEY (config_id, wishlist_id),
135 CONSTRAINT config_to_wishlist_fk FOREIGN KEY (wishlist_id, wishlist_id) REFERENCES project.wishlist(wishlist_id, client_id),
136 CONSTRAINT config_to_wishlist_fk_1 FOREIGN KEY (config_id) REFERENCES project."Configuration"(config_id)
137);
138
139
140CREATE TABLE project.contains_config (
141 config_id int4 NOT NULL,
142 cart_id int4 NOT NULL,
143 CONSTRAINT contains_config_pk PRIMARY KEY (cart_id, config_id),
144 CONSTRAINT contains_config_fk FOREIGN KEY (config_id) REFERENCES project."Configuration"(config_id),
145 CONSTRAINT contains_config_fk_1 FOREIGN KEY (cart_id) REFERENCES project.cart(cart_id)
146);
147
148
149CREATE TABLE project.contains_product (
150 prod_id int4 NOT NULL,
151 cart_id int4 NOT NULL,
152 quantity int4 NOT NULL,
153 CONSTRAINT contains_product_pk PRIMARY KEY (prod_id, cart_id),
154 CONSTRAINT contains_product_fk FOREIGN KEY (prod_id) REFERENCES project.product(prod_id),
155 CONSTRAINT contains_product_fk_1 FOREIGN KEY (cart_id) REFERENCES project.cart(cart_id)
156);
157
158
159CREATE TABLE project.delivery (
160 delivery_id int4 NOT NULL,
161 delivery_address varchar(64) NOT NULL,
162 "date" date NOT NULL,
163 status varchar(64) NOT NULL,
164 client_id int4 NOT NULL,
165 distributor_id int4 NOT NULL,
166 CONSTRAINT delivery_pk PRIMARY KEY (delivery_id),
167 CONSTRAINT delivery_fk FOREIGN KEY (distributor_id) REFERENCES project.distributor(distributor_id),
168 CONSTRAINT delivery_fk_1 FOREIGN KEY (client_id) REFERENCES project.client(client_id)
169);
170
171
172CREATE TABLE project."order" (
173 order_id int4 NOT NULL,
174 order_status varchar(64) NOT NULL,
175 total_price int4 NOT NULL,
176 client_id int4 NOT NULL,
177 delivery_id int4 NULL,
178 CONSTRAINT order_pk PRIMARY KEY (order_id),
179 CONSTRAINT order_fk FOREIGN KEY (client_id) REFERENCES project.client(client_id),
180 CONSTRAINT order_fk1 FOREIGN KEY (delivery_id) REFERENCES project.delivery(delivery_id)
181);
182
183
184CREATE TABLE project.orders_configuration (
185 config_id int4 NOT NULL,
186 order_id int4 NOT NULL,
187 CONSTRAINT orders_configuration_pk PRIMARY KEY (config_id, order_id),
188 CONSTRAINT orders_configuration_fk FOREIGN KEY (config_id) REFERENCES project."Configuration"(config_id),
189 CONSTRAINT orders_configuration_fk_1 FOREIGN KEY (order_id) REFERENCES project."order"(order_id)
190);
191
192
193CREATE TABLE project.payment (
194 payment_id int4 NOT NULL,
195 creditcard_number int4 NOT NULL,
196 order_id int4 NOT NULL,
197 CONSTRAINT payment_pk PRIMARY KEY (payment_id),
198 CONSTRAINT payment_fk FOREIGN KEY (order_id) REFERENCES project."order"(order_id)
199);
200
201
202CREATE TABLE project.price_history (
203 price_history_id int4 NOT NULL,
204 dates date NOT NULL,
205 prod_id int4 NOT NULL,
206 "Product_price" int4 NOT NULL,
207 CONSTRAINT price_history_pk PRIMARY KEY (price_history_id),
208 CONSTRAINT price_history_fk FOREIGN KEY (prod_id) REFERENCES project.product(prod_id)
209);
210
211
212CREATE TABLE project.product_to_wishlist (
213 prod_id int4 NOT NULL,
214 wishlist_id int4 NOT NULL,
215 CONSTRAINT product_to_wishlist_pk PRIMARY KEY (prod_id, wishlist_id),
216 CONSTRAINT product_to_wishlist_fk FOREIGN KEY (prod_id) REFERENCES project.product(prod_id),
217 CONSTRAINT product_to_wishlist_fk_1 FOREIGN KEY (wishlist_id, wishlist_id) REFERENCES project.wishlist(wishlist_id, client_id)
218);
219
220
221CREATE TABLE project.checkout (
222 cart_id int4 NOT NULL,
223 order_id int4 NOT NULL,
224 CONSTRAINT checkout_pk PRIMARY KEY (cart_id, order_id),
225 CONSTRAINT checkout_fk FOREIGN KEY (order_id) REFERENCES project."order"(order_id),
226 CONSTRAINT checkout_fk_1 FOREIGN KEY (cart_id) REFERENCES project.cart(cart_id)
227);
228
229CREATE TABLE project.current_price (
230 price_history_id int4 NOT NULL,
231 order_id int4 NOT NULL,
232 num_products int4 NOT NULL,
233 CONSTRAINT current_price_pk PRIMARY KEY (order_id, price_history_id),
234 CONSTRAINT current_price_fk FOREIGN KEY (price_history_id) REFERENCES project.price_history(price_history_id),
235 CONSTRAINT current_price_fk_1 FOREIGN KEY (order_id) REFERENCES project."order"(order_id)
236);