0% found this document useful (0 votes)
16 views12 pages

DBMS Unit-4

Uploaded by

Bhagya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views12 pages

DBMS Unit-4

Uploaded by

Bhagya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SEM :- 2-2 (R20) DBMS UNIT-4

SYLLABUS
1. Schema Refinement (Normalization):

2. Purpose of Normalization or schema refinement,

3. concept of functional dependency,

4. normal forms based on functional dependency(1NF, 2NF and

3 NF), concept of surrogate key,

5. Boyce-codd normal form(BCNF),

6. Lossless join and dependency preserving decomposition,

7. Fourth normal form(4NF),

8. Fifth Normal Form (5NF)

web-D Page 1
SEM :- 2-2 (R20) DBMS UNIT-4

Purpose of Normalization:

1. Minimizing Redundancy:
o Redundancy refers to the repetition of the same data or duplicate copies stored in
different places.
o Normalization eliminates redundancy by breaking down large tables into smaller,
related tables.
2. Avoiding Anomalies:
o Normalization helps avoid insertion, update, and deletion anomalies:
 Insertion Anomaly: Difficulty in inserting data due to missing related
information.
 Update Anomaly: Inconsistencies when updating data (e.g., changing an
employee’s salary in one place but not everywhere).
 Deletion Anomaly: Unintended loss of information when deleting data (e.g.,
deleting an employee also removes their department information).
3. Improving Consistency:
o Normalized schemas ensure that data is consistent and accurate across the entire
database.

Example Program Using SQL:

Let’s consider an example with a simplified table called Employees:

CREATE TABLE Employees (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
department_id INT,
salary DECIMAL(10, 2)
-- Other employee attributes...
);

web-D Page 2
SEM :- 2-2 (R20) DBMS UNIT-4

Suppose we want to refine this schema to minimize redundancy and avoid anomalies.
We’ll follow the normalization process:

1. First Normal Form (1NF):


o Ensure atomicity (no repeating groups).
o All attributes should be single-valued.
o No composite or multi-valued attributes.
o Our Employees table is already in 1NF.
2. Second Normal Form (2NF):
o Remove partial dependencies (attributes dependent on part of the primary key).
o Create a separate table for dependent attributes.
o Suppose we have a functional dependency: {emp_id, department_id} →
salary.

o We’ll create a new table for department information:


o CREATE TABLE Departments (

o department_id INT PRIMARY KEY,

o department_name VARCHAR(50)

o -- Other department attributes...

o );

3. Third Normal Form (3NF):


o Remove transitive dependencies (attributes dependent on non-key attributes).
o Our schema is already in 3NF.

Certainly! Let’s explore SQL programs for each of the First Normal Form (1NF),
Second Normal Form (2NF), and Third Normal Form (3NF):

1. First Normal Form (1NF):


o A table is in 1NF if:
 Each attribute contains only one value (atomicity).

web-D Page 3
SEM :- 2-2 (R20) DBMS UNIT-4

 There is a primary key for identification.


 There are no duplicated rows or columns.
 Each column has only one value for each row in the table.

CREATE TABLE Students (


student_id INT PRIMARY KEY,
student_name VARCHAR(50) NOT NULL,
-- Other student attributes...
);

Second Normal Form (2NF):

o To be in 2NF, a relation must be in 1NF and meet the following rules:


 No partial dependency.
 Every non-key attribute is fully dependent on the primary key.

CREATE TABLE Orders (


order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
-- Other order attributes...
);

CREATE TABLE Customers (


customer_id INT PRIMARY KEY,
customer_name VARCHAR(100),
-- Other customer attributes...
);

Third Normal Form (3NF):

o A relation is in 3NF if it meets the following conditions:


 It’s already in 1NF and 2NF.

web-D Page 4
SEM :- 2-2 (R20) DBMS UNIT-4

 No transitive dependency for non-prime attributes.


2. CREATE TABLE Employees (
3. emp_id INT PRIMARY KEY,
4. emp_name VARCHAR(50),
5. department_id INT,
6. salary DECIMAL(10, 2),
7. -- Other employee attributes...
8. );

Certainly! Let’s explore the concept of functional dependency and provide an example
using SQL.

Functional Dependency:

 A functional dependency (FD) is a relationship between two attributes within a table.


 It specifies that the value of one attribute (the dependent) is determined by the value of
another attribute (the determinant).
 In other words, if X → Y, it means that knowing the value of X uniquely determines the
value of Y.
 Functional dependencies are essential for designing efficient and organized database
schemas.

Example Program Using SQL:

Suppose we have a table called Employees with the following columns: emp_id,
emp_name, and salary.

CREATE TABLE Employees (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
salary DECIMAL(10, 2)
-- Other employee attributes...

web-D Page 5
SEM :- 2-2 (R20) DBMS UNIT-4

);

In this example:

 The table is in 1NF because each attribute contains only one value (atomicity).
 There is a primary key (emp_id) for identification.
 The salary attribute is directly dependent on the primary key (emp_id).

Certainly! Let’s explore the concepts of surrogate keys and the Boyce-Codd Normal
Form (BCNF) in the context of database design.

Surrogate Key:

 A surrogate key is a unique identifier used in databases for an entity or an object.


 It is not derived from application data but is generated by the system.
 The sole purpose of a surrogate key is to act as the primary identifier of an object or
entity.
 Surrogate keys are often used as primary keys in database tables.
 They provide a simple, system-generated, business-agnostic column for uniquely
identifying rows.

Features of Surrogate Keys:

1. Automatically Generated: The system generates surrogate keys automatically when a


new record is inserted into a table.
2. Anonymous Integer: Surrogate keys often hold anonymous integer values.
3. Uniqueness: Each surrogate key value is unique across all records in the table.
4. Immutable: Users or applications cannot modify the surrogate key value.
5. Factless Key: Surrogate keys contain no relevant fact or information useful for the table;
they exist purely for identification.

Example:
web-D Page 6
SEM :- 2-2 (R20) DBMS UNIT-4

Suppose we have two tables representing students from different schools:

Table A (School A):

registration_no name percentage


210101 Harry 90
210102 Maxwell 65
210103 Lee 87
210104 Chris 76

Table B (School B):

registration_no name percentage


CS107 Taylor 49
CS108 Simon 86
CS109 Sam 96
CS110 Andy 58

Now, suppose we want to merge the details of both schools into a single table. We can
create a surrogate key (surr_no) to uniquely identify each row:

surr_no registration_no name percentage


1 210101 Harry 90
2 210102 Maxwell 65
3 210103 Lee 87
4 210104 Chris 76
5 CS107 Taylor 49
6 CS108 Simon 86
7 CS109 Sam 96
8 CS110 Andy 58

web-D Page 7
SEM :- 2-2 (R20) DBMS UNIT-4

In this example, the registration_no cannot be the primary key because it does not
match all records in the table. Therefore, we create a surrogate key (surr_no) to uniquely
identify each row.

Surrogate Key Example:

Suppose we have a table called Students with the following columns: student_id,
student_name, and courses_enrolled.

CREATE TABLE Students (


student_id INT PRIMARY KEY,
student_name VARCHAR(50) NOT NULL,
courses_enrolled VARCHAR(100),
-- Other student attributes...
);

In this example:

 The table is in 1NF because each attribute contains only one value (atomicity).
 There is a primary key (student_id) for identification.
 The courses_enrolled attribute may contain multiple courses, but it’s stored as a
single value (not a repeating group).

Boyce-Codd Normal Form (BCNF):

 BCNF is a stronger normal form than the Third Normal Form (3NF).
 It ensures that every determinant (attribute determining another attribute) is a
superkey (a candidate key).
 BCNF eliminates redundancy caused by dependencies that violate one or more
candidate keys.

web-D Page 8
SEM :- 2-2 (R20) DBMS UNIT-4

 It is based on functional dependencies and additional constraints compared to 3NF.

Certainly! Let’s explore SQL programs for the concepts of surrogate keys and the
Boyce-Codd Normal Form (BCNF).

BCNF Example:

Suppose we have a table representing student courses and their instructors:

CREATE TABLE CourseInstructors (


course_id INT PRIMARY KEY,
instructor_id INT,
course_name VARCHAR(100),
instructor_name VARCHAR(50),
-- Other course and instructor attributes...
);

In this example:

 The course_id and instructor_id are candidate keys.


 The table satisfies BCNF because every determinant (attribute determining another
attribute) is a superkey.

Certainly! Let’s delve into the concepts of lossless join, dependency-preserving


decomposition, Fourth Normal Form (4NF), and Fifth Normal Form (5NF) in the context of
database normalization.

Lossless Join and Dependency Preserving Decomposition:


 Decomposition of a relation is performed when it is not in an appropriate normal form.
 A relation R is decomposed into two or more relations if the decomposition is both
lossless join and dependency preserving.
 Let’s break down these terms:
1. Lossless Join Decomposition:
 If we decompose a relation R into relations R1 and R2, the decomposition is
lossless join if:

web-D Page 9
SEM :- 2-2 (R20) DBMS UNIT-4

 R1 ⋈ R2 = R (i.e., the natural join of R1 and R2 results in the original


relation R).
 No spurious or extra tuples are generated when relations are reunited
through a natural join.
 To check for lossless join decomposition using the functional dependency (FD)
set, the following conditions must hold:
 The union of attributes of R1 and R2 must be equal to the attribute set
of R.
 The intersection of attributes of R1 and R2 must not be empty (i.e.,
common attributes must exist).
 The common attribute must be a key for at least one relation (R1 or R2).
2. Dependency Preserving Decomposition:
 If we decompose a relation R into relations R1 and R2, all dependencies of R
must either be a part of R1 or R2, or must be derivable from a combination of
functional dependencies of R1 and R2.
 These decomposition techniques help maintain data integrity, reduce redundancy, and
ensure that all dependencies are preserved.

Fourth Normal Form (4NF):


 4NF is a further refinement of BCNF (Boyce-Codd Normal Form).
 It ensures that a table does not contain any multi-valued dependencies.
 A multi-valued dependency occurs when an attribute depends on another attribute, but not on
the entire primary key.
 4NF eliminates redundancy caused by such dependencies.

Fifth Normal Form (5NF):


 5NF is the highest level of normalization.
 It involves decomposing a table into smaller tables to remove data redundancy and improve
data integrity.
 In 5NF, every join dependency in a relation is implied by the candidate keys of that relation.

Examples:

Lossless Join and Dependency Preserving Decomposition:

o Suppose we have two tables representing student data from different schools:
 Table A (School A):

web-D Page 10
SEM :- 2-2 (R20) DBMS UNIT-4

registration_no name percentage


210101 Harry 90
210102 Maxwell 65
210103 Lee 87
210104 Chris 76

 Table B (School B):

registration_no name percentage


CS107 Taylor 49
CS108 Simon 86
CS109 Sam 96
CS110 Andy 58

o We want to merge the details of both schools into a single table. The resulting
table will have a surrogate key (surr_no) to uniquely identify each row.
2. Fourth Normal Form (4NF):

o Suppose we have a table representing employee projects:

o CREATE TABLE EmployeeProjects (

o emp_id INT,

o project_name VARCHAR(100),

o skills_required VARCHAR(200),

o PRIMARY KEY (emp_id, project_name)

o );

o The table contains multi-valued dependencies because skills required for a


project depend on the employee working on it.
o We need to decompose this table to eliminate these dependencies..
o

web-D Page 11
SEM :- 2-2 (R20) DBMS UNIT-4

Fifth Normal Form (5NF):

Suppose we have a table representing a library system:

CREATE TABLE LibrarySystem (


book_id INT PRIMARY KEY,
book_title VARCHAR(100),
author_name VARCHAR(100),
borrower_id INT,
due_date DATE
);

web-D Page 12

You might also like