-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11-schema-design.sql
More file actions
103 lines (95 loc) · 2.76 KB
/
Copy path11-schema-design.sql
File metadata and controls
103 lines (95 loc) · 2.76 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
-- Lesson 11: Schema Design and Constraints
-- Goal: learn how to model Superstore data into related tables with keys.
-- 1. Drop practice tables in dependency order.
DROP TABLE IF EXISTS lesson_fact_order_items;
DROP TABLE IF EXISTS lesson_dim_products;
DROP TABLE IF EXISTS lesson_dim_customers;
-- 2. Create parent tables with primary keys, unique constraints, and defaults.
CREATE TABLE lesson_dim_customers (
customer_id VARCHAR(50) PRIMARY KEY,
customer_name TEXT NOT NULL,
segment TEXT NOT NULL CHECK (segment IN ('Consumer', 'Corporate', 'Home Office')),
region TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE lesson_dim_products (
product_id VARCHAR(50) PRIMARY KEY,
product_name TEXT NOT NULL,
category TEXT NOT NULL,
sub_category TEXT NOT NULL
);
-- 3. Create a child table with foreign keys and validation rules.
CREATE TABLE lesson_fact_order_items (
row_id INTEGER PRIMARY KEY,
order_id VARCHAR(50) NOT NULL,
customer_id VARCHAR(50) NOT NULL REFERENCES lesson_dim_customers (customer_id),
product_id VARCHAR(50) NOT NULL REFERENCES lesson_dim_products (product_id),
order_date DATE NOT NULL,
sales NUMERIC(12, 4) NOT NULL CHECK (sales >= 0),
quantity INTEGER NOT NULL CHECK (quantity > 0),
discount NUMERIC(4, 2) NOT NULL CHECK (discount BETWEEN 0 AND 1),
profit NUMERIC(12, 4) NOT NULL
);
-- 4. Insert sample dimension rows from superstore.
INSERT INTO lesson_dim_customers (customer_id, customer_name, segment, region)
SELECT DISTINCT ON (customer_id)
customer_id,
customer_name,
segment,
region
FROM superstore
ORDER BY customer_id
LIMIT 25;
INSERT INTO lesson_dim_products (product_id, product_name, category, sub_category)
SELECT DISTINCT ON (product_id)
product_id,
product_name,
category,
sub_category
FROM superstore
ORDER BY product_id
LIMIT 40;
-- 5. Insert fact rows that must satisfy foreign keys.
INSERT INTO lesson_fact_order_items (
row_id,
order_id,
customer_id,
product_id,
order_date,
sales,
quantity,
discount,
profit
)
SELECT
s.row_id,
s.order_id,
s.customer_id,
s.product_id,
s.order_date,
s.sales,
s.quantity,
s.discount,
s.profit
FROM superstore s
JOIN lesson_dim_customers c
ON s.customer_id = c.customer_id
JOIN lesson_dim_products p
ON s.product_id = p.product_id
ORDER BY s.row_id
LIMIT 50;
-- 6. Review the normalized result with joins.
SELECT
f.order_id,
c.customer_name,
c.region,
p.category,
p.product_name,
f.sales,
f.profit
FROM lesson_fact_order_items f
JOIN lesson_dim_customers c
ON f.customer_id = c.customer_id
JOIN lesson_dim_products p
ON f.product_id = p.product_id
ORDER BY f.order_date, f.order_id;