1) ER Diagrams: Mapping Cardinality Representation Using ER Diagram
1) ER Diagrams: Mapping Cardinality Representation Using ER Diagram
ii) One to many :When entity A is associated with more than one entities at a
time then there is one to many relation. For example - One customer places
order at a time.
iii) Many to one : When more than one entities are associated with only one
entity then there is is many to one relation. For example – Many student take a
ComputerSciCourse
Ternary Relationship
The relationship in which three entities are involved is called ternary
relationship. For example -
The SQL statement captures the information for above ER diageam as follows -
CREATE TABLE Employee( EmpID CHAR(11),
EName CHAR(30),
Salary INTEGER,
PRIMARY KEY(EmpID))
The SQL statement captures the information for relationship present in above
ER diagram as follows -
CREATE TABLE Works In (EmpID CHAR(11),
DeptID CHAR(11),
EName CHAR(30),
Salary INTEGER,
DeptName CHAR(20),
Building CHAR(10),
PRIMARY KEY(EmpID,DeptID),
FOREIGN KEY (EmpID) REFERENCES Employee,
FOREIGN KEY (DeptID) REFERENCES Department
)
Here the constraint is each department has at the most one manager to manage
it. Hence no two tuples can have same DeptID. Hence there can be a separate
table named Manages with DeptID as Primary Key. The table can be defined
using following SQL statement
CREATE TABLE Manages (EmpID CHAR(11),
DeptID INTEGER,
Since DATE,
PRIMARY KEY (DeptID),
FOREIGN KEY (EmpID) REFERENCES Employees,
FOREIGN KEY (DeptID) REFERENCES Departments)
Approach 2:
• In this approach, it is preferred to translate a relationship set with key
constraints.
• It is a superior approach because, it avoids creating a distinct table for the
relationship set.
• The idea is to include the information about the relationship set in the table
corresponding to the entity set with the key, taking advantage of the key
constraint.
• This approach eliminates the need for a separate Manages relation, and
queries asking for a department's manager can be answered without combining
information from two relations.
• The only drawback to this approach is that space could be wasted if several
departments have no managers.
• The following SQL statement, defining a Dep_Mgr relation that captures
the information in both Departments and Manages, illustrates the second
approach to translating relationship sets with key constraints:
CREATE TABLE Dep_Mgr (DeptID INTEGER,
DName CHAR(20),
Budget REAL,
EmpID CHAR (11),
since DATE,
PRIMARY KEY (DeptID),
FOREIGN KEY (EmpID) REFERENCES Employees)
Relational Mapping
The specialialization/Generalization relationship (Enhanced ER Construct) can
be mapped to database tables(relations) using three methods. To demonstrate
the methods, we will take the - InventoryItem, Book, DVD
Method 1: All the entities in the relationship are mapped to individual tables
InventoryItem(ID, name)
Book(ID,Publisher)
DVD(ID, Manufacturer)
Method 2: Only subclasses are mapped to tables. The attributes in the
superclass are duplicated in all subclasses. For example -
Book(ID,name, Publisher)
DVD(ID, name, Manufacturer)
Method 3: Only the superclass is mapped to a table. The attributes in the
subclasses are taken to the superclass. For example -
InventoryItem(ID, name, Publisher, Manufacturer)
This method will introduce null values. When we insert a Book record in the
table, the Manufacturer column value will be null. In the same way, when we
insert a DVD record in the table, the Publisher value will be null.
3)Normal Forms
• Normalization is the process of reorganizing data in a database so that it meets
two basic requirements:
1) There is no redundancy of data (all data is stored in only one place), and
2) data dependencies are logical (all related data items are stored together)
Need for normalization
1) It eliminates redundant data.
2) It reduces chances of data error.
3) The normalization is important because it allows database to take up less
disk space.
4) It also help in increasing the performance.
5) It improves the data integrity and consistency.
As there are multiple values of phone number for sid 1 and 3, the above table is
not in 1NF. We can make it in 1NF. The conversion is as follows -
This table is not in 2NF. For converting above table to 2NF we must follow the
following steps -
Step 1: The above table is in 1NF.
Step 2: Here sname and sid are associated similarly cid and cname are
associated with each other. Now if we delete a record with sid=2, then
automatically the course C++ will also get deleted. Thus,
sid->sname or cid->cname is a partial functional dependency, because {sid,cid}
should be essentially a candidate key for above table. Hence to bring the above
table to 2NF we must decompose it as follows:
Student:
Course:
Thus now table is in 2NF as there is no partial functional dependency
Superkeys
• {RegID}
• {RegID, RollNo}
• {RegID,Sname}
• {RollNo,Sname}
• {RegID, RollNo,Sname}
Candidate Keys
• {RegID}
• {RollNo}
Third Normal Form
A table is said to be in the Third Normal Form when,
i) It is in the Second Normal form.(i.e. it does not have partial functional
dependency)
ii) It doesn't have transitive dependency.
Or in other words
In other words 3NF can be defined as: 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 :
i) X is a super key of table
ii) Y is a prime attribute of table
For example: Consider following table Student_details as follows -
Here
Super keys: {sid}, {sid,sname}, {sid,sname,zipcode},
{sid,zipcode,cityname}... and so on.
Candidate keys:{sid}
Non-Prime attributes: {sname,zipcode,cityname,state}
The dependencies can be denoted as,
sid->sname
sid->zipcode
zipcode->cityname
cityname->state
The above denotes the transitive dependency. Hence above table is not in 3NF.
We can convert it into 3NF as follows:
Student
Zip
Multivalued Dependencies and Fourth Normal Form
• A table is said to have multi-valued dependency, if the following conditions
are true,
1) For a dependency A→B, if for a single value of A, multiple values of B
exists, then the table may have multi-values dependency.
2) Also, a table should have at-least 3 columns for it to have a multi-valued
dependency.
3) And, for a relation R(A,B,C), if there is a multi-valued dependency between,
A and B, then B and C should be independent of each other.
Fourth Normal Form
Definition: For a table to satisfy the Fourth Normal Form, it should satisfy the
following two conditions:
1) It should be in the Boyce-Codd Normal Form(BCNF).
2) And, the table should not have any multi-valued dependency.
For example: Consider following student relation which is not in 4NF as it
contains multivalued dependency.
Student Table
Now to convert the above table to 4NF we must decompose the table into
following two tables.
Student_Course Table
Key: (sid,Course)
Student Skill Table
Key: (sid, Skill)
Course