-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSchema DDL.sql
64 lines (59 loc) · 1.75 KB
/
Schema DDL.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
-- Table for customers
CREATE TABLE customer (
customer_id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL
);
-- Table for categories
CREATE TABLE category (
category_id SERIAL PRIMARY KEY,
category_name VARCHAR(255) NOT NULL,
parent_category_id INT,
CONSTRAINT fk_parent_category
FOREIGN KEY (parent_category_id)
REFERENCES category (category_id)
);
-- Table for products
CREATE TABLE product (
product_id SERIAL PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
description VARCHAR(255),
long_description TEXT,
product_price NUMERIC(10, 2) NOT NULL,
product_quantity INT NOT NULL,
product_discount NUMERIC(5, 2),
category_id INT NOT NULL,
CONSTRAINT fk_category
FOREIGN KEY (category_id)
REFERENCES category (category_id)
);
-- Table for orders
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
order_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
total_price NUMERIC(10, 2) NOT NULL,
order_discount NUMERIC(5, 2),
ship_date DATE,
ship_cost NUMERIC(10, 2),
customer_id INT NOT NULL,
CONSTRAINT fk_customer
FOREIGN KEY (customer_id)
REFERENCES customer (customer_id)
);
-- Table for order details
CREATE TABLE order_detail (
order_id INT,
product_id INT,
product_order_quantity INT NOT NULL,
unit_price NUMERIC(10, 2) NOT NULL,
product_order_discount NUMERIC(5, 2),
PRIMARY KEY (order_id, product_id),
CONSTRAINT fk_order
FOREIGN KEY (order_id)
REFERENCES orders (order_id),
CONSTRAINT fk_product
FOREIGN KEY (product_id)
REFERENCES product (product_id)
);