1 | /*
|
---|
2 | drop table if exists online_shop.contains;
|
---|
3 | drop table if exists online_shop.stock;
|
---|
4 | drop table if exists online_shop.categories;
|
---|
5 | drop table if exists online_shop.products;
|
---|
6 | drop table if exists online_shop.orders;
|
---|
7 | drop table if exists online_shop.customers;
|
---|
8 | drop table if exists 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 | delivery_price numeric(10, 2) not null,
|
---|
62 | total_price numeric(10, 2) not null,
|
---|
63 | invoice_code varchar(50) not null unique,
|
---|
64 | order_date timestamp not null,
|
---|
65 | constraint fk_orders_customers foreign key (id_customer) references online_shop.customers (id_customer)
|
---|
66 | on delete cascade on update cascade
|
---|
67 | );
|
---|
68 |
|
---|
69 | -- Табела ИСПОРАКА
|
---|
70 | create table online_shop.delivery
|
---|
71 | (
|
---|
72 | id_delivery serial primary key,
|
---|
73 | id_order integer not null,
|
---|
74 | delivery_address varchar(200) not null,
|
---|
75 | status varchar(20) not null check (status in ('Pending', 'Shipped', 'Delivered')),
|
---|
76 | constraint fk_delivery_orders foreign key (id_order) references online_shop.orders (id_order)
|
---|
77 | on delete cascade on update cascade
|
---|
78 | );
|
---|
79 |
|
---|
80 | -- Табела СОДРЖИ
|
---|
81 | create table online_shop.contains
|
---|
82 | (
|
---|
83 | id_order integer not null,
|
---|
84 | id_stock integer not null,
|
---|
85 | quantity integer not null,
|
---|
86 | size varchar(5) not null check (size in ('XS', 'S', 'M', 'L', 'XL')),
|
---|
87 | price numeric(10, 2) not null,
|
---|
88 | primary key (id_order, id_stock),
|
---|
89 | constraint fk_contains_orders foreign key (id_order) references online_shop.orders (id_order)
|
---|
90 | on delete cascade on update cascade,
|
---|
91 | constraint fk_contains_stock foreign key (id_stock) references online_shop.stock (id_stock)
|
---|
92 | on delete cascade on update cascade
|
---|
93 | );
|
---|