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

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