| 1 | drop schema if exists kbnteam CASCADE;
|
|---|
| 2 |
|
|---|
| 3 | create schema kbnteam;
|
|---|
| 4 |
|
|---|
| 5 | set search_path to kbnteam;
|
|---|
| 6 |
|
|---|
| 7 | create table admin (
|
|---|
| 8 | admin_id bigserial primary key,
|
|---|
| 9 | admin_name varchar(255) not null,
|
|---|
| 10 | admin_email varchar(255) unique not null,
|
|---|
| 11 | admin_password varchar(255) not null
|
|---|
| 12 | );
|
|---|
| 13 |
|
|---|
| 14 | create table api_user (
|
|---|
| 15 | api_user_id bigserial primary key,
|
|---|
| 16 | username varchar(255) unique not null,
|
|---|
| 17 | password varchar(255) not null,
|
|---|
| 18 | role varchar(100) not null
|
|---|
| 19 | );
|
|---|
| 20 |
|
|---|
| 21 | create table company (
|
|---|
| 22 | company_id bigserial primary key,
|
|---|
| 23 | company_name varchar(255) not null,
|
|---|
| 24 | company_email varchar(255) unique,
|
|---|
| 25 | company_phone varchar(255) unique,
|
|---|
| 26 | company_address varchar(255)
|
|---|
| 27 | );
|
|---|
| 28 |
|
|---|
| 29 | create table contract_status (
|
|---|
| 30 | contract_status_id bigserial primary key,
|
|---|
| 31 | status_name varchar(100) not null unique
|
|---|
| 32 | );
|
|---|
| 33 |
|
|---|
| 34 | create table contract (
|
|---|
| 35 | contract_id bigserial primary key,
|
|---|
| 36 | company_id bigint not null,
|
|---|
| 37 | contract_status_id bigint not null,
|
|---|
| 38 | contract_start_date date,
|
|---|
| 39 | contract_end_date date,
|
|---|
| 40 | contract_value numeric(12,2),
|
|---|
| 41 |
|
|---|
| 42 | constraint fk_contract_company
|
|---|
| 43 | foreign key (company_id) references company(company_id),
|
|---|
| 44 |
|
|---|
| 45 | constraint fk_contract_status
|
|---|
| 46 | foreign key (contract_status_id) references contract_status(contract_status_id)
|
|---|
| 47 | );
|
|---|
| 48 |
|
|---|
| 49 | create table restaurant (
|
|---|
| 50 | rest_id bigserial primary key,
|
|---|
| 51 | rest_name varchar(255) not null,
|
|---|
| 52 | rest_location varchar(255) not null,
|
|---|
| 53 | rest_website varchar(255),
|
|---|
| 54 | rest_phone varchar(255) unique not null,
|
|---|
| 55 | rest_email varchar(255) unique,
|
|---|
| 56 | company_id bigint,
|
|---|
| 57 |
|
|---|
| 58 | constraint fk_restaurant_company
|
|---|
| 59 | foreign key (company_id) references company(company_id)
|
|---|
| 60 | );
|
|---|
| 61 |
|
|---|
| 62 | create table category (
|
|---|
| 63 | category_id bigserial primary key,
|
|---|
| 64 | category_name varchar(255) not null unique
|
|---|
| 65 | );
|
|---|
| 66 |
|
|---|
| 67 | create table lunch_time (
|
|---|
| 68 | lunch_time_id bigserial primary key,
|
|---|
| 69 | lunch_name varchar(100) not null,
|
|---|
| 70 | start_time time,
|
|---|
| 71 | end_time time
|
|---|
| 72 | );
|
|---|
| 73 |
|
|---|
| 74 | create table ingredient (
|
|---|
| 75 | ingredient_id bigserial primary key,
|
|---|
| 76 | ingredient_name varchar(255) not null unique
|
|---|
| 77 | );
|
|---|
| 78 |
|
|---|
| 79 | create table alergen (
|
|---|
| 80 | alergen_id bigserial primary key,
|
|---|
| 81 | alergen_name varchar(255) not null unique
|
|---|
| 82 | );
|
|---|
| 83 |
|
|---|
| 84 | create table meal (
|
|---|
| 85 | meal_id bigserial primary key,
|
|---|
| 86 | rest_id bigint not null,
|
|---|
| 87 | category_id bigint,
|
|---|
| 88 | lunch_time_id bigint,
|
|---|
| 89 | meal_name varchar(255) not null,
|
|---|
| 90 | meal_price numeric(10,2) not null,
|
|---|
| 91 | meal_description text,
|
|---|
| 92 |
|
|---|
| 93 | constraint fk_meal_restaurant
|
|---|
| 94 | foreign key (rest_id) references restaurant(rest_id),
|
|---|
| 95 |
|
|---|
| 96 | constraint fk_meal_category
|
|---|
| 97 | foreign key (category_id) references category(category_id),
|
|---|
| 98 |
|
|---|
| 99 | constraint fk_meal_lunch_time
|
|---|
| 100 | foreign key (lunch_time_id) references lunch_time(lunch_time_id)
|
|---|
| 101 | );
|
|---|
| 102 |
|
|---|
| 103 | create table drink (
|
|---|
| 104 | drink_id bigserial primary key,
|
|---|
| 105 | rest_id bigint not null,
|
|---|
| 106 | drink_name varchar(255) not null,
|
|---|
| 107 | drink_price numeric(10,2) not null,
|
|---|
| 108 |
|
|---|
| 109 | constraint fk_drink_restaurant
|
|---|
| 110 | foreign key (rest_id) references restaurant(rest_id)
|
|---|
| 111 | );
|
|---|
| 112 |
|
|---|
| 113 | create table meal_ingredient (
|
|---|
| 114 | meal_id bigint not null,
|
|---|
| 115 | ingredient_id bigint not null,
|
|---|
| 116 |
|
|---|
| 117 | primary key (meal_id, ingredient_id),
|
|---|
| 118 |
|
|---|
| 119 | constraint fk_meal_ingredient_meal
|
|---|
| 120 | foreign key (meal_id) references meal(meal_id),
|
|---|
| 121 |
|
|---|
| 122 | constraint fk_meal_ingredient_ingredient
|
|---|
| 123 | foreign key (ingredient_id) references ingredient(ingredient_id)
|
|---|
| 124 | );
|
|---|
| 125 |
|
|---|
| 126 | create table alergen_ingredient (
|
|---|
| 127 | alergen_id bigint not null,
|
|---|
| 128 | ingredient_id bigint not null,
|
|---|
| 129 |
|
|---|
| 130 | primary key (alergen_id, ingredient_id),
|
|---|
| 131 |
|
|---|
| 132 | constraint fk_alergen_ingredient_alergen
|
|---|
| 133 | foreign key (alergen_id) references alergen(alergen_id),
|
|---|
| 134 |
|
|---|
| 135 | constraint fk_alergen_ingredient_ingredient
|
|---|
| 136 | foreign key (ingredient_id) references ingredient(ingredient_id)
|
|---|
| 137 | );
|
|---|
| 138 |
|
|---|
| 139 | create table customer (
|
|---|
| 140 | customer_id bigserial primary key,
|
|---|
| 141 | first_name varchar(255) not null,
|
|---|
| 142 | last_name varchar(255) not null,
|
|---|
| 143 | email varchar(255) unique not null,
|
|---|
| 144 | phone varchar(255) unique,
|
|---|
| 145 | address varchar(255),
|
|---|
| 146 | password varchar(255) not null
|
|---|
| 147 | );
|
|---|
| 148 |
|
|---|
| 149 | create table loyalty_tier (
|
|---|
| 150 | loyalty_tier_id bigserial primary key,
|
|---|
| 151 | tier_name varchar(100) not null unique,
|
|---|
| 152 | min_points integer not null,
|
|---|
| 153 | discount_percent numeric(5,2)
|
|---|
| 154 | );
|
|---|
| 155 |
|
|---|
| 156 | create table customer_loyalty_status (
|
|---|
| 157 | customer_loyalty_status_id bigserial primary key,
|
|---|
| 158 | status_name varchar(100) not null unique
|
|---|
| 159 | );
|
|---|
| 160 |
|
|---|
| 161 | create table customer_loyalty (
|
|---|
| 162 | customer_loyalty_id bigserial primary key,
|
|---|
| 163 | customer_id bigint not null unique,
|
|---|
| 164 | loyalty_tier_id bigint,
|
|---|
| 165 | customer_loyalty_status_id bigint,
|
|---|
| 166 | points integer default 0 not null,
|
|---|
| 167 |
|
|---|
| 168 | constraint fk_customer_loyalty_customer
|
|---|
| 169 | foreign key (customer_id) references customer(customer_id),
|
|---|
| 170 |
|
|---|
| 171 | constraint fk_customer_loyalty_tier
|
|---|
| 172 | foreign key (loyalty_tier_id) references loyalty_tier(loyalty_tier_id),
|
|---|
| 173 |
|
|---|
| 174 | constraint fk_customer_loyalty_status
|
|---|
| 175 | foreign key (customer_loyalty_status_id)
|
|---|
| 176 | references customer_loyalty_status(customer_loyalty_status_id)
|
|---|
| 177 | );
|
|---|
| 178 |
|
|---|
| 179 | create table driver (
|
|---|
| 180 | driver_id bigserial primary key,
|
|---|
| 181 | company_id bigint,
|
|---|
| 182 | first_name varchar(255) not null,
|
|---|
| 183 | last_name varchar(255) not null,
|
|---|
| 184 | phone varchar(255) unique,
|
|---|
| 185 | vehicle_info varchar(255),
|
|---|
| 186 |
|
|---|
| 187 | constraint fk_driver_company
|
|---|
| 188 | foreign key (company_id) references company(company_id)
|
|---|
| 189 | );
|
|---|
| 190 |
|
|---|
| 191 | create table order_status (
|
|---|
| 192 | order_status_id bigserial primary key,
|
|---|
| 193 | status_name varchar(100) not null unique
|
|---|
| 194 | );
|
|---|
| 195 |
|
|---|
| 196 | create table customer_order (
|
|---|
| 197 | order_id bigserial primary key,
|
|---|
| 198 | customer_id bigint not null,
|
|---|
| 199 | rest_id bigint not null,
|
|---|
| 200 | order_status_id bigint not null,
|
|---|
| 201 | order_date timestamp not null default current_timestamp,
|
|---|
| 202 | total_amount numeric(12,2) not null,
|
|---|
| 203 |
|
|---|
| 204 | constraint fk_customer_order_customer
|
|---|
| 205 | foreign key (customer_id) references customer(customer_id),
|
|---|
| 206 |
|
|---|
| 207 | constraint fk_customer_order_restaurant
|
|---|
| 208 | foreign key (rest_id) references restaurant(rest_id),
|
|---|
| 209 |
|
|---|
| 210 | constraint fk_customer_order_status
|
|---|
| 211 | foreign key (order_status_id) references order_status(order_status_id)
|
|---|
| 212 | );
|
|---|
| 213 |
|
|---|
| 214 | create table order_meal (
|
|---|
| 215 | order_id bigint not null,
|
|---|
| 216 | meal_id bigint not null,
|
|---|
| 217 | quantity integer not null default 1,
|
|---|
| 218 |
|
|---|
| 219 | primary key (order_id, meal_id),
|
|---|
| 220 |
|
|---|
| 221 | constraint fk_order_meal_order
|
|---|
| 222 | foreign key (order_id) references customer_order(order_id),
|
|---|
| 223 |
|
|---|
| 224 | constraint fk_order_meal_meal
|
|---|
| 225 | foreign key (meal_id) references meal(meal_id)
|
|---|
| 226 | );
|
|---|
| 227 |
|
|---|
| 228 | create table order_drink (
|
|---|
| 229 | order_id bigint not null,
|
|---|
| 230 | drink_id bigint not null,
|
|---|
| 231 | quantity integer not null default 1,
|
|---|
| 232 |
|
|---|
| 233 | primary key (order_id, drink_id),
|
|---|
| 234 |
|
|---|
| 235 | constraint fk_order_drink_order
|
|---|
| 236 | foreign key (order_id) references customer_order(order_id),
|
|---|
| 237 |
|
|---|
| 238 | constraint fk_order_drink_drink
|
|---|
| 239 | foreign key (drink_id) references drink(drink_id)
|
|---|
| 240 | );
|
|---|
| 241 |
|
|---|
| 242 | create table delivery_status (
|
|---|
| 243 | delivery_status_id bigserial primary key,
|
|---|
| 244 | status_name varchar(100) not null unique
|
|---|
| 245 | );
|
|---|
| 246 |
|
|---|
| 247 | create table delivery (
|
|---|
| 248 | delivery_id bigserial primary key,
|
|---|
| 249 | order_id bigint not null unique,
|
|---|
| 250 | driver_id bigint,
|
|---|
| 251 | delivery_status_id bigint,
|
|---|
| 252 | delivered_at timestamp,
|
|---|
| 253 |
|
|---|
| 254 | constraint fk_delivery_order
|
|---|
| 255 | foreign key (order_id) references customer_order(order_id),
|
|---|
| 256 |
|
|---|
| 257 | constraint fk_delivery_driver
|
|---|
| 258 | foreign key (driver_id) references driver(driver_id),
|
|---|
| 259 |
|
|---|
| 260 | constraint fk_delivery_status
|
|---|
| 261 | foreign key (delivery_status_id) references delivery_status(delivery_status_id)
|
|---|
| 262 | );
|
|---|
| 263 |
|
|---|
| 264 | create table review (
|
|---|
| 265 | review_id bigserial primary key,
|
|---|
| 266 | customer_id bigint not null,
|
|---|
| 267 | review_text text,
|
|---|
| 268 | rating integer check (rating between 1 and 5),
|
|---|
| 269 |
|
|---|
| 270 | constraint fk_review_customer
|
|---|
| 271 | foreign key (customer_id) references customer(customer_id)
|
|---|
| 272 | );
|
|---|
| 273 |
|
|---|
| 274 | create table order_review (
|
|---|
| 275 | order_review_id bigserial primary key,
|
|---|
| 276 | order_id bigint not null unique,
|
|---|
| 277 | review_id bigint not null unique,
|
|---|
| 278 |
|
|---|
| 279 | constraint fk_order_review_order
|
|---|
| 280 | foreign key (order_id) references customer_order(order_id),
|
|---|
| 281 |
|
|---|
| 282 | constraint fk_order_review_review
|
|---|
| 283 | foreign key (review_id) references review(review_id)
|
|---|
| 284 | );
|
|---|
| 285 |
|
|---|
| 286 | create table delivery_review (
|
|---|
| 287 | delivery_review_id bigserial primary key,
|
|---|
| 288 | delivery_id bigint not null unique,
|
|---|
| 289 | review_id bigint not null unique,
|
|---|
| 290 |
|
|---|
| 291 | constraint fk_delivery_review_delivery
|
|---|
| 292 | foreign key (delivery_id) references delivery(delivery_id),
|
|---|
| 293 |
|
|---|
| 294 | constraint fk_delivery_review_review
|
|---|
| 295 | foreign key (review_id) references review(review_id)
|
|---|
| 296 | );
|
|---|
| 297 |
|
|---|
| 298 | create table invoice (
|
|---|
| 299 | invoice_id bigserial primary key,
|
|---|
| 300 | order_id bigint not null unique,
|
|---|
| 301 | issued_at timestamp default current_timestamp,
|
|---|
| 302 | amount numeric(12,2) not null,
|
|---|
| 303 |
|
|---|
| 304 | constraint fk_invoice_order
|
|---|
| 305 | foreign key (order_id) references customer_order(order_id)
|
|---|
| 306 | );
|
|---|
| 307 |
|
|---|
| 308 | create table company_order (
|
|---|
| 309 | company_order_id bigserial primary key,
|
|---|
| 310 | company_id bigint not null,
|
|---|
| 311 | order_id bigint not null unique,
|
|---|
| 312 |
|
|---|
| 313 | constraint fk_company_order_company
|
|---|
| 314 | foreign key (company_id) references company(company_id),
|
|---|
| 315 |
|
|---|
| 316 | constraint fk_company_order_order
|
|---|
| 317 | foreign key (order_id) references customer_order(order_id)
|
|---|
| 318 | ); |
|---|