Data Modeling in SQL
Thomas Alex Mathew
Insight Data Science
2020-07-21
What is Data Modeling ?
What is Data Modeling ?
Twitter Data Model
Why is Data Modeling Important
Architecture of a typical data-heavy system
Production DBs
OLTP
Analytic DBs
OLAP
APIs
Android
WWW
iOS
📸
Reporting/Business Intelligence (BI)
Data Warehouse
Running the business
OLTP Modeling�(normalized)
Relational Data Model (OLTP)
Data Normalization
First Normalization Form (1NF)
You can’t handle the key!
First Normalization Form (1NF)
You can’t handle the key!
First Normalization Form (1NF)
You can’t handle the key!
First Normalization Form (1NF)
You can’t handle the key!
1NF compliant
1NF noncompliant
First Normalization Form (1NF)
You can’t handle the key!
First Normalization Form (1NF)
You can’t handle the key!
normalize
Second Normalization Form (2NF)
You can’t handle the key!
Second Normalization Form (2NF)
You can’t handle the key!
Second Normalization Form (2NF)
You can’t handle the key!
normalize
Third Normalization Form (3NF)
You can’t handle the key!
and B is functionally dependent on C�THEN A is transitively dependent on C (via B)
Third Normalization Form (3NF)
You can’t handle the key!
AND Genre Type is functionally dependent on Genre ID��THEN Genre Type is transitively dependent on Book ID
Third Normalization Form (3NF)
You can’t handle the key!
normalize
Good Approximation
"[Every] non-key [attribute] must provide a fact about the key, the whole key, and nothing but the key"
- Bill Kent
You can’t handle the key!
How to Data Model
You can’t handle the key!
How to Data Model
You can’t handle the key!
How to Data Model
You can’t handle the key!
How to Data Model - Conceptual
You can’t handle the key!
sale
How to Data Model - Conceptual
You can’t handle the key!
sale
How to Data Model
You can’t handle the key!
How to Data Model - Logical
How to Data Model
You can’t handle the key!
How to Data Model - Physical
How to Data Model - RECAP
OLAP Modeling�(denormalized)
Resources
Dimensional Data Modeling (denormalizing)
Star Schema
Snowflake Schema
Building a Dimensional Data Model
Identify:
The Grain
The grain is the resolution of a single row in the fact table:
Dimensions
Dimension Examples
dim_all_dates | |||
Date (PK) | DayOfWeek | Month | Year |
2014-05-24 | Saturday | May | 2014 |
dim_all_cities | ||||
ID | Name | Country | State | Continent |
1 | San Francisco | USA | California | North America |
2 | Amsterdam | Netherlands | North Holland | Europe |
What are other good dates/cities attributes you could think of?
Conformed Dimensions
A conformed dimension is a dimension table that is shared by two or more fact tables.
Slowly Changing Dimensions
Facts
The facts of a fact table are roughly “measurements” - things you can aggregate in some way:
Fact or Dimension?
Transaction, Periodic Snapshot and Accumulating Periodic Snapshot Tables