The entity-relationship (ER) model is fundamental to relational database design, depicting entities, attributes, and relationships between them. Entities are represented as tables, attributes as columns, and relationships as foreign key constraints. Let's consider an example of an ER model representing a university database:
CREATE TABLE student (
student_id INT PRIMARY KEY,
name VARCHAR,
-- Other student attributes
);
CREATE TABLE course (
course_id INT PRIMARY KEY,
name VARCHAR,
-- Other course attributes
);
CREATE TABLE enrollment (
student_id INT,
course_id INT,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES student(student_id),
FOREIGN KEY (course_id) REFERENCES course(course_id)
);
Normalization aims to reduce redundancy and improve data integrity by organizing data into well-structured relations. Techniques such as first normal form (1NF), second normal form (2NF), and beyond are essential for maintaining database consistency. Let's illustrate the process of normalizing a denormalized table into third normal form (3NF):
-- Original denormalized table
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_name VARCHAR,
product_name VARCHAR,
quantity INT,
total_price DECIMAL
);
-- Normalized tables in third normal form (3NF)
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR,
-- Other customer attributes
);
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR,
-- Other product attributes
);
CREATE TABLE order_items (
order_id INT,
product_id INT,
quantity INT,
total_price DECIMAL,
PRIMARY KEY (order_id, product_id),
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
Beyond traditional schemas like star and snowflake, advanced schema designs such as fact constellation, galaxy schema, and anchor modeling offer alternative approaches to data warehousing.
- Star Schema:
CREATE TABLE fact_sales (
date_id INT,
product_id INT,
store_id INT,
sales_amount DECIMAL,
PRIMARY KEY (date_id, product_id, store_id)
);
CREATE TABLE dim_date (
date_id INT PRIMARY KEY,
date DATE,
-- Other date attributes
);
CREATE TABLE dim_product (
product_id INT PRIMARY KEY,
-- Product attributes
);
CREATE TABLE dim_store (
store_id INT PRIMARY KEY,
-- Store attributes
);
- Snowflake Schema:
-- Example of a Galaxy schema would involve multiple interconnected star schemas.
-- Consider a scenario where we have a retail database with separate star schemas for sales, customers, and products.
-- We can create a Galaxy schema by connecting these star schemas through shared dimensions like time and geography.
-- Star schema for sales
CREATE TABLE fact_sales (
date_id INT,
product_id INT,
store_id INT,
sales_amount DECIMAL,
PRIMARY KEY (date_id, product_id, store_id)
);
CREATE TABLE dim_date (
date_id INT PRIMARY KEY,
date DATE,
-- Other date attributes
);
CREATE TABLE dim_product (
product_id INT PRIMARY KEY,
-- Product attributes
);
CREATE TABLE dim_store (
store_id INT PRIMARY KEY,
-- Store attributes
);
-- Star schema for customers
CREATE TABLE fact_customers (
date_id INT,
customer_id INT,
store_id INT,
purchase_count INT,
PRIMARY KEY (date_id, customer_id, store_id)
);
CREATE TABLE dim_customer (
customer_id INT PRIMARY KEY,
-- Customer attributes
);
-- Interconnecting dimensions
CREATE TABLE dim_time (
date_id INT PRIMARY KEY,
date DATE,
-- Other time attributes
);
CREATE TABLE dim_geography (
store_id INT PRIMARY KEY,
-- Geography attributes
);
- Anchor Modeling:
-- Example of anchor modeling involves defining anchor tables representing atomic data elements and flexible relationships.
-- Let's consider a simple example of anchor modeling for a blogging platform.
-- Anchor table for blog posts
CREATE TABLE anchor_post (
post_id INT PRIMARY KEY,
title VARCHAR,
content TEXT,
-- Other post attributes
);
-- Anchor table for authors
CREATE TABLE anchor_author (
author_id INT PRIMARY KEY,
name VARCHAR,
-- Other author attributes
);
-- Anchor table for categories
CREATE TABLE anchor_category (
category_id INT PRIMARY KEY,
name VARCHAR,
-- Other category attributes
);
-- Table for representing relationships
CREATE TABLE anchor_post_author (
post_id INT,
author_id INT,
PRIMARY KEY (post_id, author_id),
FOREIGN KEY (post_id) REFERENCES anchor_post(post_id),
FOREIGN KEY (author_id) REFERENCES anchor_author(author_id)
);
-- Table for representing relationships
CREATE TABLE anchor_post_category (
post_id INT,
category_id INT,
PRIMARY KEY (post_id, category_id),
FOREIGN KEY (post_id) REFERENCES anchor_post(post_id),
FOREIGN KEY (category_id) REFERENCES anchor_category(category_id)
);
Blog Statistics