Introduction to Relational Database Design
Relational Database Design is the process of organizing data into tables (relations) and columns (attributes) and establishing relationships between them. The primary goals are to:
- Minimize Redundancy: Store each piece of information only once.
- Ensure Data Integrity: Maintain consistency and accuracy of data.
- Avoid Anomalies: Prevent issues during insertion, deletion, and update operations.
- Improve Query Performance: Design a schema that allows efficient data retrieval.
Features of Good Relational Database Design
A well-designed relational database schema exhibits several key features:
- Minimizing Data Redundancy: Duplication of data wastes storage space and can lead to inconsistencies. Good design reduces this by structuring data appropriately.
- Avoiding Update Anomalies: Anomalies are problems that arise when redundant data is not handled properly during data modification operations.
- Insertion Anomaly: Cannot insert new data without also inserting data for unrelated attributes. Example: Cannot add a new department without having an employee assigned to it.
- Deletion Anomaly: Deleting one piece of data inadvertently deletes other, unrelated data. Example: Deleting the last employee of a department also deletes the department's information.
- Update Anomaly: Updating one piece of data requires updating multiple copies, leading to inconsistency if some copies are missed. Example: Changing a department's name requires updating every employee record associated with that department.
- Ensuring Data Integrity: Maintaining the accuracy and consistency of data over its entire life cycle. This includes entity integrity (primary keys are unique and non-null) and referential integrity (foreign keys correctly reference primary keys).
- Flexibility and Scalability: The design should be adaptable to future changes in requirements and capable of handling increasing amounts of data and users.
- Ease of Querying and Reporting: A logical and structured design simplifies writing queries and generating reports, making data access more intuitive for users and applications.
Atomic Domains and First Normal Form (1NF)
Atomic Domains
A domain is atomic if elements of the domain are considered to be indivisible units. This means that each value in the domain cannot be further decomposed into smaller meaningful parts.
Example:
- An integer (e.g.,
123) is atomic. - A string (e.g.,
'John Doe') is atomic in the context of a name, but if we wanted to query by 'first name' or 'last name' separately, it wouldn't be atomic for those individual parts. A good practice is to separate first and last names into different attributes. - A list of phone numbers (e.g.,
'{123-456-7890, 987-654-3210}') within a single attribute is not atomic because it's a collection of values. Each phone number should be a separate entry or in a separate table.
First Normal Form (1NF)
A relation (table) is in First Normal Form (1NF) if and only if all attribute values are atomic (indivisible) and there are no repeating groups of attributes.
Conditions for 1NF:
- Each column must contain only atomic values (no lists, sets, or nested structures within a single cell).
- Each column must contain values of the same data type.
- Each column must have a unique name.
- The order of rows does not matter.
- The order of columns does not matter.
Example of Non-1NF Relation:
STUDENT_COURSE (StudentID, StudentName, Courses)
| StudentID | StudentName | Courses |
|---|---|---|
| 101 | Alice | Math, Physics |
| 102 | Bob | Chemistry |
Here, the Courses attribute contains multiple values (a repeating group), violating 1NF.
Converting to 1NF:
To convert the above table to 1NF, we eliminate the repeating group by creating separate rows for each course or by creating a separate relation.
| StudentID | StudentName | Course |
|---|---|---|
| 101 | Alice | Math |
| 101 | Alice | Physics |
| 102 | Bob | Chemistry |
This table is now in 1NF. Note that the primary key for this table would be (StudentID, Course).
Functional Dependency Theory
Functional dependencies are fundamental to understanding and applying normal forms. They describe relationships between attributes.
Definition of Functional Dependency (FD)
A functional dependency (FD) is a constraint between two sets of attributes in a relation. For a relation R, if A and B are two sets of attributes of R, then B is functionally dependent on A (denoted as A -> B), if for every valid instance of R, whenever two tuples have the same values for A, they must also have the same values for B. In simpler terms, A uniquely determines B.
Key Concepts:
- Determinant: The attribute(s) on the left-hand side of the arrow (A).
- Dependent: The attribute(s) on the right-hand side of the arrow (B).
Example: Consider a Student relation with attributes (StudentID, StudentName, Major, Advisor).
StudentID -> StudentName: If you know a StudentID, you uniquely know the StudentName.StudentID -> Major: A student has only one major.StudentID -> StudentName, Major, Advisor: StudentID determines all other attributes of that student.
Armstrong's Axioms
Armstrong's Axioms are a set of inference rules used to derive all possible functional dependencies from a given set of FDs. They are sound (do not derive incorrect FDs) and complete (can derive all correct FDs).
-
Reflexivity Axiom: If B is a subset of A, then A -> B.
- Explanation: Any set of attributes trivially determines any of its subsets. This is always true.
- Example: If A = {StudentID, StudentName} and B = {StudentName}, then {StudentID, StudentName} -> {StudentName}.
-
Augmentation Axiom: If A -> B, then AC -> BC (where C is any set of attributes).
- Explanation: If A determines B, then adding common attributes C to both sides of the dependency does not change its validity.
- Example: If StudentID -> StudentName, then {StudentID, CourseID} -> {StudentName, CourseID}.
-
Transitivity Axiom: If A -> B and B -> C, then A -> C.
- Explanation: If A determines B, and B in turn determines C, then A indirectly determines C.
- Example: If StudentID -> Major and Major -> DepartmentHead, then StudentID -> DepartmentHead.
Additional Derived Rules: These rules can be proven using Armstrong's Axioms but are often used directly for convenience.
-
Union Rule: If A -> B and A -> C, then A -> BC.
- Explanation: If a set of attributes determines multiple other attributes independently, it also determines their combination.
-
Decomposition Rule: If A -> BC, then A -> B and A -> C.
- Explanation: If a set of attributes determines a combination of attributes, it also determines each of them individually.
-
Pseudotransitivity Rule: If A -> B and BC -> D, then AC -> D.
- Explanation: Similar to transitivity, but with augmentation.
Closure of a Set of FDs (F+)
The closure of a set of functional dependencies F (denoted as F+) is the set of all functional dependencies that can be logically inferred from F using Armstrong's Axioms.
Finding F+ involves iteratively applying Armstrong's Axioms until no new FDs can be derived.
Example: Given R(A, B, C, D) and F = {A -> B, B -> C}.
Initial F+ = {A -> B, B -> C}
- Applying Transitivity: A -> C (from A -> B and B -> C)
- Applying Augmentation: A -> BD (from A -> B), AB -> BC (from A -> B), etc.
- Applying Reflexivity: A -> A, B -> B, etc.
The complete F+ would include all these and further derived dependencies.
Closure of an Attribute Set (X+)
The closure of an attribute set X (denoted as X+) with respect to a set of FDs F is the set of all attributes A such that X -> A can be inferred from F. It helps identify all attributes determined by X.
Algorithm to find X+:
- Initialize
result = X. - Repeat until no new attributes can be added to
result:- For each functional dependency Y -> Z in F:
- If Y is a subset of
result, then add all attributes in Z toresult.
- Return
result.
Example: Given R(A, B, C, D, E) and F = {A -> BC, CE -> D, B -> E}. Find the closure of A (A+).
result = {A}-
Iteration 1:
- A -> BC: A is a subset of
result. Add B, C.result = {A, B, C}. - CE -> D: C is in
result, but E is not. Skip. - B -> E: B is in
result. Add E.result = {A, B, C, E}.
- A -> BC: A is a subset of
-
Iteration 2:
- A -> BC: Y ({A}) is subset of
result. Z ({B,C}) already inresult. - CE -> D: Y ({C, E}) is subset of
result({A, B, C, E}). Add D.result = {A, B, C, D, E}. - B -> E: Y ({B}) is subset of
result. Z ({E}) already inresult.
- A -> BC: Y ({A}) is subset of
-
Iteration 3: No new attributes added.
Therefore, A+ = {A, B, C, D, E}. Since A+ contains all attributes of R, A is a candidate key for R.
Minimal Cover (Canonical Cover)
A minimal cover (or canonical cover) for a set of functional dependencies F is a simplified set of FDs G that is equivalent to F (meaning G+ = F+), but satisfies the following properties:
- Every dependency in G has a single attribute on the right-hand side.
- No dependency in G can be removed without changing G+. (Irreducible)
- No attribute can be removed from the left-hand side of any dependency in G without changing G+. (Non-redundant left-hand side)
Finding a minimal cover is useful for database design and normalization as it provides a minimal set of constraints to consider.
Algorithm (general steps):
-
Split right-hand sides: For each FD X -> Y in F, if Y is a composite attribute, replace it with X -> A1, X -> A2, ..., X -> An, where Y = {A1, A2, ..., An}.
Example: If
A -> BC, replace withA -> BandA -> C. -
Remove extraneous attributes from left-hand sides: For each FD X -> A in the current set G, and for each attribute B in X, check if
(X - {B}) -> Acan be inferred from F. If it can, remove B from X. Repeat until no more attributes can be removed.Example: If
ABC -> DandAC -> Dis derivable using the rest of FDs, thenBis extraneous, andABC -> Dcan be replaced byAC -> D. -
Remove redundant FDs: For each remaining FD X -> A in G, check if X -> A can be inferred from
G - {X -> A}(i.e., from all other FDs in G). If it can, remove X -> A from G.Example: If
G = {A -> B, B -> C, A -> C}, thenA -> Cis redundant because it can be derived fromA -> BandB -> C. RemoveA -> C.
The resulting set G is a minimal cover for F.
Decomposition Using Functional Dependencies
Decomposition is the process of breaking down a large, complex relation into smaller, simpler relations to improve database design by eliminating redundancy and anomalies.
Need for Decomposition
When a relation is not in a desirable normal form (e.g., it contains partial or transitive dependencies), it can suffer from update anomalies. Decomposing the relation into multiple smaller relations can resolve these issues, making the database more efficient and consistent.
Desirable Properties of Decomposition
When decomposing a relation, it's crucial to ensure that certain properties are maintained to avoid losing information or constraints.
Lossless Join Decomposition
A decomposition of a relation R into relations R1, R2, ..., Rn is a lossless-join decomposition if the natural join of all the decomposed relations (R1 JOIN R2 JOIN ... JOIN Rn) yields exactly the original relation R, without generating any spurious tuples (extra rows that were not in the original relation).
Condition for Lossless Join: For a decomposition of R into R1 and R2 to be lossless with respect to a set of FDs F, at least one of the following must hold:
(R1 intersection R2) -> R1is in F+(R1 intersection R2) -> R2is in F+
The common attributes between R1 and R2 must functionally determine all other attributes in at least one of the decomposed relations.
Dependency Preservation
A decomposition of a relation R with FDs F into relations R1, R2, ..., Rn is dependency-preserving if all functional dependencies from the original set F can be enforced by simply enforcing the functional dependencies in each of the decomposed relations Ri. This means the union of FDs in each Ri (F1 U F2 U ... U Fn) is equivalent to F (i.e., (F1 U F2 U ... U Fn)+ = F+).
Why it's important: If a decomposition is not dependency-preserving, we might have to check dependencies across multiple tables, which can be inefficient or impossible for the DBMS to enforce automatically.
Decomposition to Third Normal Form (3NF)
The goal of 3NF decomposition is to eliminate transitive dependencies while preserving all functional dependencies and ensuring a lossless join.
Algorithm Sketch for 3NF Decomposition (Synthesis Approach):
- Find a minimal cover G for the set of FDs F.
- For each FD
X -> Ain G, create a relation schemaRi = {X U A}. - If none of the created schemas Ri contains a candidate key of the original relation R, then add a new relation schema
Rkconsisting of a candidate key of R.
This algorithm guarantees a lossless-join and dependency-preserving decomposition into 3NF.
Decomposition to Boyce-Codd Normal Form (BCNF)
BCNF is a stricter normal form than 3NF. Decomposition into BCNF aims to eliminate all FDs where the determinant is not a superkey.
Algorithm Sketch for BCNF Decomposition:
- Start with a relation R that is not in BCNF.
- Find a functional dependency
X -> Yin R that violates BCNF (i.e., X is not a superkey of R, and X -> Y is non-trivial). - Decompose R into two relations:
R1 = (X U Y)andR2 = (R - Y) U X. - Repeat the process for R1 and R2 until all relations are in BCNF.
Important Note: Decomposition into BCNF is always lossless-join, but it might *not* always be dependency-preserving. If dependency preservation is a strong requirement, 3NF might be preferred over BCNF in some cases, or a compromise might be sought.
Multivalued Dependencies (MVDs)
Definition of Multivalued Dependency (MVD)
A multivalued dependency (MVD) is a constraint that requires certain tuples to be present in a relation. For a relation R with attributes A, B, and C, an MVD A ->-> B (read as 'A multidetermines B') exists if, for any two tuples in R that have the same value for A, then the set of B-values associated with A is independent of the set of C-values associated with A.
In simpler terms, if A ->-> B, it means that for a given value of A, there is a set of B values associated with it, and this set of B values is independent of other attributes (C) in the relation. MVDs capture the fact that two or more independent, multivalued facts about an entity can coexist in a single table.
Example: Consider an EMPLOYEE_SKILL_PROJECT relation (EmployeeID, Skill, Project).
An employee can have multiple skills, and an employee can work on multiple projects. These two facts (skills and projects) might be independent of each other for a given employee. If EmployeeID 'E1' has skills {'S1', 'S2'} and works on projects {'P1', 'P2'}, the table might contain:
| EmployeeID | Skill | Project |
|---|---|---|
| E1 | S1 | P1 |
| E1 | S1 | P2 |
| E1 | S2 | P1 |
| E1 | S2 | P2 |
Here, EmployeeID ->-> Skill and EmployeeID ->-> Project are MVDs. This redundancy leads to storage waste and anomalies.
Functional Dependency vs. Multivalued Dependency
While FDs imply a one-to-one or many-to-one relationship, MVDs describe one-to-many relationships where the 'many' is independent of other attributes.
| Feature | Functional Dependency (FD) | Multivalued Dependency (MVD) |
|---|---|---|
| Notation | A -> B | A ->-> B |
| Relationship Type | A determines a single B value. (One-to-one or Many-to-one) | A determines a set of B values, independent of other attributes. (One-to-many) |
| Implication | Every FD (A -> B) is a trivial MVD (A ->-> B). | An MVD (A ->-> B) is not necessarily an FD. If A ->-> B and C is the empty set, it is trivial. If B is all attributes of R minus A, it is trivial. |
| Anomaly Type | Leads to redundancy and update anomalies if not in 3NF/BCNF. | Leads to redundancy if independent multivalued facts are stored together (e.g., employee skills and projects in one table). |
| Normalization Level | Addressed by 2NF, 3NF, BCNF. | Addressed by 4NF. |