DBMS Unit-2
DBMS Unit-2
Part-2
Dr. K ADISESHA
2
Entity-Relationship model
Introduction
E-R diagram
Relational Keys
Relation Algebra
Prof. K. Adisesha
Prof. K. Adisesha
3
Introduction
Prof. K. Adisesha
5
Introduction
Prof. K. Adisesha
8
Introduction
Prof. K. Adisesha
9
Introduction
Relational Model:
Relational Model represents how data is stored in Relational Databases. A relational
database stores data in the form of relations (tables).
➢ Relational Model was proposed by E.F. Codd to model data in the form of relations or
tables.
➢ After designing the conceptual model of Database using ER diagram, we need to
convert the conceptual model in the relational model which can be implemented using
any RDBMS languages like Oracle SQL, MySQL etc,.
➢ Consider a relation STUDENT with attributes ROLL_NO, NAME, ADDRESS, PHONE
and AGE represented as table.
ROLL_NO NAME ADDRESS PHONE AGE
ER model:
ER model stands for an Entity-Relationship model. It is a high-level data model.
➢ This model is used to define the data elements and relationship for a specified system.
➢ It develops a conceptual design for the database. It also develops a very simple and
easy to design view of data.
➢ In ER modeling, the database structure is portrayed as a diagram called an entity-
relationship diagram.
➢ Example, Suppose we design a school database. In
this database, the student will be an entity with
attributes like address, name, id, age, etc.
Prof. K. Adisesha
11
E-R diagram
Prof. K. Adisesha
12
E-R diagram
Prof. K. Adisesha
14
E-R diagram
Weak Entity: An entity that depends on another entity called a weak entity. The weak
entity doesn't contain any key attribute of its own.
➢ The weak entity is represented by a double rectangle.
Prof. K. Adisesha
15
E-R diagram
Prof. K. Adisesha
17
E-R diagram
Derived Attribute: An attribute that can be derived 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.
Prof. K. Adisesha
18
E-R diagram
Relationship:
A Relationship describes relations between entities. Relationship is represented using
diamonds shaped box.
➢ There are three types of relationship that exist between entities:
❖ Binary Relationship
❖ Recursive Relationship
❖ Ternary Relationship
Prof. K. Adisesha
19
E-R diagram
Binary Relationship:
It means relation between two entities.
➢ This is further divided into three types.
❖ One to One
❖ One to Many
❖ Many to Many
➢ One to One:
✓ This type of relationship is rarely seen in real world.
✓ The above example describes that one student can enroll only for one course and a
course will have only one Student. This is not what you will usually see in relationship.
Prof. K. Adisesha
20
E-R diagram
Binary Relationship:
➢ One to Many:
✓ It reflects business rule that one entity is associated with many number of same
entity.
✓ For example, Student enrolls for only one Course but a Course can have many
Students.
➢ Many to Many:
✓ It reflects business rule that many entity are associated with many number of same
entity.
✓ The above diagram represents that many students can enroll for more than one
course.
Prof. K. Adisesha
21
E-R diagram
Prof. K. Adisesha
22
E-R diagram
Recursive relationship:
A relationship between two entities of a similar entity type is called a recursive
relationship.
➢ Here the same entity type participates more than once in a relationship type with a
different role for each instance.
➢ The person who is a supervisor for many other
employees also come under employees category
➢ Student can be a class monitor and handle other
students but a person who is working as a class leader
is itself a student of the class and hence a class
monitor has a recursive relationship of entity student
Prof. K. Adisesha
23
E-R diagram
Ternary relationship:
When there are exactly three entity sets participating in a relationship then such type of
relationship is called ternary relationship.
➢ n the real world, a patient goes to a doctor and doctor prescribes the medicine to the
patient, three entities Doctor, patient and medicine are involved in the relationship
“prescribes”.
Prof. K. Adisesha
24
Relational Keys
Prof. K. Adisesha
25
Relational Keys
Prof. K. Adisesha
28
Relational Keys
Prof. K. Adisesha
29
Relational Keys
Prof. K. Adisesha
30
Relational Keys
Relational Keys
Prof. K. Adisesha
33
Integrity Constraints
Integrity Constraints:
Integrity constraints are pre-defined set of rules that are applied on the table
fields(columns) or relations to ensure that the overall validity, integrity, and consistency
of the data present in the database table is maintained.
➢ It is used to maintain the quality of information.
➢ It ensures that the data insertion, updating, and other processes have to be performed in
such a way that data integrity is not affected.
➢ Types of integrity constraints in relational data model:
❖ Domain Constraint
❖ Entity Constraint
❖ Referential Integrity Constraint
❖ Key Constraint
Prof. K. Adisesha
34
Integrity Constraints
Prof. K. Adisesha
36
Integrity Constraints
Prof. K. Adisesha
37
Integrity Constraints
Prof. K. Adisesha
38
Integrity Constraints
Prof. K. Adisesha
39
Generalization
Generalization:
Generalization is a bottom-up approach in which two lower level entities combine to
form a higher level entity.
➢ In generalization, a number of entities are brought together into one generalized entity
based on their similar characteristics.
➢ For example, Student and Parent details can all be generalized as a group ‘Person’ as
Personal details.
Prof. K. Adisesha
40
Specialization
Specialization:
Specialization is a Top-down approach in which one higher level entity can be broken
down into two lower level entities.
➢ Specialization is the opposite of generalization.
➢ In specialization, a group of entities is divided into sub-groups based on their
characteristics.
➢ Consider an entity employee which can be
further classified as sub-entities Technician,
Engineer & Accountant because these sub
entities have some distinguish attributes.
Prof. K. Adisesha
41
Aggregation
Aggregation:
In aggregation, the relation between two entities is treated as a single entity. In
aggregation, relationship with its corresponding entities is aggregated into a higher level
entity.
➢ Center entity offers the Course entity act as a single entity in the relationship which is in
a relationship with another entity visitor. If a visitor visits a coaching center then he will
enquiry about both Course and Center.
Prof. K. Adisesha
42
Aggregation
Aggregate function:
Aggregate functions in DBMS take multiple rows from the table and return a value
according to the query.
➢ Common aggregate functions include:
❖ Average (i.e., arithmetic mean)
❖ Count
❖ Maximum
❖ Median
❖ Minimum
❖ Mode
❖ Range
❖ Sum
Prof. K. Adisesha
43
ER diagram
ER diagram Design:
The database can be represented using the notations, and these notations can be reduced
to a collection of tables.
Prof. K. Adisesha
44
ER diagram
Case Study:
Sunny Travels: It is in Travel business since 2000 with several buses connecting different
places in India. Its main office is located in Bangalore. The company wants to computerize
its operations in the following areas:
❖ Reservations and Ticketing
❖ Cancellations
➢ Reservations &Cancellation: Reservations are directly handled by booking office.
Reservations can be made 30 days in advance and tickets issued to passenger. One
Passenger/person can book many tickets (to his/her family).
➢ Cancellations are also directly handed at the booking office. In the process of computerization of
Roadway Travels you have to design and develop a Database which consists the data of Buses,
Passengers, Tickets, and Reservation and cancellation details. You should also develop query's
Prof. K. Adisesha
using SQL to retrieve the data from the database.
45
ER diagram
Case Study:
AIM: Analyze the problem and come with the entities in it. Identify what Data has to be
persisted in the databases.
The Following are the entities:
1.Bus
2. Reservation
3. Ticket
4. Passenger
5. Cancellation
Prof. K. Adisesha
46
ER diagram
Prof. K. Adisesha
47
ER diagram
Prof. K. Adisesha
48
ER diagram
Relation Algebra:
Relational algebra is a procedural query language. It gives a step by step process to
obtain the result of the query. It uses operators to perform queries.
➢ Types of Relational operation:
❖ Select Operation
❖ Project Operation
❖ Union Operation
❖ Set Different Operation
❖ Cartesian Product Operation
❖ Rename Operation
Prof. K. Adisesha
51
Relation Algebra
Relation Algebra:
Relational algebra is a procedural query language that consists of a set of operations
that take one or more relations as input and result into a new relation as an output.
➢ The relational algebraic operations can be divided into:
❖ Basic set-oriented operations:
✓ Union, Set different, Cartesian product
❖ Relational-oriented operations:
✓ Selection, Projection, Division, Joins
Prof. K. Adisesha
52
Relation Algebra
Relation Algebra:
Select Operation: The select operation selects tuples that satisfy a given predicate.
➢ It is denoted by sigma (σ).
❖ Notation: σ p(r)
➢ Where:
❖ σ is used for selection prediction
❖ r is used for relation
❖ p is used as a propositional logic formula which may use connectors like: AND OR and
NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
➢ Example:
❖ σsubject = “Books” and cost = “150” or year > “2021”(DBMS)
➢ The output: Selecting tuples from the Text_Books wherever the subject happens to be ‘Books’ and the
Prof. K. Adisesha
‘Cost’ is 150 or those DBMS have been published after 2021.
53
Relation Algebra
Relation Algebra:
Project Operation (or ∏): This operation shows the list of those attributes that we wish to
appear in the result. Rest of the attributes are eliminated from the table.
➢ It is denoted by ∏.
❖ Notation: ∏ A1, A2,.., An (r)
➢ Where:
❖ A1, A2, A3 is used as an attribute name of relation r.
➢ Example:
❖ ∏subject, writer (Text_books)
➢ The output: Selecting and projecting columns named as writer as well as the subject from the relation
Text_Books.
Prof. K. Adisesha
54
Relation Algebra
Relation Algebra:
Union Operation (∪ ): Suppose there are two tuples R and S. The union operation
contains all the tuples that are either in R or S or both in R & S.
➢ It eliminates the duplicate tuples. It is denoted by ∪.
❖ Notation: R ∪ S R CUSTOMER_NAME ACCOUNT_NO
➢ Where: S CUSTOMER_NAME LOAN_NO
Relation Algebra:
Intersection Operation (∩): Suppose there are two tuples R and S. The set intersection
operation contains all tuples that are in both R & S.
➢ It is denoted by intersection ∩. CUSTOMER_NAME ACCOUNT_NO
❖ Notation: R ∩ S
➢ Where: CUSTOMER_NAME LOAN_NO
Relation Algebra:
Cartesian product (X): The Cartesian product is used to combine each row in one table
with each row in the other table. It is also known as a cross product.
➢ It is denoted by X.
❖ Notation: R X S
➢ Where:
❖ R and S must have the attribute of the same number.
❖ All tuples that are in both R & S selected automatically.
➢ Example: Using the above DEPOSITOR table and BORROW table.
❖ EMPLOYEE X DEPARTMENT
➢ The output: Selecting and projecting all columns named from the relations present in EMPLOYEE &
DEPARTMENT EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME
Prof. K. Adisesha
57
Relation Algebra
Joins (⋈):
Joins in Database Management System are used in relational algebra and SQL to
join/combine more than one table to get some specific results out of those tables.
➢ A Join operation combines related tuples from different relations, if and only if a given
join condition is satisfied. It is denoted by ⋈.
➢ Different types of the JOINs in SQL:
❖ (INNER) JOIN
❖ LEFT (OUTER) JOIN
❖ RIGHT (OUTER) JOIN
❖ FULL (OUTER) JOIN
Prof. K. Adisesha
58
Relation Algebra
Joins:
Joins in Database Management System are used in relational algebra and SQL to
join/combine more than one table to get some specific results out of those tables.
➢ Different types of the JOINs in SQL:
❖ (INNER) JOIN: Returns records that have matching values in both tables
❖ LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from
the right table
❖ RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records
from the left table
❖ FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
Prof. K. Adisesha
59
Normalization
Normalization:
A large database defined as a single relation may result in data duplication. This
repetition of data may result in:
❖ Making relations very large.
❖ It isn't easy to maintain and update data as it would involve searching many records
in relation.
❖ Wastage and poor utilization of disk space and resources.
❖ The likelihood of errors and inconsistencies increases.
➢ So to handle these problems, we should analyze and decompose the relations with
redundant data into smaller, simpler, and well-structured relations that are satisfy
desirable properties.
Prof. K. Adisesha
60
Normalization
Normalization:
Normalization is a process of decomposing the relations into relations with fewer
attributes.
➢ Normalization is the process of organizing the data in the database.
➢ Normalization is used to minimize the redundancy from a relation or set of relations. It
is also used to eliminate undesirable characteristics like Insertion, Update, and Deletion
Anomalies.
➢ Normalization divides the larger table into smaller and links them using relationships.
➢ The normal form is used to reduce redundancy from the database table.
Prof. K. Adisesha
61
Normalization
Normalization:
The main reason for normalizing the relations is removing these anomalies.
Normalization consists of a series of guidelines that helps to guide you in creating a
good database structure.
➢ Data modification anomalies can be categorized into three types:
❖ Insertion Anomaly: Insertion Anomaly refers to when one cannot insert a new tuple
into a relationship due to lack of data.
❖ Deletion Anomaly: The delete anomaly refers to the situation where the deletion of
data results in the unintended loss of some other important data.
❖ Updatation Anomaly: The update anomaly is when an update of a single data value
requires multiple rows of data to be updated.
Prof. K. Adisesha
62
Normalization
Normalization:
Normalization is a step by step process of removing the different kinds of redundancy
and anomaly one step at a time from the database.
➢ E.F Codd developed for the relation data model in 1970.
➢ Normalization rules are divided into following normal form:
Prof. K. Adisesha
63
Normalization
Normalization:
Normalization is a step by step process of removing the different kinds of redundancy
and anomaly one step at a time from the database.
Prof. K. Adisesha
64
Normalization
Prof. K. Adisesha
65
Normalization
Prof. K. Adisesha
66
Normalization
Prof. K. Adisesha
67
Normalization
Prof. K. Adisesha
68
Normalization
Prof. K. Adisesha
70
Normalization
Prof. K. Adisesha
72
Normalization
Prof. K. Adisesha
74
Questions
Important Questions:
➢ Define the following database terms:
a. Data Model b. Tuple c. Domain d. Primary key e. Foreign key
➢ Write the difference between manual and electronic data processing.
➢ Explain any five applications of database.
➢ Briefly explain the data processing cycle.
➢ Write the difference between Hierarchical data model and network data model.
➢ What is normalization? Explain second normal form with an example.
➢ What is database model? Explain Hierarchical model.
➢ Explain 3-level DBMS architecture.
Prof. K. Adisesha
➢ .
75
Discussion
Queries ?
Prof. K. Adisesha
9449081542