In Database Management Systems (DBMS), keys are the backbone of data organization, retrieval, and relationship management. They ensure that databases remain structured, efficient, and free from redundancy. Whether you're a beginner learning SQL or an experienced developer optimizing a database, understanding the different types of keys in DBMS is crucial for designing robust and scalable systems.
Keys serve multiple purposes:
- Uniquely identifying records (e.g., a student ID in a university database).
- Establishing relationships between tables (e.g., linking orders to customers).
- Maintaining data integrity by preventing duplicate or inconsistent entries.
Types of Keys in the Relational Model.
Here is the list of Keys that are present in the Relational Model:
- Primary Key (PK) – Uniquely identifies each row (e.g., Student_ID).
- Foreign Key (FK) – Links tables by referencing a PK (e.g., Order.Customer_ID).
- Candidate Key – A potential PK (e.g., Email if unique).
- Alternate Key – Unused candidate keys (e.g., SSN when ID is PK).
- Composite Key – A PK made of multiple columns (e.g., (Student_ID, Course_ID)).
- Super Key – A set of columns containing a candidate key (e.g., {ID, Name}).
Let's discuss each of them one by one in detail,
1. Primary Key (PK).
A Primary Key (PK) is a column (or set of columns) in a table that uniquely identifies each row. It enforces data integrity by ensuring:
- No duplicate values (all entries must be unique).
- No NULL values (every row must have a PK value).
Key Features
- Uniqueness: Guarantees no two rows have the same PK value. Example: Employee_ID in the Employees table.
- Irreducibility: Cannot remove any column from a composite PK without breaking uniqueness.
- Mandatory: Every table must have exactly one PK (but can have multiple candidate keys).
CREATE TABLE Students ( Student_ID INT PRIMARY KEY, -- PK column Name VARCHAR(50), Email VARCHAR(100) UNIQUE -- Candidate key );
When to use Primary Key (PK)?
- You need to uniquely identify each row in a table, such as assigning a unique EmployeeID to every record in an Employees table.
- You want to enforce data integrity, ensuring no duplicate or NULL values exist in the key column (e.g., ProductID in a Products table).
- Creating relationships between tables, as it serves as the target for Foreign Keys in related tables (e.g., CustomerID in a Customers table referenced by Orders).
- Optimizing query performance, since databases automatically index PKs for faster searches and joins (e.g., filtering orders by OrderID).
- Working with frameworks or ORMs, as most require a PK for CRUD operations (e.g., Django models or SQLAlchemy).
2. Foreign Key (FK).
- Links Tables: Creates relationships (e.g., Orders.Customer_ID → Customers.Customer_ID).
- Allows NULLs: Optional relationships (unless constrained).
- Supports Cascading Actions: Automatic updates/deletes (e.g., ON DELETE CASCADE).
CREATE TABLE Orders ( Order_ID INT PRIMARY KEY, Customer_ID INT REFERENCES Customers(Customer_ID) -- FK );
When to use a Foreign Key (FK)?
- You need to link two tables, such as connecting an Orders table to a Customers table via CustomerID.
- You want to enforce referential integrity, ensuring that every record in the child table (e.g., Orders) corresponds to a valid record in the parent table (e.g., Customers).
- You want to automate updates or deletions across related tables, such as deleting all orders when a customer is removed (using ON DELETE CASCADE).
- Modeling relationships like one-to-many (e.g., one customer can have many orders) or many-to-one (e.g., many orders belong to one customer).
3. Candidate Key.
- Uniqueness: No two rows can have identical values in the candidate key columns.
- Irreducibility: No subset of the key can uniquely identify rows (it's "minimal").
- A table can have multiple candidate keys.
- One candidate key is chosen as the Primary Key (PK); others become Alternate Keys.
- Candidate Key cannot contain NULL values (to ensure row uniqueness).
- Student_ID (Unique) → Candidate Key
- Email (Unique) → Candidate Key
- {Name, Birthdate} (If unique) → Composite Candidate Key
4. Alternate Key.
- Uniqueness: Guarantees no duplicate values in its columns.
- Non-NULL: Cannot contain NULL values (like all candidate keys).
- Secondary Role: Used when the PK is impractical for certain queries.
- Primary Key (PK): Student_ID
- Alternate Keys: Email or SSN (if both are unique and minimal).
CREATE TABLE Students ( Student_ID INT PRIMARY KEY, Email VARCHAR(100) UNIQUE NOT NULL, -- Alternate Key SSN CHAR(9) UNIQUE NOT NULL -- Alternate Key );
5. Composite Key.
- Multi-Column Uniqueness: The combination of columns must be unique.
- Mandatory Fields: None of the component columns can contain NULL values.
- Relationship Enforcement: Often used in junction tables for many-to-many relationships.
CREATE TABLE Order_Details ( OrderID INT NOT NULL, ProductID INT NOT NULL, Quantity INT, PRIMARY KEY (OrderID, ProductID) -- Composite Key );
When to use a Composite Key?
- When you need to uniquely identify relationships between entities
- When a single column isn't sufficient for uniqueness (e.g., flight number + date)
- When you need to uniquely identify records across multiple dimensions
6. Super Key.
- Uniqueness Guarantee: Must uniquely identify each record.
- Non-Minimal: May include additional columns beyond what's needed for uniqueness.
- Flexible Composition: Can be any size from one column to all columns in the table.
- Student_ID (unique)
- Email (unique)
- Name
- Birthdate
- {Student_ID}
- {Email}
- {Student_ID, Name}
- {Email, Birthdate}
- {Student_ID, Name, Email, Birthdate}
No comments:
Post a Comment