| 1 | /*
|
|---|
| 2 | drop table online_shop.products_orders;
|
|---|
| 3 | drop table online_shop.stock;
|
|---|
| 4 | drop table online_shop.categories;
|
|---|
| 5 | drop table online_shop.products;
|
|---|
| 6 | drop table online_shop.orders;
|
|---|
| 7 | drop table online_shop.customers;
|
|---|
| 8 | drop table online_shop.delivery;
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | -- Табела КОРИСНИК
|
|---|
| 12 | create table online_shop.customers
|
|---|
| 13 | (
|
|---|
| 14 | id_customer serial primary key,
|
|---|
| 15 | email varchar(100) not null unique,
|
|---|
| 16 | first_name varchar(50) not null,
|
|---|
| 17 | last_name varchar(50) not null,
|
|---|
| 18 | phone varchar(15) not null,
|
|---|
| 19 | delivery_address varchar(200) not null,
|
|---|
| 20 | password varchar(100) not null
|
|---|
| 21 | );
|
|---|
| 22 |
|
|---|
| 23 | -- Табела КАТЕГОРИИ
|
|---|
| 24 | create table online_shop.categories
|
|---|
| 25 | (
|
|---|
| 26 | id_category serial primary key,
|
|---|
| 27 | category_name varchar(50) not null
|
|---|
| 28 | );
|
|---|
| 29 |
|
|---|
| 30 | -- Табела ПРОИЗВОДИ
|
|---|
| 31 | create table online_shop.products
|
|---|
| 32 | (
|
|---|
| 33 | id_product serial primary key,
|
|---|
| 34 | id_category integer not null,
|
|---|
| 35 | product_name varchar(100) not null,
|
|---|
| 36 | color varchar(50) not null,
|
|---|
| 37 | price numeric(10, 2) not null,
|
|---|
| 38 | description text,
|
|---|
| 39 | constraint fk_products_categories foreign key (id_category) references online_shop.categories (id_category)
|
|---|
| 40 | on delete cascade on update cascade
|
|---|
| 41 | );
|
|---|
| 42 |
|
|---|
| 43 | -- Табела ЗАЛИХА
|
|---|
| 44 | create table online_shop.stock
|
|---|
| 45 | (
|
|---|
| 46 | id_stock serial primary key,
|
|---|
| 47 | id_product integer not null,
|
|---|
| 48 | size varchar(5) not null check (size in ('XS', 'S', 'M', 'L', 'XL')),
|
|---|
| 49 | quantity integer not null,
|
|---|
| 50 | constraint fk_stock_products foreign key (id_product) references online_shop.products (id_product)
|
|---|
| 51 | on delete cascade on update cascade
|
|---|
| 52 | );
|
|---|
| 53 |
|
|---|
| 54 | -- Табела НАРАЧКИ
|
|---|
| 55 | create table online_shop.orders
|
|---|
| 56 | (
|
|---|
| 57 | id_order serial primary key,
|
|---|
| 58 | id_customer integer not null,
|
|---|
| 59 | payment_method varchar(20) not null check (payment_method in ('Cash', 'Card')),
|
|---|
| 60 | status varchar(20) not null check (status in ('Completed', 'Failed')),
|
|---|
| 61 | order_price numeric(10, 2) not null,
|
|---|
| 62 | delivery_price numeric(10, 2) not null,
|
|---|
| 63 | total_price numeric(10, 2) not null,
|
|---|
| 64 | invoice_code varchar(50) not null unique,
|
|---|
| 65 | order_date timestamp not null,
|
|---|
| 66 | constraint fk_orders_customers foreign key (id_customer) references online_shop.customers (id_customer)
|
|---|
| 67 | on delete cascade on update cascade
|
|---|
| 68 | );
|
|---|
| 69 |
|
|---|
| 70 | -- Табела НАРАЧКИ_Производи
|
|---|
| 71 | create table online_shop.products_orders
|
|---|
| 72 | (
|
|---|
| 73 | id_order integer not null,
|
|---|
| 74 | id_product integer not null,
|
|---|
| 75 | quantity integer not null,
|
|---|
| 76 | size varchar(5) not null check (size in ('XS', 'S', 'M', 'L', 'XL')),
|
|---|
| 77 | id_stock integer not null,
|
|---|
| 78 | primary key (id_order, id_product, id_stock),
|
|---|
| 79 | constraint
|
|---|
| 80 | ) |
|---|