1 | CREATE SCHEMA project AUTHORIZATION db_202122z_va_prj_pcpicco_owner;
|
---|
2 |
|
---|
3 | CREATE 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 | CREATE 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 |
|
---|
15 | CREATE 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 |
|
---|
28 | CREATE 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 |
|
---|
37 | CREATE 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 |
|
---|
46 | CREATE 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 |
|
---|
53 | CREATE 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 |
|
---|
67 | CREATE 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 |
|
---|
79 | CREATE 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 |
|
---|
88 | CREATE 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 |
|
---|
95 | CREATE 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 |
|
---|
102 | CREATE 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 |
|
---|
113 | CREATE 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 |
|
---|
121 | CREATE 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 |
|
---|
129 | CREATE 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 |
|
---|
138 | CREATE 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 |
|
---|
146 | CREATE 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 |
|
---|
154 | CREATE 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 |
|
---|
162 | CREATE 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 |
|
---|
171 | CREATE 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 |
|
---|
180 | CREATE 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 |
|
---|
188 | CREATE 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 |
|
---|
198 | CREATE 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 |
|
---|
208 | CREATE 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 |
|
---|
216 | CREATE 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 | );
|
---|