RelationalDesign: kreiranje.sql

File kreiranje.sql, 5.8 KB (added by 193230, 2 years ago)
Line 
1drop schema if exists project cascade;
2create schema project;
3
4drop table if exists users;
5drop table if exists client;
6drop table if exists shipping_employee;
7drop table if exists roles;
8drop table if exists online_stores;
9drop table if exists shipping;
10drop table if exists payment;
11drop table if exists category;
12drop table if exists store_employee;
13drop table if exists product_in_store;
14drop table if exists catalogue;
15drop table if exists price;
16drop table if exists shopping_bag;
17drop table if exists product;
18drop table if exists works_as;
19drop table if exists exists_;
20drop table if exists orders;
21
22create table users (
23
24 user_id integer primary key,
25 user_name varchar(50) not null,
26 surname varchar(50) not null,
27 email varchar(50) not null,
28 username varchar(50) not null,
29 user_password varchar(50) not null
30
31);
32
33create table roles (
34
35 id_role integer primary key,
36 role_name varchar(50) not null,
37 role_desc varchar(200) not null
38
39);
40
41create table online_stores (
42
43 store_id integer primary key,
44 names varchar(50) not null,
45 web_address varchar(50) not null,
46 phone_number varchar(50) not null,
47 email varchar(50) not null,
48 social_media varchar(50) not null
49
50);
51
52create table category(
53
54 category_id integer primary key,
55 category_name varchar(50) not null
56
57);
58
59create table client (
60
61 client_number_id serial not null,
62 user_id integer not null,
63 constraint fk_client_users foreign key (user_id) references users(user_id),
64 constraint pk_client_users primary key (user_id)
65);
66
67
68create table shipping_employee (
69
70 shipping_employee_id serial not null,
71 user_id integer not null,
72 constraint fk_shipping_employee_users foreign key (user_id) references users(user_id),
73 constraint pk_shipping_employee_users primary key (user_id)
74
75);
76
77create table store_employee (
78
79 store_employee_id integer not null,
80 store_id integer not null,
81 user_id integer not null,
82 constraint fk_id_store foreign key (store_id) references online_stores(store_id),
83 constraint fk_store_employee_users foreign key (user_id) references users(user_id),
84 constraint pk_store_employee_users primary key (user_id)
85
86);
87
88create table works_as (
89
90 works_from timestamp not null,
91 works_to timestamp,
92 id_role integer not null,
93 user_id integer not null,
94 constraint fk_role_id foreign key (id_role) references roles(id_role),
95 constraint fk_id_employee_store foreign key (user_id) references users(user_id),
96 constraint pk_works_as primary key (id_role, user_id)
97
98);
99
100create table catalogue(
101
102 catalogue_id integer,
103 catalogue_from timestamp not null,
104 catalogue_to timestamp not null,
105 type_ varchar(50) not null,
106 store_id integer not null,
107 constraint fk_catalogue_online_stores foreign key (store_id) references online_stores(store_id),
108 constraint pk_catalogue_id primary key (catalogue_id, store_id)
109
110);
111
112create table product (
113
114 product_id integer primary key,
115 names varchar(50) not null,
116 in_store bool not null,
117 sizes varchar(50) not null,
118 color varchar(50) not null,
119 category_id integer not null,
120 constraint fk_product_category foreign key (category_id) references category (category_id)
121
122);
123
124
125create table product_in_store (
126
127 product_in_store_id integer primary key,
128 store_id integer not null,
129 product_id integer not null,
130 constraint fk_product_in_store_online_stores foreign key (store_id) references online_stores (store_id),
131 constraint fk_product_in_store_product foreign key (product_id) references product (product_id)
132
133);
134
135create table existss (
136
137 product_in_store_id integer not null,
138 catalogue_id integer not null,
139 store_id integer not null,
140 constraint fk_id_catalogue foreign key (catalogue_id, store_id) references catalogue (catalogue_id, store_id),
141 constraint fk_id_product_in_store2 foreign key (product_in_store_id) references product_in_store (product_in_store_id),
142 constraint pk_exists primary key (catalogue_id, store_id, product_in_store_id)
143
144 );
145
146 create table price (
147
148 price_id integer not null,
149 price integer not null,
150 price_from timestamp not null,
151 price_to timestamp not null,
152 product_in_store_id integer not null,
153 constraint fk_price_product_in_store foreign key (product_in_store_id) references product_in_store (product_in_store_id),
154 constraint pk_price_product_in_store primary key (product_in_store_id, price_id)
155
156);
157
158
159create table shopping_bag(
160
161 shopping_id integer primary key,
162 order_date timestamp not null,
163 shipping_date timestamp not null,
164 modified timestamp not null,
165 user_id integer not null,
166 constraint fk_id_client_number foreign key (user_id) references client (user_id)
167
168);
169
170create table payment(
171
172 payment_id integer primary key,
173 info varchar(50) not null,
174 shopping_id integer not null,
175 constraint fk_payment_shopping_id foreign key (shopping_id) references shopping_bag (shopping_id)
176
177);
178
179
180create table shipping (
181
182 shipping_id integer primary key,
183 locations varchar(50) not null,
184 date_from timestamp not null,
185 date_to timestamp not null,
186 user_id integer not null,
187 shopping_id integer not null,
188 constraint fk_shipping_user foreign key (user_id) references users(user_id),
189 constraint fk_shipping_shopping foreign key (shopping_id) references shopping_bag(shopping_id)
190
191);
192
193create table orders (
194
195 amount integer not null,
196 shopping_id integer not null,
197 product_in_store_id integer not null,
198 price_id integer not null,
199 constraint fk_order_shopping foreign key (shopping_id) references shopping_bag(shopping_id),
200 constraint fk_order_product_in_store foreign key (product_in_store_id, price_id) references price(product_in_store_id, price_id),
201 constraint pk_order primary key(shopping_id, product_in_store_id, price_id)
202);
203
204
205
206
207
208
209
210