Database Management SystemUnit 1012 min read
Database Decomposition & Keys: Normalization, Lossless Decomposition & Candidate Keys
Unit 10 of Database Management System explores how to break down complex relations into smaller, optimized tables (decomposition) while preserving data integrity, and how to identify unique identifiers (keys) that ensure accurate record retrieval. Covers lossless decomposition, dependency preservation, candidate keys,
TAKEAWAYS:
- Decomposition splits relations into smaller tables to eliminate redundancy and improve efficiency, but must preserve dependencies and avoid data loss.
- Lossless decomposition ensures no information is lost when relations are split, verified using functional dependencies.
- Candidate keys are minimal sets of attributes that uniquely identify tuples, while primary keys are chosen from candidates for database operations.
- Superkeys are any set of attributes that can uniquely identify tuples, containing at least one candidate key.
- Normalization (1NF, 2NF, 3NF, BCNF) directly relates to decomposition, ensuring tables are free of anomalies.
- SQL implementation uses
PRIMARY KEY,FOREIGN KEY, andUNIQUEconstraints to enforce key properties in decomposed databases.
Core Concepts: Keys in Databases
erDiagram
Student ||--o{ Enrollment : takes
Course ||--o{ Enrollment : offered
Student {
int SID PK
string SName
string SAddress
string SEmail
}
Course {
int CID PK
string CName
int Credit_hours
}
Enrollment {
int SID PK, FK
int CID PK, FK
}ER diagram showing candidate keys (SID, CID) and primary keys (Enrollment composite key)1. Superkeys, Candidate Keys, and Primary Keys
Keys are attributes (or sets of attributes) that uniquely identify tuples in a relation. They are fundamental to maintaining data integrity and enabling efficient querying.
Definitions
- Superkey: A set of attributes that can uniquely identify tuples in a relation. It may contain redundant attributes.
Example: In
Student(SID, SName, SAddress, SEmail),{SID, SName}is a superkey becauseSIDalone is sufficient, but includingSName(which may not be unique) still makes it a superkey. - Candidate Key: A minimal superkey—no proper subset of it is a superkey. A relation can have multiple candidate keys.
Example: In
Employee(EID, SSN, Name), both{EID}and{SSN}are candidate keys if neither can be derived from the other. - Primary Key: A candidate key chosen by the database designer to uniquely identify tuples. It cannot contain
NULLvalues. Example: InCourse(CID, CName, Credit_hours),CIDis the primary key.
Why Keys Matter
- Uniqueness: Ensure no duplicate tuples exist.
- Referential Integrity: Enable foreign key relationships between tables.
- Efficiency: Speed up joins and indexing.
Visual: Key Types in a Relation
Database Decomposition
2. What is Decomposition?
Decomposition is the process of breaking down a relation into smaller relations (tables) to:
- Eliminate redundancy (e.g., repeating groups).
- Improve data integrity.
- Simplify queries.
Example: Decomposing a University Database
Consider a relation StudentCourse(SID, SName, SAddress, CID, CName, Credit_hours) with redundancy (e.g., SName repeats for each course a student takes). Decompose it into:
Student(SID, SName, SAddress)Course(CID, CName, Credit_hours)Enrollment(SID, CID)(junction table for many-to-many relationships).
3. Lossless Decomposition
Decomposition is lossless if the original relation can be reconstructed from the decomposed relations using natural joins. This depends on functional dependencies (FDs).
How to Check for Lossless Decomposition
- Compute the closure of the intersection of attributes between decomposed relations under the given FDs.
- If the closure includes all attributes of at least one of the decomposed relations, the decomposition is lossless.
Example: Lossless vs. Lossy Decomposition
Relation: R(A, B, C) with FDs: A → B, B → C.
- Decomposition 1:
R1(A, B)andR2(B, C).- Intersection:
{B}. - Closure of
{B}:{B, C}(sinceB → C). {B, C}includes all attributes ofR2, so lossless.
- Intersection:
- Decomposition 2:
R1(A, C)andR2(B, C).- Intersection:
{C}. - Closure of
{C}:{C}(no FDs start withC). {C}does not include all attributes ofR1orR2, so lossy.
- Intersection:
4. Dependency Preservation
A decomposition preserves dependencies if all FDs of the original relation can be inferred from the FDs of the decomposed relations. This ensures no information is lost during updates.
Example: Dependency Preservation
Original Relation: R(A, B, C) with FDs: A → B, B → C.
- Decomposition:
R1(A, B)andR2(B, C).- FDs in
R1:A → B. - FDs in
R2:B → C. - Both original FDs are preserved, so the decomposition is dependency-preserving.
- FDs in
Practical Applications: Keys and Decomposition in Real-World Systems
In the Real World
eSewa (Nepal):
- Primary Key:
TransactionIDin theTransactionstable ensures each payment is uniquely identified. - Foreign Key:
UserIDlinks transactions to users in theUserstable, enforcing referential integrity. - Decomposition: The database splits into
Users,Transactions, andServiceProvidersto avoid redundancy (e.g., storing user details repeatedly for each transaction).
- Primary Key:
Khalti (Nepal):
- Candidate Keys: Both
EmailandPhoneNumbercan uniquely identify a user (if no duplicates exist), butPhoneNumberis chosen as the primary key for simplicity. - Normalization: The
Orderstable is decomposed intoCustomers,Products, andOrderItemsto eliminate repeating groups (e.g., multiple products per order).
- Candidate Keys: Both
Nepal Rastra Bank (NRB):
- Superkey: In the
BankAccountstable,{AccountNumber, BranchID}is a superkey becauseAccountNumberalone suffices, but includingBranchIDensures uniqueness across branches. - Decomposition: The
Loanstable is split intoLoanApplicants,LoanTypes, andLoanDisbursementsto separate static (e.g., applicant details) and dynamic (e.g., payment schedules) data.
- Superkey: In the
Worked Example: Decomposing a Bank Database
Original Relation: CustomerAccounts(CID, Name, Address, AcNo, Balance, Branch)
Problem: Redundancy in Name and Address for each account, and repeating Branch details.
Solution: Decompose into:
Customers(CID, Name, Address)Accounts(AcNo, CID, Balance)Branches(BranchID, BranchName, Location)
SQL Implementation:
-- Create decomposed tables
CREATE TABLE Customers (
CID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Address VARCHAR(200)
);
CREATE TABLE Accounts (
AcNo VARCHAR(20) PRIMARY KEY,
CID INT,
Balance DECIMAL(10, 2),
FOREIGN KEY (CID) REFERENCES Customers(CID)
);
CREATE TABLE Branches (
BranchID INT PRIMARY KEY,
BranchName VARCHAR(100),
Location VARCHAR(100)
);
Why This Works:
- Lossless: The original relation can be reconstructed by joining
Customers,Accounts, andBranchesonCIDandAcNo. - Dependency Preservation: All FDs (e.g.,
CID → Name) are preserved in the decomposed tables.
Types of Decomposition
5. Horizontal vs. Vertical Decomposition
| Type | Definition | Example |
|---|---|---|
| Horizontal | Splitting rows based on a condition (e.g., by department). | Employees table split into IT_Employees and HR_Employees. |
| Vertical | Splitting columns into separate tables (e.g., separating core and optional data). | Student table split into StudentCore(SID, SName) and StudentDetails(SID, Address, Email). |
classDiagram
class StudentAll {
+SID: int
+SName: string
+SAddress: string
+CID: int
+CName: string
+Credit_hours: int
}
class Student {
+SID: int
+SName: string
+SAddress: string
}
class Course {
+CID: int
+CName: string
+Credit_hours: int
}
StudentAll --> Student : Vertical split
StudentAll --> Course : Vertical split
Student ||--o{ Enrollment : horizontal split
Course ||--o{ Enrollment : horizontal splitVertical decomposition splits columns; horizontal splits rows (e.g., Enrollment table)Visual: Horizontal vs. Vertical Decomposition
Normalization and Decomposition
6. How Normalization Guides Decomposition
Normalization (1NF, 2NF, 3NF, BCNF) directly influences decomposition:
- 1NF: Eliminate repeating groups (e.g., decompose
Student(Courses)intoStudentandEnrollment). - 2NF: Remove partial dependencies (e.g., decompose
OrderItems(OrderID, ProductID, Quantity, ProductName)intoOrdersandProducts). - 3NF: Remove transitive dependencies (e.g., decompose
Student(SID, SName, Department, DeptHead)intoStudentandDepartment).
Example: Decomposing to 3NF
Original Relation: R(SID, SName, Dept, DeptHead) with FDs: SID → SName, SID → Dept, Dept → DeptHead.
- Problem: Transitive dependency
SID → DeptHeadviaDept. - Decomposition:
Student(SID, SName, Dept)Department(Dept, DeptHead)
- Result: Both tables are in 3NF.
In the real world
- eSewa: Uses candidate keys (
TransactionIDandUserID) to uniquely identify transactions and users. The database is vertically decomposed intoUsers,Transactions, andServiceProviderstables to eliminate redundancy (e.g., storing user details repeatedly for each transaction). - Nepal Rastra Bank (NRB): Employs lossless decomposition in its
Loansystem. TheLoanApplicantstable (candidate key:ApplicantID) is joined withLoanDisbursements(foreign key:ApplicantID) to reconstruct the original relation without data loss, ensuring integrity during updates. - Daraz (Nepal): Implements horizontal decomposition for order processing. The
Orderstable splits intoCustomers,Products, andOrderItemstables, whereOrderItemsstores only rows for products in a specific order (horizontal subset), whileCustomersandProductshold master data.
Exam Tip
Key Identification:
- Always identify candidate keys first by checking for minimal uniqueness.
- Remember: A primary key is just one chosen candidate key.
- Superkeys are larger sets that include candidate keys (e.g.,
{A, B}where{A}is a candidate key).
Lossless Decomposition:
- To prove losslessness, compute the closure of the intersection of attributes and check if it includes all attributes of at least one decomposed relation.
- Example: For
R1(A, B)andR2(B, C), check ifB+includesBandC(it does ifB → Cholds).
SQL Implementation:
- Use
PRIMARY KEY,FOREIGN KEY, andUNIQUEconstraints to enforce keys in decomposed tables. - Example:
CREATE TABLE Orders ( OrderID INT PRIMARY KEY, CustomerID INT, FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) );
- Use
Common Pitfalls:
- Lossy Decomposition: Forgetting to check for losslessness can lead to incorrect queries (e.g., missing tuples after joins).
- Redundant FDs: Not preserving all functional dependencies can cause anomalies (e.g., updates not propagating correctly).
- NULL Values: Primary keys cannot contain
NULL; use default values or surrogate keys (e.g., auto-incremented IDs) if natural keys allowNULL.
Real-World Scenarios:
- Exams often ask about banking systems (e.g., decomposing
AccountsandTransactions) or e-commerce (e.g.,Customers,Products,Orders). - Relate decomposition to normalization levels (e.g., "Decompose this relation to 3NF").
- Use past exam questions as templates:
- Given a relation, decompose it into 3NF.
- Given decomposed tables, write SQL to create them with proper keys.
- Given a scenario (e.g., university database), identify keys and propose a decomposition.
- Exams often ask about banking systems (e.g., decomposing
Based on the TU BBA syllabus for Database Management System (IT232), unit 10.
Discussion
Loading…