Логички и физички дизајн - Креирање база податоци (со SQL DDL): DDL V2.sql

File DDL V2.sql, 3.5 KB (added by 183175, 6 weeks ago)
Line 
1/*
2drop table online_shop.products_orders;
3drop table online_shop.stock;
4drop table online_shop.categories;
5drop table online_shop.products;
6drop table online_shop.orders;
7drop table online_shop.customers;
8drop table online_shop.delivery;
9*/
10
11-- Табела КОРИСНИК
12create 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-- Табела КАТЕГОРИИ
24create table online_shop.categories
25(
26 id_category serial primary key,
27 category_name varchar(50) not null
28);
29
30-- Табела ПРОИЗВОДИ
31create 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-- Табела ЗАЛИХА
44create 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-- Табела НАРАЧКИ
55create 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-- Табела НАРАЧКИ_Производи
71create 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 fk_products_orders_orders foreign key (id_order) references online_shop.orders (id_order)
80 on delete cascade on update cascade,
81 constraint fk_products_orders_products foreign key (id_product) references online_shop.products (id_product)
82 on delete cascade on update cascade,
83 constraint fk_products_orders_stock foreign key (id_stock) references online_shop.stock (id_stock)
84 on delete cascade on update cascade
85);
86
87-- Табела ИСПОРАКА
88create table online_shop.delivery
89(
90 id_delivery serial primary key,
91 id_order integer not null,
92 delivery_address varchar(200) not null,
93 status varchar(20) not null check (status in ('Pending', 'Shipped', 'Delivered')),
94 constraint fk_delivery_orders foreign key (id_order) references online_shop.orders (id_order)
95 on delete cascade on update cascade
96);