DDL: kreiranje_ddl.sql

File kreiranje_ddl.sql, 2.4 KB (added by 161029, 3 years ago)
Line 
1create table product_category (
2id serial,
3category_name varchar(50) not null,
4constraint pk_pc primary key(id)
5);
6
7create table product (
8id serial,
9sku varchar(255) default null,
10description varchar(1000) not null,
11units_in_stock integer not null,
12unit_price decimal(13,2) not null,
13image_url varchar(255) not null,
14name varchar(100) not null,
15active boolean default true,
16date_created timestamp default null,
17last_updated timestamp default null,
18category_id integer not null,
19constraint product_id primary key(id),
20constraint fk_category foreign key(category_id) references product_category(id)
21);
22
23
24create table address(
25id serial,
26street varchar(255) not null,
27city varchar(100) not null,
28country varchar(100) not null,
29zip_code varchar(50) default null,
30constraint pk_address primary key(id)
31);
32
33create table customer(
34id serial,
35first_name varchar(100) not null,
36last_name varchar(100) not null,
37email varchar(255) unique not null,
38constraint pk_customer primary key(id)
39);
40
41-- áèä弝è order å ðåçåðâèðàí çáîð âî postgresql òàáåëàòà ¼à èìàíóâàâ êàêî orders
42create table orders(
43 id bigserial,
44 order_tracking_number varchar(255) default null,
45 total_price decimal(19,2) not null,
46 total_quantity int not null,
47 date_created timestamp default null,
48 customer_id int not null,
49 billing_address_id int not null,
50 shipping_address_id int not null,
51 constraint pk_order primary key(id),
52 constraint fk_customer_id foreign key(customer_id) references customer(id),
53 constraint fk_billing_address_id foreign key(billing_address_id) references address(id),
54 constraint fk_shipping_address_id foreign key(shipping_address_id) references address(id)
55 );
56
57create table order_item (
58 o_id serial,
59 order_id bigserial,
60 image_url varchar(255) default null,
61 quantity int not null,
62 unit_price decimal(19,2) not null,
63 product_id int not null,
64 constraint pk_order_item primary key(o_id, order_id),
65 constraint fk_order_id foreign key(order_id) references orders(id),
66 constraint fk_product_id foreign key(product_id) references product(id)
67);
68
69create table order_status(
70 os_id serial,
71 order_id bigserial,
72 status varchar(128) default 'active',
73 date_updated timestamp default null,
74 constraint pk_order_status primary key(os_id, order_id),
75 constraint fk_order foreign key(order_id) references orders(id)
76);
77
78
79
80
81