Unlocking the Secrets of Database Keys: A Comprehensive Guide
Introduction
In the realm of database management, keys play a crucial role in maintaining data integrity, ensuring data consistency, and facilitating efficient data retrieval. In this masterclass, we will delve into the world of database keys, exploring the concepts of primary, foreign, candidate, composite, unique, super, surrogate, and natural keys. By the end of this tutorial, you will have a deep understanding of each key type and how to effectively utilize them in your database design.
Primary Key (PK)
A primary key is a unique identifier for each row in a table. It ensures that no duplicate values exist in the table and provides a way to uniquely identify each record. A primary key can be a single column or a combination of columns.
Properties of a Primary Key:
- Unique: Each value in the primary key column(s) must be unique.
- Not Null: Primary key columns cannot contain null values.
- Indexing: Primary keys are automatically indexed, which facilitates faster data retrieval.
SQL Example: Creating a Primary Key
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(255),
Age INT
);
Foreign Key (FK)
A foreign key is a column or set of columns in a table that references the primary key of another table. It establishes a relationship between two tables and ensures data consistency by preventing the creation of orphan records.
Properties of a Foreign Key:
- Referential Integrity: The foreign key value must exist in the primary key column of the referenced table.
- Cascading Actions: Changes to the primary key value can be cascaded to the foreign key table.
SQL Example: Creating a Foreign Key
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
EmployeeID INT,
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID)
);
Candidate Key
A candidate key is a set of columns that can uniquely identify each row in a table. A table can have multiple candidate keys, but only one can be designated as the primary key.
Properties of a Candidate Key:
- Unique: Each value in the candidate key column(s) must be unique.
- Not Null: Candidate key columns cannot contain null values.
SQL Example: Creating a Candidate Key
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Email VARCHAR(255) UNIQUE NOT NULL
);
In this example, the Email column is a candidate key because it can uniquely identify each employee.
Composite Key
A composite key is a primary key or candidate key that consists of multiple columns. It is used when a single column cannot uniquely identify each row in a table.
SQL Example: Creating a Composite Key
CREATE TABLE OrderDetails (
OrderID INT,
ProductID INT,
PRIMARY KEY (OrderID, ProductID)
);
Unique Key
A unique key is a set of columns that ensures uniqueness, but allows null values. Unlike primary keys, unique keys do not have to be not null.
SQL Example: Creating a Unique Key
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Email VARCHAR(255) UNIQUE
);
Super Key
A super key is a set of columns that includes the primary key and zero or more additional columns. It is used to ensure uniqueness and provide additional indexing.
SQL Example: Creating a Super Key
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(255),
Age INT,
UNIQUE (EmployeeID, Name)
);
Surrogate Key
A surrogate key is an artificial primary key that replaces a natural primary key. It is used to improve performance, reduce storage requirements, and simplify database design.
SQL Example: Creating a Surrogate Key
CREATE TABLE Employees (
EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(255),
Age INT
);
Natural Key
A natural key is a primary key that is based on a real-world attribute, such as a name or an email address. It is used when a surrogate key is not necessary or desirable.
SQL Example: Creating a Natural Key
CREATE TABLE Employees (
Email VARCHAR(255) PRIMARY KEY,
Name VARCHAR(255),
Age INT
);
Conclusion
In this masterclass, we explored the different types of database keys, including primary, foreign, candidate, composite, unique, super, surrogate, and natural keys. We discussed the properties and uses of each key type, and provided SQL examples to illustrate their implementation. By understanding and effectively utilizing these keys, you can design robust, efficient, and scalable databases that meet the needs of your applications.
⚔️ YOUR MISSION
Design a Database Schema for an E-commerce Application
Create a database schema for an e-commerce application that includes the following entities:
- Customers
- Orders
- Products
- Order Details
Use the concepts learned in this masterclass to design a robust and efficient database schema. Include primary keys, foreign keys, candidate keys, composite keys, unique keys, super keys, surrogate keys, and natural keys as necessary.
Read the exercise prompt
CREATE DATABASE EcommerceDB;
USE EcommerceDB;
CREATE TABLE Customers (
CustomerID INT IDENTITY(1,1) PRIMARY KEY,
Email VARCHAR(255) UNIQUE NOT NULL,
Name VARCHAR(255),
Address VARCHAR(255)
);
CREATE TABLE Orders (
OrderID INT IDENTITY(1,1) PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
CREATE TABLE Products (
ProductID INT IDENTITY(1,1) PRIMARY KEY,
ProductName VARCHAR(255),
Price DECIMAL(10, 2)
);
CREATE TABLE OrderDetails (
OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
