Welcome to our in-depth guide on the SQL Snowflake Schema! This tutorial is designed for beginners and intermediates, so let's dive right in. ctic_fox
A Snowflake Schema is a type of data warehouse schema that organizes data into a star-like structure, providing optimized query performance for data warehousing and business intelligence purposes.
In a Snowflake Schema, fact tables are connected to dimension tables, which are further normalized, resembling a snowflake. This structure allows for efficient querying and analysis of large, complex datasets.
Fact tables store measurable data, such as sales, transactions, or website clicks. Each fact table should have a primary key and foreign keys that reference related dimension tables.
-- Example of a Fact Table: Sales
CREATE TABLE Sales (
sale_id INT PRIMARY KEY,
sale_date DATE,
product_id INT,
quantity INT,
price DECIMAL(10,2),
FOREIGN KEY (product_id) REFERENCES Products(product_id)
);Dimension tables store descriptive attributes, such as product names, customer demographics, or geographic locations. Each dimension table should have a primary key and foreign keys that can be used to join with fact tables.
-- Example of a Dimension Table: Products
CREATE TABLE Products (
product_id INT PRIMARY KEY,
product_name VARCHAR(255),
product_description TEXT,
category_id INT,
FOREIGN KEY (category_id) REFERENCES Categories(category_id)
);In a Snowflake Schema, dimension tables are further normalized to reduce data redundancy and improve data consistency. For example, if the Products table contains a category_id, a separate Categories table can be created to store the category details.
-- Example of a Normalized Dimension Table: Categories
CREATE TABLE Categories (
category_id INT PRIMARY KEY,
category_name VARCHAR(255)
);What is the main difference between a fact table and a dimension table in a Snowflake Schema?