-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_data_model_design.sql
More file actions
140 lines (109 loc) · 3.68 KB
/
Copy path02_data_model_design.sql
File metadata and controls
140 lines (109 loc) · 3.68 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
-- =====================================================
-- DATA MODEL DESIGN
-- Superstore Sales Analysis
-- =====================================================
-- =====================================================
-- MODEL OVERVIEW
-- =====================================================
-- Schema Type:
-- Star Schema
-- Fact Table:
-- sales
-- Dimension Tables:
-- customers
-- products
-- orders
-- Date Dimension:
-- Created in Power BI using DAX.
-- =====================================================
-- CUSTOMER DIMENSION DESIGN
-- =====================================================
-- customer_id selected as business key.
CREATE TABLE customers (
customer_id VARCHAR(20) PRIMARY KEY,
customer_name VARCHAR(100),
segment VARCHAR(50)
);
-- =====================================================
-- PRODUCT DIMENSION DESIGN
-- =====================================================
-- Exploration identified inconsistent product identifiers.
-- Findings:
-- Multiple product_ids were associated with the same
-- product_name and vice versa.
-- Decision:
-- Introduce product_key as a surrogate primary key.
-- product_id retained as source-system identifier.
-- product_name retained as primary business identifier.
CREATE TABLE products (
product_key INT AUTO_INCREMENT PRIMARY KEY,
product_id VARCHAR(50),
product_name VARCHAR(255),
category VARCHAR(100),
sub_category VARCHAR(100)
);
-- =====================================================
-- ORDERS DIMENSION DESIGN
-- =====================================================
-- order_id could not be used as a primary key because
-- same order_id appeared multiple times
-- Decision:
-- Introduce row_id as surrogate key.
-- Geographic attributes moved to the Orders table.
CREATE TABLE orders (
row_id INT AUTO_INCREMENT PRIMARY KEY,
order_id VARCHAR(50) ,
order_date DATE,
ship_date DATE,
ship_mode VARCHAR(30),
country VARCHAR(30),
city VARCHAR(30),
state VARCHAR(50),
postal_code INT,
region VARCHAR(50),
customer_id VARCHAR(50),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
-- =====================================================
-- SALES FACT TABLE DESIGN
-- =====================================================
-- Grain:
-- One row per order-product transaction.
-- product_key added to the sales fact table.
-- Reason:
-- product_id alone could not establish a reliable
-- relationship with the product dimension.
-- During data loading, both product_id and product_name
-- are used to map records to product_key.
-- This ensures a one-to-many relationship between
-- Products and Sales and prevents many-to-many
-- relationships in the analytical model.
CREATE TABLE sales(
sales_key INT AUTO_INCREMENT PRIMARY KEY,
customer_id VARCHAR(50),
order_id VARCHAR(50),
product_key INT,
quantity INT,
sales DECIMAL(10,2),
profit DECIMAL(10,2),
discount DECIMAL(4,2),
FOREIGN KEY (product_key)
REFERENCES products(product_key)
);
-- =====================================================
-- RELATIONSHIP DESIGN
-- =====================================================
-- customers → sales (via customer_id) One-to-Many
-- orders → sales (via order_id) One-to-Many
-- products → sales (via product_key) One-to-Many
--
-- Relationship Type: One-to-Many
-- All dimensions connect to sales fact table
-- =====================================================
-- MODEL OBJECTIVE
-- =====================================================
-- The model was designed to:
-- 1. Support scalable business analysis.
-- 2. Eliminate ambiguous product relationships.
-- 3. Enable efficient Power BI reporting.
-- 4. Follow dimensional modeling best practices.