DMS UNIT 2 (1)
DMS UNIT 2 (1)
DMS UNIT 2 (1)
Candidate key In a relation there may be a key or combination of keys which uniquely
identify the record. Such a key is called as Candidate key.
Example : Consider a Student table (Rollno,Name,Percentage), if (Rollno) and(
Name)both are unique then both are identified as candidate keys. OR Consider a Student
table (Rollno,Name,Percentage), if (Rollno ,Name) is unique , then (Rollno, Name) can be
a candidate key if and only if Name and Rollno individually are not unique.
Foreign Key:-
A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY is a field
(or collection of fields) in one table that refers to the PRIMARY KEY in another table. It
acts as a cross-reference between tables because it references the primary key of another
table, thereby establishing a link between them.
Domain Constraint –
It is used to maintain value according to user specification For example: Not null, check
constraint.
1) Not Null: By default, all columns in tables allows null values. When a NOT NULL
Constraint is enforced on column or set of columns it will not allow null values.
Example SQL> CREATE TABLE STUDENT (ROLL_NO NUMBER (5), NAME
VARCHAR2 (20) NOT NULL);
2) Check Constraint: The constraint defines a condition that each row must satisfy. A
single Column can have multiple check condition.
Example SQL> CREATE TABLE EMP (ID NUMBER (5), NAME VARCHAR2 (10),
SAL NUMBER (10) CHECK (SAL>15000));
3) The DEFAULT constraint provides a default value to a column when there is no value
provided while inserting a record into a table.
Example SQL> CREATE TABLE STUDENT (ROLL_NO NUMBER (5), NAME
VARCHAR2 (20) NOT NULL, COURSE VARCHAR(20)DEFAULT ‘DMS’);
Entity:-
• An entity may be any object, class, person or place. In the ER diagram, an entity can be
represented as rectangles.
• Consider an organization as an example- manager, product, employee, department etc.
can be taken as an entity.
Entity set:-
• An entity set is a group of similar entities and these entities can have attributes.
• In terms of DBMS, an entity is a table or attribute of a table in database, so by showing
relationship among tables and their attributes, ER diagram shows the complete logical
structure of a database.
Strong Entity:-
• The strong entity has a primary key or Unique key.
• Weak entities are dependent on strong entity. Its existence is not dependent on any other
entity.
• Strong Entity is represented by a single rectangle −
• example, Professor is a strong entity here, and the primary key is Professor_ID.
Weak Entity:-
• The weak entity in DBMS do not have a primary key and are dependent on the parent
entity. It mainly depends on other entities.
• Weak Entity is represented by double rectangle −
• Continuing our previous example, Professor is a strong entity, and the primary key is
Professor_ID. However, another entity is Professor_Dependents, which is our Weak
Entity.
Attribute:-
• The attribute is used to describe the property of an entity. Eclipse is used to represent an
attribute.
• For example, id, age, contact number, name, etc. can be attributes of a student.
Types Of Attribute:-
• Key Attribute
• The key attribute is used to represent the main characteristics of an entity. It represents a
primary key. The key attribute is represented by an ellipse with the text underlined.
•
Composite attribute:-
• An attribute that composed of many other attributes is known as a composite attribute.
The composite attribute is represented by an ellipse, and those ellipses are connected
with an ellipse.
Multivalued Attribute:-
• An attribute can have more than one value. These attributes are known as a multivalued
attribute. The double oval is used to represent multivalued attribute.
• For example, a student can have more than one phone number.
Derived Attribute:-
• An attribute that can be calculated from other attribute is known as a derived attribute. It
can be represented by a dashed ellipse.
• For example, A person's age changes over time and can be derived from another
attribute like Date of birth.
Different symbols used in ER-Model are the following:
Relationship:-
• A relationship is used to describe the relation between entities. Diamond is used to
represent the relationship.
One-to-One Relationship:-
• When only one instance of an entity is associated with the relationship, then it is
known as one to one relationship.
• For example, one Department have one Manager
One-to-many relationship:-
• When only one instance of the entity on the left, and more than one instance of an entity
on the right associates with the relationship then this is known as a one-to-many
relationship.
• For example, Scientist can invent many inventions, but the invention is done by the only
specific scientist.
Many-to-one relationship:-
• When more than one instance of the entity on the left, and only one instance of an entity
on the right associates with the relationship then it is known as a many-to-one
relationship.
• For example, Student enrolls for only one course, but a course can have many students.
Many-to-many relationship:-
• When more than one instance of the entity on the left, and more than one instance of an
entity on the right associates with the relationship then it is known as a many-to-many
relationship.
• For example, Employee can assign by many projects and project can have many
employees.
Functional dependency
A functional dependency occurs when one attribute in a relation uniquely determine another
attribute.
(OR)
A relation say R attribute X is functionally dependent on attribute Y if every value in X in
the relation has exactly one value of Y in the given relation.
• X → Y
• The left side of FD is known as a determinant, the right side of the production is
known as a dependent.
Example:
Consider table : Employee( Emp_Id, Emp_Name, Emp_Address) Here Emp_Id attribute
can uniquely identify the Emp_Name attribute of employee table because if we know the
Emp_Id, we can tell that employee name associated with it. Functional dependency can be
written as:
Emp_Id Emp_Name
We can say that Emp_Name is functionally dependent on Emp_Id.
• Transitive Dependency
• Multi-valued Dependency
Fully-Functional Dependency
• An attribute is fully functional dependent on another attribute, if it is Functionally
Dependent on that attribute and not on any of its proper subset.
Project
Emp No No Hours
1 A1 12
2 B1 4
3 C1 8
4 D1 12
Emp Project
No EmpName No Project Name Hours
1 Anil A1 ABC 12
2 Sunil B1 XYZ 4
3 Yogesh C1 PQR 8
4 Sumit D1 LMO 12
• The EmpName can be determined by EmpNo that makes the relation Partial
Dependent.
• The ProjectName can be determined by ProjectNo, which that the relation Partial
Dependent.
Multivalued Dependency
• When existence of one or more rows in a table implies one or more other rows in the
same table, then the Multi-valued dependencies occur.
• ->->
• P->->Q
Q->->R
In the above case, Multivalued Dependency exists only if Q and R are independent
attributes.
Trivial functional dependency
Dependency between non prime key attribute
Suppose there is a relation R(X,Y,Z) and X->Y, Y-> Z
Then Z is said to be transitive dependent on X
Where Y and Z are Non prime attribute
And X is Prime attribute
Normalization
• Normalization is a systematic approach of decomposing tables to eliminate data
redundancy(repetition),ensuring data integrity and avoiding Insertion, Update and
Deletion Anomalies.
• It is a multi-step process that puts data into tabular form, removing duplicated data
from the relation tables.
Goals of Normalization
• Avoid Data Redundancy
• Ensure Data Integrity
• Avoid Data Anomaly
Data Anomalies
• Insertion
• Deletion
• Updation
Example 2
Faculty Subject
Faculty Faculty Sr. No Faculty Code Subject Hours
Code Name Date of Birth 1 100 DSA 16
100 Yogesh 17/06/1964 2 100 SS 8
101 Amit 24/12/1972 3 100 IS 12
102 Omprakash 3/2/1986 4 101 MIS 16
5 101 PM 8
6 101 IS 12
7 102 PWRC 8
8 102 PCOM 8
9 102 IP 16
Third Normal Form (3NF)
A relation is in 3NF, if it is in 2NF and no non-key attribute of the relation is transitively
dependent on primary key. Third normal form is used to minimize the transitive
dependency in order to remove the anomalies that arose in Second Normal Form.
Example :-
Sr. No Faculty Code Subject Hours
1 100 DSA 16
2 100 SS 8
3 100 IS 12
4 101 MIS 16
5 101 PM 8
6 101 IS 12
7 102 PWRC 8
8 102 PCOM 8
9 102 IP 16
In the above table Hours depend on Subject and Subject depends on Faculty Code and
Sr. No. But hours are not dependent on the Faculty Code nor Sr. No. Hence there exists a
transitive dependency between Sr. No, Subject and Hours. If the Faculty Code is deleted
due to transitive dependency, information regarding the Subject and Hours allotted to it
will also lost. So we can decompose the relation as follows:-
Faculty Faculty_Subject Subject_Hours
Faculty Faculty Sr. No Faculty Code Subject Subject Hours
Code Name Date of Birth DSA 16
1 100 DSA
100 Yogesh 17/06/1964 SS 8
2 100 SS
101 Amit 24/12/1972 IS 12
3 100 IS
102 Omprakash 3/2/1986 MIS 16
4 101 MIS
5 101 PM PM 8
6 101 IS IS 12
7 102 PWRC PWRC 8
8 102 PCOM PCOM 8
9 102 IP IP 16
BCNF:
Boyce Codd Normal Form (BCNF) is considered a special condition of third Normal form.
A table is in BCNF if every determinant is a candidate key. A table can be in 3NF but not in
BCNF. This occurs when a non key attribute is a determinant of a key attribute
Example of BCNF:
Let's assume there is a company where employees work in more than one department.
EMPLOYEE(EMP_ID,EMP_COUNTRY,EMP_DEPT,DEPT_TYPE,EMP_DEPT_N
O)
In the above table Functional dependencies are as follows:
1. EMP_ID -> EMP_COUNTRY
2. EMP_DEPT {DEPT_TYPE, EMP_DEPT_NO}
Candidate key: {EMP-ID, EMP-DEPT}
The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.
To convert the given table into BCNF, we decompose it into three tables:
1.EMP_COUNTRY table: EMP_ID EMP_COUNTRY
2.EMP_DEPT table: EMP_DEPT {DEPT_TYPE, EMP_DEPT_NO}
3. EMP_DEPT_MAPPING table:EMP_ID,EMP_DEPT
Functional dependencies:
1. EMP_ID EMP_COUNTRY
2. EMP_DEPT {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:
For the first table: EMP_ID
For the second table: EMP_DEPT
For the third table: {EMP_ID, EMP_DEPT}
Now, this is in BCNF because left side part of both the functional dependencies is a key.
List of Advantages of Normalization are the following:
1. More efficient data structure.
2. Avoid redundant fields or columns.
3. More flexible data structure i.e. we should be able to add new rows and data values easily
4. Better understanding of data.
5. Ensures that distinct tables exist when necessary.
6. Easier to maintain data structure i.e. it is easy to perform operations and complex queries
can be easily handled.
7. Minimizes data duplication.
8. Close modeling of real world entities, processes and their relationships.
Benefits of denormalization
Reduce number of relations : It reduce the number of relations because it combines two
relations into one new relation.
Reduce number of foreign keys-It reduce number of foreign keys because number of
relations are reduced .
Minimizes need for joins-It minimizes need for joins because it combines many relations
into one.
Increase Performance - It increase performance of database by adding redundant data or
by grouping data.
Drawbacks of demoralization.
Slow Data Updates-It may speed up the retrieval but can slow down database updates
Increase size of relations -It can increase size of the relations due to combining multiple
relations into one single relation.
Complex implementation -It may simplify implementation in some cases but may make it
more complex in other.
Application Specific -It is always application-specific and needs to be re-evaluated if the
application changes.
Enhanced ER model :
Enhanced ER is a high-level data model that incorporates the extensions to the original ER
model. It is created to design more accurate database schemas. EER reflects data properties
and constraints more precisely. It also includes more complex requirements than traditional
application.
For example: Square, Circle, Triangle are the sub class of Shape super class.
Generalization uses bottom-up approach where two or more lower level entities combine
together to form a higher level new entity if they have common attributes in common. The
new generalized entity can further combine together with lower
level entity to create a further higher level generalized entity.
For Example, STUDENT and FACULTY can be generalized to a higher level entity called
PERSON
Q. Draw ER diagram for Banking system, to represent a customer has account
scenario. Identify entities with their attributes and draw a diagram.
Q. Consider a single table consisting following columns. Convert it into 2NF and 3NF.
Table (Supplier_no, Supplier_name, Supplier_city,. Order_no, Order_quantity,
order_amount, product_name)
(ii) IsRin3NF?:
M, P, L are prime attributes, so R(L,M,N,O,P) is in 3NF.
Q. Draw enhanced ER diagram for loan payment system. Consider the following entities:
(i) Loan (Loan_id, Loan_amount, Loan_date)
(ii) Payment (payment_id, Payment_date, Balance_amount)
(iii) Personal Loan (Personal Loan_no, Interest rate)
(iv) Home Loan (Home Loan_no, Interest rate)
Show strong entity set, weak entity set, super class and sub class.
Q. Consider following realtion student (Roll_No, name, class, total_marks, percentage,
Grade). Find appropriate dependencies and normalize upto 3NF.
Functional Dependencies:
Roll_no->name
Roll_no->class
total_marks->percentage
percentage ->Grade
1NF: Student(Roll_no, name. class, total_marks, percentage, Grade)
2NF: To convert It into 2NF, We have to decompose the given table into two tables with
fully functional dependencies and establishing a referential integrity constraint relationship
among the two tables.
Student(Roll_No, name, class )
Marks(Roll_No, total_marks, Percentage, Grade)
Q.Draw the enhanced E-R diagram for College Management System and show strong
entity set, weak entity set, super class and sub class.
Q. Find 3NF decomposition of given relation schema.
Shipping (ship, capacity, date, cargo, value).
Functional dependencies Ship -> capacity.
Ship, date -> cargo.
Cargo, Capacity -> Value
R= (ship, capacity, date, cargo, value).
Functional dependencies Ship -> capacity.
Ship, date -> cargo.
Cargo, Capacity -> Value
1) Find all attributes in R that are not involved in any functional dependency.
Here no such attribute found.
2) R= (ship, capacity, date, cargo, value) No functional dependency has all the attributes.
3) For each Functional dependency
i)Ship -> capacity R1= (ship, capacity)
ii)Ship, date -> cargo. R2= (ship, date, Cargo)
iii) Cargo, Capacity -> Value R3= (cargo, capacity, value)
Above 3 relations R1, R2 and R3 gives 3NF decomposition which is lossless and
dependency preserving