28.4 KB · commit 40bb182
Rajshahi University of Engineering & Technology
Md. Rakibul Islam
Lecturer, Dept. of ETE, RUET
Heaven's Light is Our Guide
| Student_ID | Student_Name | Course | Instructor | Instructor_Phone |
|---|---|---|---|---|
| S01 | Riad | DBMS | Dr. Hasan | 017... |
| S01 | Riad | AI | Dr. Abdullah | 018... |
| S02 | Zoha | DBMS | Dr. Hasan | 017... |
Notice that:
Problem: Redundant data can cause inconsistencies.
Solution: Database normalization.
Normalization is the process of organizing data in a database to:
Row-level duplication refers to entirely identical records across multiple rows. Row-level duplication occurs when an entire horizontal record (across all columns) is repeated.
| Student_ID | Student_Name | Course | Instructor | Instructor_Phone |
|---|---|---|---|---|
| S01 | Riad | DBMS | Dr. Hasan | 017... |
| S01 | Riad | DBMS | Dr. Hasan | 017... |
| S02 | Zoha | DBMS | Dr. Hasan | 017... |
Solution: Primary Key.
Column-level duplication happens vertically within a single attribute field, or horizontally when two separate columns share redundant information. A single column repeats the same value across different rows.
Problem: There are three types of Anomalies.
Solution: Normalization.
| Student_ID | Student_Name | Course | Instructor | Instructor_Phone |
|---|---|---|---|---|
| S01 | Riad | DBMS | Dr. Hasan | 017... |
| S01 | Riad | AI | Dr. Abdullah | 018... |
| S02 | Zoha | DBMS | Dr. Hasan | 017... |
Update Anomaly
The same information must be updated in multiple rows.
Insertion Anomaly
We cannot insert information unless some unrelated information is also available.
Deletion Anomaly
Deleting one piece of information accidentally removes another important piece of information.
| Student_ID | Student_Name | Course | Instructor | Instructor_Phone |
|---|---|---|---|---|
| S01 | Riad | DBMS | Dr. Hasan | 017... |
| S01 | Riad | AI | Dr. Abdullah | 018... |
| S02 | Zoha | DSA | Dr. Hasan | 017... |
Suppose Dr. Hasan changes to another instructor.
We must update multiple rows.
If one row is not updated:
→ Inconsistent data
This is an update anomaly.
| Student_ID | Student_Name | Course | Instructor | Instructor_Phone |
|---|---|---|---|---|
| S01 | Riad | DBMS | Dr. Hasan | 017... |
| S01 | Riad | AI | Dr. Abdullah | 018... |
| S02 | Zoha | DSA | Dr. Hasan | 017... |
Suppose a new course is introduced:
C04 - Machine Learning
But no student has enrolled yet.
If the table requires Student_ID:
We cannot insert the course information without a student.
This is an Insertion Anomaly
Normalization separates independent entities into different relations.
| Student_ID | Student_Name | Course | Instructor | Instructor_Phone |
|---|---|---|---|---|
| S01 | Riad | DBMS | Dr. Hasan | 017... |
| S01 | Riad | AI | Dr. Abdullah | 018... |
| S02 | Zoha | DSA | Dr. Hasan | 017... |
If S01 drops the AI course and we delete the row:
→ Information about AI course may also disappear.
This is a:
Deletion Anomaly
A key is an attribute or a combination of attributes used to uniquely identify records (tuples) in a relational table.
Main Types of Keys:
| Student_ID | Name | Department | |
|---|---|---|---|
| 101 | a@gmail.com | Akib | ECE |
| 102 | b@gmail.com | Shuvo | ECE |
| 103 | c@gmail.com | Sakib | ETE |
Possible keys:
| Student_ID | Course_ID | Semester | Grade |
|---|---|---|---|
| 101 | CSE101 | Spring | A |
| 101 | CSE102 | Spring | B+ |
| 102 | CSE101 | Spring | A- |
Composite Key={Student_ID,Course_ID}
A functional dependency (FD) describes a relationship between attributes.
Notation:
X→Y
Read as:
X determines Y.
| Student_ID | Student_Name | Department |
|---|---|---|
| S01 | Ferdous | CSE |
| S02 | Zaman | ETE |
| S03 | Nabila | ECE |
If the attributes on the right side are already part of the left side: {Student_ID,Student_Name}→Student_Name This is a Trivial FD.
If the right-side attribute is not part of the left side: Student_ID→Student_Name This is a Non-Trivial FD.
An attribute depends on the entire composite key, not just part of it.
Example: {Student_ID,Course_ID}→Grade The grade depends on both the student and the course.
A non-key (non-prime) attribute depends on only part of a composite key.
Example: {Student_ID,Course_ID}→Student_Name But: Student_ID→Student_Name Therefore, Student_Name depends only on part of the composite key.
A Prime Attribute is an attribute that is a member of at least one Candidate Key of a relation.
An attribute that does not belong to any Candidate Key is called a Non-Prime Attribute.
A Transitive Dependency occurs when a non-key (non-prime) attribute depends on another non-key (non-prime) attribute, which in turn depends on the primary key.
Example:
STUDENT(Student_ID,Student_Name,Dept_ID,Department_Name)
Functional Dependencies:
Candidate key={Student_ID}
Dept_ID and Department_Name both are non-prime attribute.
So a non-prime attribute depends on another non-prime attribute.
Armstrong’s Axioms are a set of rules used to infer new Functional Dependencies (FDs) from existing FDs.
Suppose we have attributes A,B,C.
If B⊆A, then: A→B
Example: {Student_ID,Name}→Student_ID Because Student_ID is already part of the left-hand side.
If: A→B then: AC→BC
Example:
If:
Student_ID→Student_Name
Then:
{Student_ID,Course_ID}→{Student_Name,Course_ID}
If: A→B and B→C then: A→C
Example:
Student_ID→Dept_ID
Dept_ID→Department
Therefore:
Student_ID→Department
If: A→B A→C Then: A→BC
Example:
If:
Student_ID→Name
Student_ID→Department
Then:
Student_ID→{Name,Department}
If: A→BC Then: A→B and A→C
Example:
Student_ID→{Name,Department}
can be decomposed into:
Student_ID→Name
Student_ID→Department
If: A→B and CB→D Then: CA→D
Example:
Student_ID→Dept_ID
and
{Dept_ID,Semester}→Faculty
Therefore:
{Student_ID,Semester}→Faculty
| Property | Rule | Meaning |
|---|---|---|
| Reflexivity | A→B, if B⊆A | A determines its subset |
| Augmentation | A→B⇒AC→BC | Add the same attributes to both sides |
| Transitivity | A→B,B→C⇒A→C | Dependency can be transferred |
| Union | A→B,A→C⇒A→BC | Combine dependencies |
| Decomposition | A→BC⇒A→B,A→C | Split dependencies |
| Pseudotransitivity | A→B,CB→D⇒CA→D | Extended transitivity |
The closure of an attribute set, written as F+ or X+, is the set of all attributes that can be functionally determined by X using a given set of Functional Dependencies (FDs).
If: X+={A,B,C,D}
it means that starting with attributes X, we can determine A, B, C, and D.
Attribute closure is useful for:
Given Relation
R(A,B,C,D,E)FD={A→B,B→C,C→D,D→E}
Find: A+
Since A+ contains all attributes of R, A is a Super Key.
If A is also minimal, then A is a Candidate Key.
B+={B,C,D,E} C+={C,D,E} D+={D,E} E+={E}
CK={A} Prime attribute={A} Non-prime attribute={B,C,D,E}
Given Relation
R(A,B,C,D)FD={A→B,B→C,C→D,D→A}
A+={A,B,C,D} B+={B,C,D,A} C+={C,D,A,B} D+={D,A,B,C}
CK={A,B,C,D} Prime attribute={A,B,C,D} Non-prime attribute={null}
Given Relation
R(A,B,C,D,E)FD={A→B,B→C,CD→E}
AD+={A,D,B,C,E}
CK={AD} Prime attribute={A,D} Non-prime attribute={B,C,E}
Given Relation
R(A,B,C,D,E)FD={A→B,BC→D,E→C,D→A}
E+={E,C} AE+={A,B,E,C,D} DE+={D,A,B,E,C} BE+={B,E,C,D,A} CE+={C,E}
CK={AE,DE,BE} Prime attribute={A,B,D,E} Non-prime attribute={C}
A relation is in 1NF when:
| Student_ID | Name | Phone |
|---|---|---|
| S01 | Fahim | 0171, 0182 |
| S02 | Noor | 0193 |
| Student_ID | Name | Phone |
|---|---|---|
| S01 | Fahim | 0171 |
| S01 | Fahim | 0182 |
| S02 | Noor | 0193 |
Consider:
ENROLLMENT(Student_ID,Course_ID,Student_Name,Course_Name,Grade)
Primary Key: (Student_ID,Course_ID)
Dependencies:
Student_ID→Student_Name
Course_ID→Course_Name
(Student_ID,Course_ID)→Grade
A relation is in 2NF if:
| Student_ID | Course_ID | Student_Name | Course_Name | Grade |
|---|---|---|---|---|
| S01 | C01 | Santo | DBMS | A |
| S02 | C02 | Ratul | AI | B+ |
Problem:
Student_Name depends only on Student_ID.
Course_Name depends only on Course_ID.
Therefore, partial dependencies exist.
The relation is NOT in 2NF.
| Student_ID | Student_Name |
|---|---|
| S01 | Santo |
| S02 | Ratul |
| Course_ID | Course_Name |
|---|---|
| C01 | DBMS |
| C02 | AI |
| Student_ID | Course_ID | Grade |
|---|---|---|
| S01 | C01 | A |
| S02 | C02 | B+ |
| Customer_ID | Store_ID | Location |
|---|---|---|
| 1 | 1 | R |
| 1 | 3 | S |
| 2 | 1 | R |
| 3 | 2 | I |
| 4 | 3 | S |
Primary Key: (Customer_ID,Store_ID)
Dependencies:
Store_ID→Location
Here partial dependencies exist.
The relation is NOT in 2NF.
| Store_ID | Location |
|---|---|
| 1 | R |
| 2 | I |
| 3 | S |
| Customer_ID | Store_ID |
|---|---|
| 1 | 1 |
| 1 | 3 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
Given Relation
R(A,B,C,D,E,F)FD={A→B,C→F,EC→D,E→A}
CE+={C,E,F,A,B,D}
CK={CE} Prime attribute={C,E} Non-prime attribute={A,B,D,F}
Given Relation
R(A,B,C,D,E,F)FD={A→B,C→F,EC→D,E→A}
The relation is NOT in 2NF.
A relation is in 3NF if:
| Student_ID | Student_Name | Department_ID | Department_Name |
|---|---|---|---|
| S01 | Santo | D01 | ECE |
| S02 | Ratul | D02 | ETE |
| Student_ID | Student_Name | Department_ID |
|---|---|---|
| S01 | Santo | D01 |
| S02 | Ratul | D02 |
| Department_ID | Department_Name |
|---|---|
| D01 | ECE |
| D02 | ETE |
| Roll | Division | District | Thana |
|---|---|---|---|
| 1 | Rajshahi | Bogura | Nandigram |
| 2 | Dhaka | Faridpur | Kotwali |
| 3 | Chattogram | Cox's Bazar | Ramu |
FD={Roll→Division,Division→District,District→Thana}
CK={Roll} Prime attribute={Roll} Non-prime attribute={Division,District,Thana}
Here Division determines District and District determines Thana. In both case transitive dependencies exits as a non prime attributes determines another non-prime attributes.
The relation is NOT in 3NF.
Given Relation
R(A,B,C,D)FD={AB→C,C→D}
AB+={A,B,C,D}
CK={AB} Prime attribute={A,B} Non-prime attribute={C,D}
The relation is NOT in 3NF.
Given Relation
R(A,B,C,D)FD={AB→CD,D→A}
AB+={A,B,C,D} DB+={D,B,A,C}
CK={AB,BD} Prime attribute={A,B,D} Non-prime attribute={C}
The relation is in 3NF.
BCNF is a stronger version of 3NF.
A relation is in BCNF if:
For every non-trivial functional dependency X→Y, X must be a super key or candidate key.
In other words, Every determinant must be a candidate key/super key.
| 3NF | BCNF |
|---|---|
| Less strict | More strict |
| Allows some dependencies where determinant isn't a super key | Every determinant must be a super key |
| Usually easier to achieve | May require further decomposition |
| Dependency preservation is often easier | Dependency preservation may sometimes be lost |
R(Student,Course,Instructor)
Functional Dependencies: {Student,Course}→Instructor,Instructor→Course
Candidate key: {StudentCourse,StudentInstructor}
| Functional Dependency | Is LHS a Super Key? | BCNF |
|---|---|---|
| {Student,Course}→Instructor | Yes | ✓ |
| Instructor→Course | No | ✗ |
The relation is NOT in BCNF.
Given Relation
R(A,B,C,D)FD={AB→CD,D→A}
AB+={A,B,C,D} DB+={D,B,A,C}
CK={AB,BD} Prime attribute={A,B,D} Non-prime attribute={C}
The relation is NOT in BCNF.
Given Relation
R(A,B,C,D,E)FD={A→B,B→C,AC→D,D→E}
A+={A,B,C,D,E}
CK={A}
| FD | Is LHS a Super Key? | BCNF |
|---|---|---|
| A→B | Yes | ✓ |
| B→C | No | ✗ |
| AC→D | Yes, because AC contains A | ✓ |
| D→E | No | ✗ |
The relation is NOT in BCNF.
| Normal Form | Main Condition |
|---|---|
| 1NF | Atomic values |
| 2NF | 1NF + No partial dependency |
| 3NF | 2NF + No transitive dependency |
| BCNF | Every determinant is a super key or candidate key |
Consider:
ENROLLMENT(Student_ID,Student_Name,Course_ID,Course_Name,Instructor_ID,Instructor_Name,Grade)
Assume:
Normalization provides:
Normalization is not always perfect.
There are some disadvantages also:
Therefore:
Good database design requires a balance between normalization and performance.
Denormalization is the process of intentionally adding redundant data or combining normalized tables to improve database read performance.
It is generally performed after normalization when query performance becomes more important than minimizing redundancy.
Denormalization is useful when:
| Normalization | Denormalization |
|---|---|
| Reduces redundancy | Introduces controlled redundancy |
| Improves data consistency | Improves read performance |
| Requires more tables | May combine tables |
| May require more JOINs | Reduces JOIN operations |
| Useful for OLTP systems | Often useful for OLAP/reporting systems |
The term decomposition refers to the process in which we break down a table in a database into various elements or parts. Thus, decomposition replaces a given relation with a collection of various smaller relations. Thus, in a database, we can make any table break down into multiple tables when we want to collect a particular set of data.
The process in which where we can regain the original relation R with the help of joins from the multiple relations formed after decomposition. This process is termed as lossless decomposition.
It is used to remove the redundant data from the database while retaining the useful information. The lossless decomposition tries to ensure following things:
A decomposition is lossy if joining the decomposed relations produces:
Suppose relation:
R is decomposed into: R1 and R2
The decomposition is lossless if:
(R1∩R2)→R1or(R1∩R2)→R2
The common attributes between the two relations must functionally determine all attributes of at least one relation.
Consider:
R(A,B,C)Functional dependency: A→B
Decompose R into: R1(A,B) and R2(A,C)
Common attribute: R1∩R2={A}
Since: A→AB Therefore: A→R1
Hence:
The decomposition is Lossless.
A decomposition is dependency preserving if all functional dependencies can be checked without joining the decomposed relations.
Suppose:
R(A,B,C)Functional dependencies: A→B, B→C
Decompose into: R1(A,B) and R2(B,C)
Both dependencies can still be enforced directly:
Therefore:
The decomposition is Dependency Preserving.
Consider:
R(A,B,C)Functional dependencies: A→B, B→C
Suppose we decompose into:
R1(A,B) and R2(A,C)
The dependency:
B→C cannot be checked directly in either relation.
Therefore:
The decomposition is NOT dependency preserving.
Decomposition may be required to remove:
Decomposition removes:
Decomposition removes:
Decomposition removes:
3NF always ensure dependency preserving decomposition but not in BCNF
Check whether this decomposition given is a lossy or lossless join decomposition.
Relational Schema=R(X,Y,Z)
Decompositions,
R1(X,Y)
R2(X,Z)
Relation R(X,Y,Z):
| X | Y | Z |
|---|---|---|
| X1 | Y1 | Z1 |
| X2 | Y1 | Z1 |
| X1 | Y2 | Z2 |
| X1 | Y3 | Z3 |
Decomposition R1(X,Y):
| X | Y |
|---|---|
| X1 | Y1 |
| X2 | Y1 |
| X1 | Y2 |
| X1 | Y3 |
Decomposition R2(X,Z):
| X | Z |
|---|---|
| X1 | Z1 |
| X2 | Z1 |
| X1 | Z2 |
| X1 | Z3 |
Solution: Now, this decomposition will be lossless if R=R1⋈R2
Thus, R1⋈R2 will be equal to
R1⋈R2:
| X | Y | Z |
|---|---|---|
| X1 | Y1 | Z1 |
| X1 | Y1 | Z2 |
| X2 | Y1 | Z1 |
| X1 | Y2 | Z2 |
| X1 | Y2 | Z1 |
| X1 | Y3 | Z3 |
| X1 | Y3 | Z1 |
Original Relation R:
| X | Y | Z |
|---|---|---|
| X1 | Y1 | Z1 |
| X2 | Y1 | Z1 |
| X1 | Y2 | Z2 |
| X1 | Y3 | Z3 |
As R1⋈R2 is not equal to R, so it is Lossy Decomposition