In data warehousing, a snowflake schema is a logical arrangement of tables in a relational database that is designed for improved query performance and optimized storage. It is an extension of the star schema, where dimensions are further normalized into multiple, smaller tables.
In this tutorial, we will go through the steps to create a snowflake schema in SQL.
Table Structure
To create a snowflake schema, we need to create multiple tables for dimensions and fact tables. Let’s assume we have the following tables:
dimension_country
- Contains information about countries.id
- Primary keyname
- Country name
dimension_product
- Contains information about products.id
- Primary keyname
- Product namecategory
- Product category
dimension_time
- Contains information about time periods.id
- Primary keydate
- Datemonth
- Month of the yearquarter
- Quarter of the yearyear
- Year
fact_sales
- Contains information about sales.id
- Primary keyproduct_id
- Foreign key todimension_product
country_id
- Foreign key todimension_country
time_id
- Foreign key todimension_time
quantity
- Quantity soldrevenue
- Revenue generated
Creating Snowflake Schema
Here are the steps to create a snowflake schema SQL:
-
Create dimension tables:
CREATE TABLE dimension_country ( id INT PRIMARY KEY, name VARCHAR(255) ); CREATE TABLE dimension_product ( id INT PRIMARY KEY, name VARCHAR(255), category VARCHAR(255) ); CREATE TABLE dimension_time ( id INT PRIMARY KEY, date DATE, month INT, quarter INT, year INT );
-
Create the fact table:
CREATE TABLE fact_sales ( id INT PRIMARY KEY, product_id INT, country_id INT, time_id INT, quantity INT, revenue FLOAT, FOREIGN KEY (product_id) REFERENCES dimension_product(id), FOREIGN KEY (country_id) REFERENCES dimension_country(id), FOREIGN KEY (time_id) REFERENCES dimension_time(id) );
-
Ensure the appropriate indexes and constraints are in place for efficient querying and data integrity.
And that’s it! You have now created a snowflake schema in SQL.
Remember to populate the dimension tables with data, and then you can insert data into the fact table to start analyzing your data using the snowflake schema.
#datawarehousing #sql
By following these steps, you can effectively create a snowflake schema in SQL for your data warehousing needs. This schema provides a structured and optimized way to store and analyze data, improving performance and ease of use.