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

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