Designing a robust database schema is fundamental to ensuring that an SQL database is efficient, scalable, and easy to maintain. A schema defines how data is organized, stored, and interrelated in a database. In this chapter, we will cover Schema Design Patterns in SQL, from basic concepts to advanced patterns, with detailed examples, explanations, and code snippets.
A schema is the blueprint of your database that defines its structure, including tables, columns, relationships, and constraints.
Normalization is the process of organizing data to reduce redundancy and improve data integrity.
OrderID | Product1 | Product2 |
---|---|---|
1 | Phone | Charger |
CREATE TABLE Orders (
OrderID INT PRIMARY KEY
);
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
Name VARCHAR(100)
);
CREATE TABLE OrderDetails (
OrderID INT,
ProductID INT,
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
Orders
table holds order details.Products
table holds product details.OrderDetails
table links orders and products to reduce redundancy.A Star Schema is used in data warehousing and involves a central fact table surrounded by dimension tables.
CREATE TABLE Sales (
SaleID INT PRIMARY KEY,
CustomerID INT,
ProductID INT,
Quantity INT,
SaleDate DATE
);
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name VARCHAR(100),
Region VARCHAR(50)
);
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
Name VARCHAR(100),
Category VARCHAR(50)
);
A Snowflake Schema is a more normalized version of the Star Schema, where dimension tables are further split into related sub-tables.
CREATE TABLE ProductCategories (
CategoryID INT PRIMARY KEY,
CategoryName VARCHAR(50)
);
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
Name VARCHAR(100),
CategoryID INT,
FOREIGN KEY (CategoryID) REFERENCES ProductCategories(CategoryID)
);
Here, the Products
table is normalized by splitting Category
into its own table.
The Galaxy Schema supports multiple fact tables sharing common dimension tables.
CREATE TABLE Sales (
SaleID INT PRIMARY KEY,
CustomerID INT,
ProductID INT,
Quantity INT
);
CREATE TABLE Returns (
ReturnID INT PRIMARY KEY,
CustomerID INT,
ProductID INT,
ReturnDate DATE
);
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name VARCHAR(100)
);
Sales
and Returns
share the Customers
dimension table.One entity relates to multiple entities.
CREATE TABLE Authors (
AuthorID INT PRIMARY KEY,
Name VARCHAR(100)
);
CREATE TABLE Books (
BookID INT PRIMARY KEY,
Title VARCHAR(100),
AuthorID INT,
FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID)
);
Many entities relate to many entities through a junction table.
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100)
);
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
Title VARCHAR(100)
);
CREATE TABLE Enrollments (
StudentID INT,
CourseID INT,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
Used for hierarchical data like organizational charts or file systems.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100),
ManagerID INT,
FOREIGN KEY (ManagerID) REFERENCES Employees(EmployeeID)
);
Denormalization combines multiple tables into one to improve query performance.
CREATE TABLE SalesDetails (
SaleID INT PRIMARY KEY,
CustomerName VARCHAR(100),
ProductName VARCHAR(100),
Quantity INT
);
Combines normalized and denormalized designs for flexibility.
Partitioning splits a table into parts for better query performance. Sharding distributes data across multiple databases.
Manages changes in schema over time without breaking existing functionality.
FOREIGN KEY
, NOT NULL
) for data integrity.Schema design patterns are essential for building efficient, scalable databases. By understanding and applying these patterns, you can create robust systems that support various applications, from transactional systems to data warehouses. This chapter has provided you with the tools and examples to design schemas effectively, catering to both current and future needs. Happy coding !❤️