F Edit Chapter 3
F Edit Chapter 3
DATABASE DESIGN
The Entity-Relationship (E-R) data model, which is popular for high level database
design, provides a means for representing relationships between entities. This approach was
profounded by P.P. Chen in 1976. The salient features of the E-R model are represented in
the following section:
This is used to give structure to the data.
Model can be evolved independent of any DBMS.
It is an aid for database design.
It is easy to visualize and understand.
The database structure, employing the E-R model is usually depicted pictorially using
entity-relationship (E-R) diagram as shown in Fig. 2.1.
Attributes
Relationship
An entity has set of properties, and the values for some set of properties may
uniquely identify an entity.
For example person may a have a person-id property whose value uniquely
identities that person.
The individual entities in an entity set are called the extension of the entity set.
All the individual bank customers are the extension of the entity set customer.
(b) Attributes
In the customer entity customer id, name, street are the attributes.
(i) Simple attribute: An attribute that cannot be divided into further subparts.
(ii) Composite attribute: An attribute that can be divided into a set of subparts.
Example: In a customer entity, the attribute name can further be divided into
first-name, middle-name, last-name.
(iii) Single value attribute: An attribute having only one value in a particular
entity.
Example: In a customer entity, name, id, street are single valued attributes.
3.3 Database Management Systems
(iv) Multi-valued attribute: An attribute having more than one value for a
particular entity.
Example: Consider the customer entity set with the attribute phone no. A
customer may have zero, one or several phone nos and different customers
may have different numbers of phone.
(v) Derived attribute: An attribute that is derived from other related attributes or
entities.
For example, the age of a customer entity set is derived from the attribute
date-of-birth of a customer.
Same entity set participates in a relationship set more than once, in different roles is
called recursive relationship set.
For example, employee entity set participates in relationship set works for as manager
or worker. A relationship set may also have descriptive attributes. For example, consider the
relationship set depositor with entity sets customer and account. Here we would associate the
attribute access date to the relationship to specify the most recent date on which a customer
accessed an account.
The number of entity sets that participate in a relationship set is called the degree of
relationship set.
Relationship between 2 entity sets are called binary relationship set.
Medical
3.1.2 Constraints
An E-R enterprise schema may define certain constraints to which the constraints to
which the contents of a database must conform.
Two types of constraints are
(i) Mapping cardinalities
(ii) Participation constraint
(i) Mapping Cardinalities
For a binary relationship set R between entity sets A and B, the mapping co-ordinality
must be one of the following
(i) One-to-one (1 : 1)
(a) (b)
(ii)One-to-many (1 : M)
(iii)Many-to-Many (M : N)
(c) (d)
Fig. 3.3 Many-to-Many & Many-to-One
(ii) Participation Constraint
It specifies whether the existence of an entity depends on its being related to another
entity via the relationship type.
Types
Eg. If a company policy states that every employee must work for a department, then
an employee entity can exist only if it participates in a WORKS – FOR relationship instance.
Database Design 3.6
Entity types that do not have key attributes of their own are called weak entity types.
A weak entity type always has a total participation constraint (existence dependency)
with respect to its identifying relationship, because a weak entity cannot be identified without
an owner entity type.
A weak entity set is indicated in E-R diagrams by a doubly outlined rectangular box
and the corresponding identifying relationship by a doubly outlined diamond.
Entity types that have key attributes of their own are called strong entity types. A
strong entity set is indicated in E-R diagrams by rectangular box and its relationship by a
diamond.
3.2 ER DIAGRAMS
The logical representation of the overall logical structure of a database is called as E-R
diagram.
Entity
Weak entity
Relationship
Identifying relationship
Attribute
Key attribute
Multivalued
Composite attribute
Derived attribute
E1 R E2
Total participation of E2 in R
1 N
E1 R E2
Cardinality ratio 1:N for E1:E2 in R
Database Design 3.8
(min,
max)
R E
Structural constraint (min,max) on participation of E in R
Locations
Bdate (1, 1) WORKS_FOR (4, N)
Sex Employee Department Name
Ssn
Name
Locations
Number
DEPENDENTS_OF
(1, 1) Dependent
DEPENDENT
EXAMPLE:
College Management System
1. A college contains many departments
2. Each department can offer any number of courses
3. Many instructors can work in a department
4. An instructor can work only in one department
5. For each department there is a Head
6. An instructor can be head of only one department
7. Each instructor can take any number of courses
8. A course can be taken by only one instructor
9. A student can enroll for any number of courses
10. Each course can have any number of students
Step 1 : Identify the Entities
1. Department
2. Course
3. Instructor
4. Student
Stem 2 : Identify the relationships
1. One department offers many courses. But one particular course can be offered by only one
department. hence the cardinality between department and course is One to Many (1:N)
2. One department has multiple instructors . But instructor belongs to only one department.
Hence the cardinality between department and instructor is One to Many (1:N)
3. One department has only one head and one head can be the head of only one department.
Hence the cardinality is one to one. (1:1)
4. One course can be enrolled by many students and one student can enroll for many courses.
Hence the cardinality between course and student is Many to Many (M:N)
5. One course is taught by only one instructor. But one instructor teaches many courses.
Hence the cardinality between course and instructor is Many to One (N :1)
The E-R diagrams discussed so far represents the basic concepts of a database schema.
However, some aspects of a database such as inheritance among various entity types cannot be
expressed using the basic E-R model. These aspects can be expressed by enhancing the E-R
3.13 Database Management Systems
model. The resulting diagrams are known as enhanced E-R or EER diagrams and the model
is called EER model.
The basic E-R model can represent the traditional database applications such as typical
data processing application of an organization effectively. On the other hand, the EER model
is used to represent the new and complex database applications such as telecommunications,
Geographical Information Systems (GIS), etc. This section discusses the extended E-R
features including specialization, generalization, and aggregation and their representation
using EER diagrams.
Specialization/generalization
Subclass/super class
Categories
Attribute inheritance
In some cases, and entity type has numerous sub-groupings of its entities that are
meaningful, and need to be explicitly represented, because of their importance.
For example, members of entity Employee can be grouped further into Secretary,
Engineer, Manager, Technician, Salaried_Employee.
The set listed is a subset of the entities that belong to the Employee entity, which
means that every entity that belongs to one of the sub sets is also an Employee.
Each of these sub-groupings is called a subclass, and the Employee entity is called
the super-class.
Database Design 3.14
Engineer
Secretary
Type Inheritance
Because an entity in a subclass represents the same entity from the super class,
it should possess all the values for its attributes, as well as the attributes as a
member of the super class.
This means that an entity that is a member of a subclass inherits all the attributes of the
entity as a member of the super class; as well, an entity inherits all the relationships in which
the super class participates.
Specialization
Wor Department
Employee
kFor
Belon Professional
gs To
Organization
Reasons for Specialization
Certain attributes may apply to some but not all entities of a super class. A
subclass is defined in order to group the entities to which the attributes apply.
The second reason for using subclasses is that some relationship types may be
participated in only by entities that are members of the subclass.
Database Design 3.16
Summary of Specialization
Allows for:
Defining set of subclasses of entity type
Create additional specific attributes for each sub class
Create additional specific relationship types between each sub class and other
entity types or other subclasses.
Generalization
The reverse of specialization is generalization.
Several classes with common features are generalized into a super class.
For example, the entity types Car and Truck share common attributes
License_PlateNo, VehicleID and Price, therefore they can be generalized into the
super class Vehicle.
Constraints on Specialization and Generalization
Several specializations can be defined on an entity type.
Entities may belong to subclasses in each of the specializations.
The specialization may also consist of a single subclass, such as the manager
specialization, in this case we don’t use the circle notation.
Types of Specializations
Occurs in cases where we can determine exactly the entities of each sub class by
placing a condition of the value of an attribute in the super class.
An example is where the Employee entity has an attribute, Job Type. We can
specify the condition of membership in the Secretary subclass by the condition,
JobType=”Secretary”
Another Example:
University Incom
e
Income > 0
Instructor
Student
Predicate defined subclasses are displayed by writing the predicate condition next
to the line that connects the subclass to the specialization circle.
Attribute-defined specialization
User-defined specialization
Disjointness/Overlap Constraint
Specifies that the subclass of the specialization must be disjoint, which means
that an entity can be a member of, at most, one subclass of the specialization.
Overlap means that an entity can be a member of more than one subclass of the
specialization.
Completeness Constraint
Total specialization is shown by using a double line to connect the super class
to the circle.
a) Disjoint, total
Department
Academic Administrative
b) Disjoint, partial
Employee
c) Overlapping, total
Part
Manufactured Puchased
d) Overlapping, partial
Movie
3.5.1 Introduction
Relational database design requires that we find a “good” collection of relation
schemas.
Pit-falls in Relational Database Design
A bad design may lead to
(a) Repetition of information - that leads to insertion, deletion, updation problems.
(b) Inability to represent certain information.
Database Design 3.20
Design goals
(a) Avoid redundant data.
(b) Ensure that relationships among attributes are represented.
(c) Facilitate the checking of updates for violation of database integrity constraints.
Example
Consider the relation schema:
Lending-schema (branch_name, branch_city, assets,
customer_name, loan_no, amount)
branch_ name branch_ assets Customer_ Laon_no amount
city name
Here branch Downtown details are represented 2 times. This leads to a redundancy
problem.
Redundancy
Data for branch_name, branch_city, assets are repeated for each loan that a branch makes:
(a)wastes space
(b) complicates updating, introducing inconsistency of assets value.
Null Values
(a) Cannot store information about a branch if no loan exist.
(b) Can use null values, but they are difficult to handle.
Decomposition
Decompose the relation-schema, lending schema into
Branch-schema (branch_name, branch_city, assets)
Loan-schema (customer_name, loan_no, branch_name, amount)
All attributes of original schema (R) must appear in decomposition (R1, R2).
R = R1 R2
3.21 Database Management Systems
Goal: To decide whether a particular relation R is in ‘good’ form, decompose it into a set of
relations (R1, R2, ..., Rn) such that
It requires that the value for a certain set of attributes determines uniquely the
value for another set of attributes.
XY
For example, in a student relation the value of an attribute “Marks” is known then the
value of an attribute “Grade” is determined since
Marks Grade.
Types
Student_no
Marks
Course_no
X Y
Y Z
X Z
For example, grade depends on marks and in turn make depends on {student_no
course_no}, hence Grade depends fully transitively on {student_no & course_no}.
Test relations to see if they are legal under a given set of functional dependencies.
Loan_no customer_name
3.6 NORMALIZATION
Normalizing a logical database design involves organizing the data into more than one
table. Normalization improves performance by avoiding the redundancy. Redundancy can
lead to:
• Inconsistencies - Errors are more likely to occur when facts are repeated.
There is a high likelihood of data in one table being updated or deleted, while
corresponding changes in other relations are omitted.
Normalization has numerous benefits. These include faster sorting and index creation,
few indexes per table, few NULLs and an increase in the compactness of the database.
However the number and complexity of joins increase with the increase in normalization. If
the number of joins between table increases, the performance of the database may deteriorate.
Normalization helps to simplify the structure of tables. The performance of an application is
directly linked to the data base design. A poor design hinders the performance of the system.
The logical design of the database lays the foundation for an optimal database.
Some rules that should be followed to achieve a good database design are:
Normal Forms:
The data in the table is not normalized because a cell in ProjCode and Hours has more
then one value.
By applying the INF definition to the project table, you arrive at the following table.
Project:
Ecode Dept Proj Code Hours
3.25 Database Management Systems
• Insertion
Database Design 3.26
• Updating
For giving employee, the employee code and department are repeated several
times. Hence if an employee is transferred to another department, this change
will have to be recorded in every row of the employee table. Any omission will
lead to inconsistencies.
• Deletion
The table satisfies the definition of 1 NF. You need to now check if it satisfies 2NF.
In the table for each value of ECode, there is more than one value of Hours. For
example, for ECode, E101, there are three values of Hours: 90, 101 and 60. Hence, Hours is
not functionally dependent on ECode. Similarly, for each value of ProjCode, there is more
than one value of Hours. For example for ProjCode, P27 there is three values of Hours, 90, 10
and 72. However, for a combination of the ECode and ProjCode values, there is exactly one
value of Hours. Hence Hours is functionally dependent on the whole key, ECode + ProjCode.
i.e. Ecode Hours and Projcode Hours.
Now you are must check it Dept is functionally dependent on the whole key,
ECode+ProjCode. For each value of ECode, there is exactly one value of Dept. For example,
for ECode 101, there is exactly one value the systems department. Hence, Dept is functionally
department on ECode i.e. Ecode Dept. However, for each value of ProjCode, there is more
than one value of Dept. For example, for ProjCode P27, there are two values of Dept, System
and Finance. Hence, Dept is not functionally dependent on ProjCode. Dept is not functionally
dependent on ProjCode. Dept is, therefore, functionally dependent on part of the key (which is
ECode) and not functionally dependent on the whole key (ECode+ProjCode). Therefore the
table Project is not in 2NF. For the table to be in 2NF, the non-key attributes must be fully
functionally dependent on the whole key and not part of the key.
• Find and remove attributes that are functionally dependent on only a part of the
key and not on the whole key. Place them in a different table.
To convert the table Project into 2NF, you must remove the attributes that are not fully
functionally dependent on the whole key and place them in a different table along with the
attribute that it is functionally dependent on. In the above example, since Dept is not fully
functionally dependent on the whole key ECode+ProjCode, you place Dept along with ECode
in a separate table called EmployeeDept.
Now the table Project will contain ECode, ProjCode and Hours.
EmployeeDept:
Ecode Dept
E101 Systems
E305 Sales
E508 Admin
Project
Ecode Proj Code Hours
E101 P27 90
E101 P51 101
E101 P20 60
E305 P27 10
E508 P51 Null
E508 P27 72
You must check if the table is in 3NF. Since each cell in the table has the single
value, the table is in 1NF.
The primary key in Employee table is ECode. For Each value of Ecode, there is
exactly one value of Dept. Hence the attribute Dept is functionally dependent on the primary
key ECode i.e. Ecode Dept. Similarly for each value of ECode, there is exactly one value
of DeptHead. Hence DeptHead is functionally dependent on the primary key ECode. Hence
all the attributes are functionally dependent on the whole key, ECode. Hence the table is in
2NF.
However, the attribute DeptHead is dependent on the attribute Dept also.
i.e. Dept Depthead As per 3NF, all non-key attributes have to be functionally dependent
3.29 Database Management Systems
only on the primary key. This table is not in 3NF since DeptHead is functionally dependent on
Dept, which is not a primary key.
Guidelines for Converting a Table to 3NF
• Find and remove non-key attributes that are functionally dependent on the
attributes that are not the primary key. Place them in a different table.
• Group the remaining attributes.
To convert the table employee into 3NF, you must remove the column DeptHead since
it is not functionally dependent on only the primary key ECode and place it in another table
called Department along with the attribute dependent on.
Employee
Ecode Dept
E101 Systems
E305 Finance
E402 Sales
E508 Admin
E607 Finance
E608 Finance
Department
Ecode DeptHead
E101 E901
E305 E909
E402 E906
E508 E908
E607 E909
E608 E909
Database Design 3.30
The original definition of 3NF was inadequate in some situation. It was not
satisfactory for the tables:
That had multiple candidate keys
Where the multiple candidate key were composite.
Where the multiple candidate key overlapped (Had at least one attribute in
common)
Hence, a new normal form - the Boyce-Codd normal form was introduced. You must
understand that in table were the above three condition do not apply, you can stop at the third
normal form. In such cases, the third NF is the same as the Boyce-Codd normal form.
A relation is in the Boyce-Codd normal form (BCNF) if and only if every determinant
is a candidate key.
Consider the table Project given below.
Project
Ecode Name Proj Code Hours
E1 Veronica P2 48
E2 Anthony P5 100
E3 Mac P6 15
E4 Susan P3 250
E4 Susan P5 75
E1 Veronica P5 40
This table has redundancies. If the name of an employee is changed, the change will
have to be made in every row of the table, otherwise there will be inconsistencies.
ECode+ProjCode is the primary key. You will notice that Name+ProjCode could be
chosen as the primary key and hence, is a candidate key.
* Hours is functionally dependent on ECode+ProjCode.
* Hours is also functionally dependent on Name+ProjCode.
* Name is functionally dependent on Ecode.
* ECode Is functionally dependent on Name.
You will notice that this table has:
• Multiple candidate keys, that is ECode+ProjCode and Name+ProjCode.
3.31 Database Management Systems
• Find the remove the overlapping candidate keys. Place the part of the candidate
key and the attribute it is functionally dependent on, in a different table.
• Group the remaining items into a table.
Hence, remove Name and ECode and place them in a different table. You will arrive at
the following tables.
Employee
Ecode Name
E1 Veronica
E2 Anthony
E3 Mac
E4 Susan
E4 Susan
E1 Veronica
Project
Definition
Given a set of dependencies F on R, the projection of F on R i, denoted by Ri(F)
where Ri-subset of R, is the set of dependencies X Y in F+ such that the attributes in XUY
are all condition in Ri.
Hence the projection of F on each relation schema R i in the decomposition D is the set of
FDs in F+, such that all their left and RHS attributes are in Ri.
A decomposition D={R1, R2…Rm} of R is dependency preserving with respect to F if
the union of the properties of F on each Ri on D is equivalent to F.
(ie)
R i , (R) ..... R m (F) ) F
[Link] Lossless (non additive) Joins
This property ensures that no spurious tuples are generated when a natural join
operation is applied to the relations in the decomposition.
3.33 Database Management Systems
A decomposition D={R1, R2…Rm}of R has the lossless (non additive) join property
with respect to the set of dependencies F on r if, for every relation state r of R that satisfies F,
the following holds, where
is the natural join of all the relations in D.
(Ri (r), ……Rm (r)) = r
A schema that is in 3NF but not in BCNF has the problems of:
If two tuples t1 and t2 exist in r such that t1 [x] = t2 [x], then two tuples t3 and t4 should
also exist in r with the following properties.
Database Design 3.34
The EMP relation of Fig. 10.1 (a) is not in 4NF because in the nontrivial MVDs
ENAME PNAME and ENAME DNAME, ENAME is not a superkey of EMP. We
decompose EMP into EMP – PROJECTS and EMP - DEPENDENTS, shown in Fig. (b). Both
EMP - PROJECTS and EMP - DEPENDENTS are in 4NF, because the MVDs ENAME
PNAME in EMP - PROJECTS and ENAME DNAME in EMP - DEPENDENTS are
trivial MVDs. No other nontrivial MVDs hold in either EMP - PROJECTS or EMP -
DEPENDENTS.
EMP
(a)
ENAME PNAME DNAME
Dharshan X Prajan
Dharshan Y Pradeep
Dharshan X Pradeep
Dharshan Y Prajan
3.35 Database Management Systems
EMP-PROJECTS EMP-DEPENDENTS
(b)
ENAME PNAME ENAME DNAME
Property LJ
The relation schemas R1 and R2 form a lossless join decomposition of R if and only if
(R1 R2) (R1 – R2 ).
Algorithm
Input
A universal relation R and a set of FDs and multivalued dependencies F.
1. SetD:={R};
2. While there is a relation schema Q in D that is not in 4NF do
{
choose a relation schema Q in D that is not in 4NF;
Find a nontrivial MVD X Y in Q that violates 4NF.
replace Q in D by two relations schemas (Q – Y) and (X Y);
};
Database Design 3.36
The normal forms discussed so far required that the given relation R if not in the given
normal form be decomposed in two relations to meet the requirements of the normal form. In
some rare cases, a relation can have problems like redundant information and update
anomalies because of it but cannot be decomposed in two relations to remove the problems. In
such cases it may be possible to decompose the relation in three or more relations using the
5NF.
The fifth normal form deals with join-dependencies which is a generalisation of the
MVD. The aim of fifth normal form is to have relations that cannot be decomposed further. A
relation in 5NF cannot be constructed from several smaller relations.
A relation R satisfies join dependency (R1, R2, ..., Rn) if and only if R is equal to the
join of R1, R2, ..., Rn where Ri are subsets of the set of attributes of R.
A relation R is in 5NF (or project-join normal form, PJNF) if for all join
dependencies at least one of the following holds.
(a) (R1, R2, ..., Rn) is a trivial join-dependency (that is, one of Ri is R)
An example of 5NF can be provided by the example below that deals with departments,
subjects and students.
The above relation says that Comp. Sc. offers subjects CP1000, CP2000 and CP3000
which are taken by a variety of students. No student takes all the subjects and no subject has
all students enrolled in it and therefore all three fields are needed to represent the information.
3.37 Database Management Systems
The above relation does not show MVDs since the attributes subject and student are
not independent; they are related to each other and the pairings have significant information in
them. The relation can therefore not be decomposed in two relations
The idea behind DKNF is to specify the “ultimate normal form” that takes into account
all possible types of dependencies and constraints.
A relation is said to be in DKNF if all constraints and dependencies that should hold
on the relation can be enforced simply by enforcing the domain constraint and key constraint
on the relation.
For a relation in DKNF, it becomes very straight forward to enforce all data base
constraints by simply checking that each attribute value in a tuple is of the appropriate domain
and that every key constraint is enforced.
3.7.11 De-normalization
A normalized design will often store different but related pieces of information in
separate logical tables (called relations). If these relations are stored physically as separate
disk files, completing a database query that draws information from several relations (a join
operation) can be slow. If many relations are joined, it may be prohibitively slow. There are
two strategies for dealing with this. The preferred method is to keep the logical design
Database Design 3.38
normalized, but allow the database management system (DBMS) to store additional redundant
information on disk to optimize query response. In this case it is the DBMS software's
responsibility to ensure that any redundant copies are kept consistent. This method is often
implemented in SQL as indexed views (Microsoft SQL Server) or materialized views
(Oracle). A view represents information in a format convenient for querying, and the index
ensures that queries against the view are optimized.
The more usual approach is to denormalize the logical data design. With care this can
achieve a similar improvement in query response, but at a cost—it is now the database
designer's responsibility to ensure that the denormalized database does not become
inconsistent. This is done by creating rules in the database called constraints, that specify how
the redundant copies of information must be kept synchronized. It is the increase in logical
complexity of the database design and the added complexity of the additional constraints that
make this approach hazardous. Moreover, constraints introduce a trade-off, speeding up reads
(SELECT in SQL) while slowing down writes (INSERT, UPDATE, and DELETE). This
means a denormalized database under heavy write load may actually offer worse performance
than its functionally equivalent normalized counterpart.
A denormalized data model is not the same as a data model that has not been
normalized, and denormalization should only take place after a satisfactory level of
normalization has taken place and that any required constraints and/or rules have been created
to deal with the inherent anomalies in the design. For example, all the relations are in third
normal form and any relations with join and multi-valued dependencies are handled
appropriately.
Star schemas, which are also known as fact-dimension models and have been extended
to snowflake schemas
PART - A
PART – B
8. Explain the Join dependency and fifth normal form with example.
9. Draw an ER diagram for Hospital management system.
10. What is Functional Dependency? Explain types and properties of FD’s.
11. Construct an ER diagram for university registrar’s office. The office maintains
data about each class, including the instructor, the enrollment and the time and
place of the class meetings. For each student class pair a grade is recorded.
Determine the entities and relationships.
12. Define generalization and aggregation. Demonstrate generalization and
aggregation using E-R diagram.