| 1 | = Креирање на табели |
| 2 | ---- |
| 3 | |
| 4 | create table users |
| 5 | ( |
| 6 | id bigserial primary key, |
| 7 | first_name varchar(255), |
| 8 | last_name varchar(255), |
| 9 | phone_number varchar(255), |
| 10 | email varchar(255) unique, |
| 11 | password varchar(255), |
| 12 | role varchar(255) |
| 13 | ); |
| 14 | |
| 15 | create table address |
| 16 | ( |
| 17 | id bigserial primary key, |
| 18 | address varchar(255), |
| 19 | user_id bigint, |
| 20 | foreign key (user_id) references users (id) |
| 21 | on delete cascade |
| 22 | on update cascade |
| 23 | ); |
| 24 | |
| 25 | create table category |
| 26 | ( |
| 27 | id bigserial primary key, |
| 28 | name varchar(255), |
| 29 | category_id bigint references category (id) |
| 30 | on delete cascade |
| 31 | on update cascade |
| 32 | ); |
| 33 | |
| 34 | create table supplier |
| 35 | ( |
| 36 | id bigserial primary key, |
| 37 | name varchar(255), |
| 38 | location varchar(255), |
| 39 | phone_number varchar(255) |
| 40 | ); |
| 41 | |
| 42 | create table product |
| 43 | ( |
| 44 | id bigserial primary key, |
| 45 | name varchar(255), |
| 46 | description varchar(255), |
| 47 | image bytea, |
| 48 | quantity integer, |
| 49 | category_id bigint references category (id) |
| 50 | on delete set null |
| 51 | on update cascade, |
| 52 | supplier_id bigint references supplier (id) |
| 53 | on delete set null |
| 54 | on update cascade |
| 55 | ); |
| 56 | |
| 57 | create table product_price |
| 58 | ( |
| 59 | product_id bigint, |
| 60 | price integer, |
| 61 | start_date date, |
| 62 | end_date date, |
| 63 | foreign key (product_id) references product (id) |
| 64 | on delete cascade |
| 65 | on update cascade, |
| 66 | primary key (product_id, price, start_date), |
| 67 | check ( price > 0 ), |
| 68 | check ( end_date >= start_date ) |
| 69 | ); |
| 70 | |
| 71 | |
| 72 | create table property |
| 73 | ( |
| 74 | id bigserial primary key, |
| 75 | name varchar(255) |
| 76 | ); |
| 77 | |
| 78 | create table product_property |
| 79 | ( |
| 80 | product_id bigint, |
| 81 | property_id bigint, |
| 82 | value varchar(255), |
| 83 | foreign key (product_id) references product (id) |
| 84 | on delete cascade |
| 85 | on update cascade, |
| 86 | foreign key (property_id) references property (id) |
| 87 | on delete cascade |
| 88 | on update cascade, |
| 89 | primary key (product_id, property_id) |
| 90 | ); |
| 91 | |
| 92 | create table orders |
| 93 | ( |
| 94 | id bigserial primary key, |
| 95 | status varchar(255), |
| 96 | date date, |
| 97 | price integer, |
| 98 | user_id bigint, |
| 99 | foreign key (user_id) references users (id) |
| 100 | on delete set null |
| 101 | on update cascade |
| 102 | ); |
| 103 | |
| 104 | create table order_details |
| 105 | ( |
| 106 | order_id bigint, |
| 107 | product_id bigint, |
| 108 | quantity integer, |
| 109 | foreign key (order_id) references orders (id) |
| 110 | on delete cascade |
| 111 | on update cascade, |
| 112 | foreign key (product_id) references product (id) |
| 113 | on delete cascade |
| 114 | on update cascade, |
| 115 | primary key (order_id, product_id) |
| 116 | ); |