Unit4
Unit4
Anomalies in DBMS
There are three types of anomalies that occur when the database is not
normalized. These are – Insertion, update and deletion anomaly. Let’s take an
example to understand this.
Delhi
The above table is not normalized. We will see the problems that we face
when a table is not normalized.
Update anomaly: In the above table we have two rows for employee Rick as
he belongs to two departments of the company. If we want to update the
address of Rick then we have to update the same in two rows or the data will
become inconsistent. If somehow, the correct address gets updated in one
department but not in other then as per the database, Rick would be having
two different addresses, which is not correct and would lead to inconsistent
data.
Insert anomaly: Suppose a new employee joins the company, who is under
training and currently not assigned to any department then we would not be
able to insert the data into the table if emp_dept field doesn’t allow nulls.
Functional Dependency:
roll_n
o name dept_name dept_building
42 abc CO A4
43 pqr IT A3
44 xyz CO A4
45 xyz IT A3
46 mno EC B2
47 jkl ME B2
Example –
In the relation ABC->D, attribute D is fully functionally dependent on
ABC and not on any proper subset of ABC. That means that subsets
of ABC like AB, BC, A, B, etc cannot determine D.
Supply table
supplier_i
item_id price
d
1 1 540
2 1 545
1 2 200
2 2 201
1 1 540
2 2 201
3 1 542
From the table, we can clearly see that neither supplier_id nor item_id can
uniquely determine the price but both supplier_id and item_id together can
do so. So we can say that price is fully functionally dependent on
{ supplier_id, item_id }. This summarizes and gives our fully functional
dependency −
{ supplier_id , item_id } -> price
Partial Functional Dependency :
A functional dependency X->Y is a partial dependency if Y is
functionally dependent on X and Y can be determined by any proper
subset of X.
For example, we have a relationship AC->B, A->D, and D->B.
Now if we compute the closure of {A +}=ADB
Here A is alone capable of determining B, which means B is partially
dependent on AC.
Student table
Name roll_no Course
Ravi 2 DBMS
Tim 3 OS
John 5 Java
Here, we can see that both the attributes name and roll_no alone are able to
uniquely identify a course. Hence we can say that the relationship is partially
dependent.
Example:
Example:
1. ID → Name,
2. Name → DOB
Example:
Example:
Car_model Maf_year Color
H001 2017 Metallic
H001 2017 Green
H005 2018 Metallic
H005 2018 Blue
H010 2015 Metallic
Car_model Maf_year Color
H033 2012 Gray
In this example, maf_year and color are independent of each other but
dependent on car_model. In this example, these two columns are said to
be multivalue dependent on car_model.
car_model->-> colour
Normalization
Here are the most commonly used normal forms:
9900012222
9990000123
104 Lester Bangalore 8123450987
Example: Suppose a company wants to store the names and contact details of
its employees. It creates a table that looks like this:
Two employees (Jon & Lester) are having two mobile numbers so the
company stored them in the same field as you can see in the table above.
This table is not in 1NF as the rule says “each attribute of a table must have
atomic (single) values”, the emp_mobile values for employees Jon & Lester
violates that rule.
To make the table complies with 1NF we should have the data like this:
111 Maths 38
111 Physics 38
222 Biology 38
333 Physics 40
333 Chemistry 40
Example: Suppose a school wants to store the data of teachers and the
subjects they teach. They create a table that looks like this: Since a teacher
can teach more than one subjects, the table can have multiple rows for a
same teacher.
To make the table complies with 2NF we can break it in two tables like this:
teacher_details table:
teacher_id teacher_age
111 38
222 38
333 40
teacher_subject table:
teacher_id subject
111 Maths
111 Physics
222 Biology
333 Physics
333 Chemistry
Now the tables comply with Second normal form (2NF).
In other words 3NF can be explained like this: A table is in 3NF if it is in 2NF
and for each functional dependency X-> Y at least one of the following
conditions hold:
d me p te ty ct
22200 Chenn
1002 Ajeet TN M-City
8 ai
29200
1101 Lilly UK Pauri Bhagwan
8
22299
1201 Steve MP GwaliorRatan
9
To make this table complies with 3NF we have to break the table into two
tables to remove the transitive dependency:
employee table:
employee_zip table:
Jhansi K.Das C
Subbu R.Prasad C
Teacher Subject
P.Naresh Database
K.DAS C
R.Prasad C
R2
Student Teacher
Jhansi P.Naresh
Jhansi K.Das
Subbu P.Naresh
Subbu R.Prasad
emp_idemp_nationalityemp_dept dept_typedept_no_of_emp
1001 Austrian Production and planning D001 200
The table is not in BCNF as neither emp_id nor emp_dept alone are keys.
emp_id emp_nationality
1001 Austrian
1002 American
To make the table comply with BCNF we can break the table in three tables
like this:
emp_nationality table:
emp_dept table:
emp_id emp_dept
1001 Stores
emp_dept_mapping table:
Functional dependencies:
emp_id -> emp_nationality
emp_dept -> {dept_type, dept_no_of_emp}
Candidate keys:
For first table: emp_id
For second table: emp_dept
For third table: {emp_id, emp_dept}
This is now in BCNF as in both the functional dependencies left side part is a
key.
11. 3NF can be achieved without losing any For obtaining BCNF we may lose
S.NO
. 3NF BCNF
information from the old table. some information from old table.
Table – ACP
Agen
t Company Product
A1 PQR Nut
Agen
t Company Product
A1 PQR Bolt
A1 XYZ Nut
A1 XYZ Bolt
A2 PQR Nut
The relation ACP is again decompose into 3 relations. Now, the natural Join
of all the three relations will be shown as:
Table – R1
Agen
t Company
A1 PQR
A1 XYZ
A2 PQR
Table – R2
Agent Product
A1 Nut
Agent Product
A1 Bolt
A2 Nut
Table – R3
Compan
y Product
PQR Nut
PQR Bolt
XYZ Nut
XYZ Bolt
Result of Natural Join of R1 and R3 over ‘Company’ and then Natural Join of
R13 and R2 over ‘Agent’and ‘Product’ will be table ACP.
Hence, in this example, all the redundancies are eliminated, and the
decomposition of ACP is a lossless join decomposition. Therefore, the
relation is in 5NF as it does not violate the property of lossless join.
Surrogate Key
A surrogate key in DBMS is the key or can say a unique identifier that
uniquely identifies an object or an entity in their respective fields. It is the
unique identifier in a database.
Consider an example :
Suppose we have two tables of two different schools having the same
column registration_no , name and percentage , each table having its own
natural primary key, that is registration_no.
Table of school A –
210101 Harry 90
210102 Maxwell 65
210103 Lee 87
210104 Chris 76
Table of school B –
registration_n
name percentage
o
CS107 Taylor 49
CS108 Simon 86
CS109 Sam 96
CS110 Andy 58
Now, suppose we want to merge the details of both the schools in a single
table .
Resulting table will be –
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
As we can observe the above table and see that registration_no cannot be
the primary key of the table as it does not match with all the records of the
table though it is holding all unique values of the table . Now , in this case,
we have to artificially create one primary key for this table. We can do this by
adding a column surr_no in the table that contains anonymous integers and
has no direct relation with other columns . This additional column of surr_no
is the surrogate key of the table.
Advantages of the surrogate key :
As there is no direct information related with the table, so the changes are
only based on the requirements of the application.
Performance is enhanced as the value of the key is relatively smaller.
The key value is guaranteed to contain unique information .
As it holds smaller constant values , this makes integration of the table
easy .
Enables us to run fast queries (as compared to the natural primary key)
Disadvantages of the surrogate key :
The surrogate key value can never be used as a search key.
As the key value has no relation to the data of the table, so third normal
form is violated.
The extra column for surrogate key will require extra disk space.
We will need extra IO when we have to insert or update data of the table.
Some examples of Surrogate key are :
System date & time stamp
Random alphanumeric string