DBMS Module 2
DBMS Module 2
Model
Module II - DBMS
Knowledge is not free…
You have to pay
attention!
● Conceptual Data Models for Database Design
● Entity Relationship Models
● Concept of Entity, Entity Sets, Relationship Sets, Attributes,
Domains, Constraints, Keys, Strong and Weak Entities
● Concepts of EER
● Relational Data Model - Relations, Domains and Attributes,
Tuples, Keys.
● Integrity Rules
● Relational Algebra and Operations
● Relational Calculus and Domain Calculus
● Relational Database Design using ER to Relational Mapping
Conceptual Data Models for Database Design
★ A Data Model in Database Management System (DBMS) is the concept of
tools that are developed to summarize the description of the database.
★ Data Models provide us with a transparent picture of data which helps us in
creating an actual database.
★ It shows us from the design of the data to its proper implementation of
data.
★ It is basically classified into 3 types:-
Conceptual Data Models for Database Design
★ The conceptual data model describes the database at a very high level and
is useful to understand the needs or requirements of the database.
★ It is this model, that is used in the requirement-gathering process i.e. before
the Database Designers start making a particular database.
○ Entity-Relationship Model( ER Model): It is a high-level data model
which is used to define the data and the relationships between them. It
is basically a conceptual design of any database which is easy to design
the view of data.
■ Design is independent of all physical considerations
■ Questions that are addressed during conceptual design:
– What are the entities and relationships of interest (miniworld)?
– What information about entities and relationships among entities
needs to be stored in the database?
– What are the constraints (or business rules) that (must) hold for
the entities and relationships?
Entity Relationship Model
★ The Entity Relationship Model is a model for identifying entities to be
represented in the database and representation of how those entities are
related.
★ The ER data model specifies enterprise schema that represents the overall
logical structure of a database graphically.- ER Diagram
★ The Entity Relationship Diagram explains the relationship among the
entities present in the database.
★ ER models are used to model real-world objects like a person, a car, or a
company and the relation between these real-world objects.
★ The ER Diagram is the structural format of the database.
★ ER Model consists of Entities, Attributes, and Relationships among Entities
in a Database System.
Entity Relationship Model
Entity, Entity Type and Entity Sets
★ Entity: real-world object or thing with an independent existence and which is
distinguishable from other objects.
○ Examples are a person, car, customer, product, gene, book etc.
○ An Entity may be an object with a physical existence – a particular
person, car, house, or employee – or it may be an object with a conceptual
existence – a company, a job, or a university course.
★ Entity Type: Collection of entities that all have the same attributes.
○ e.g., persons, cars, customers etc.
★ Entity Set: Collection of entities of a particular entity type
at any point in time; entity set is typically referred to using
the same name as entity type.
○ For Example, E1 is an entity having Entity Type Student
and the set of all students is called Entity Set.
○ In ER diagram, Entity Type is represented as:
Types of Entity
There are two types of entity:
1. Strong Entity : A Strong Entity is a type of entity that has a key Attribute.
Strong Entity does not depend on other Entity in the Schema. It has a primary
key, that helps in identifying it uniquely, and it is represented by a rectangle.
These are called Strong Entity Types.
2. Weak Entity : An Entity type has a key attribute that uniquely identifies each
entity in the entity set. But some entity type exists for which key attributes can’t
be defined. These are called Weak Entity types .
Every student in the Student Entity set participates in a relationship but there
exists a course C4 that is not taking part in the relationship.
KEYS
● Keys are used to uniquely identify any record or row of data from the table.
● It is also used to establish and identify relationships between tables.
ID is used as a key in the
STUDENT table because it is
unique for each student.
1. Primary key: It is the first key used to identify one and only
one instance of an entity uniquely. An entity can contain
multiple keys.
The key which is most suitable from those lists becomes a
primary key.
In the EMPLOYEE table, ID can be the primary key since it
is unique for each employee. In the EMPLOYEE table, we
can even select License_Number and Passport_Number
as primary keys since they are also unique.
Types of KEYS
2. Candidate key: A candidate key is an attribute or set of attributes that can
uniquely identify a tuple. Except for the primary key, the remaining attributes
are considered a candidate key. The candidate keys are as strong as the
primary key.
For example: In the EMPLOYEE table, id is best suited for the primary key.
The rest of the attributes, like SSN, Passport_Number, License_Number, etc., are
considered a candidate key.
Types of KEYS
3. Super Key: Super key is an attribute set that can uniquely identify a tuple. A
super key is a superset of a candidate key.
In the EMPLOYEE table, for(EMPLOEE_ID, EMPLOYEE_NAME), the name of
two employees can be the same, but their EMPLYEE_ID can't be the same.
Hence, this combination can also be a key.
The super key would be Employee_ID,
(EMPLOYEE_ID, EMPLOYEE-NAME),
etc.
Types of KEYS
4. Foreign key: Foreign keys are the column of the table used to point to the
primary key of another table.
Every employee works in a specific department in a company, and employee
and department are two different entities. So we can't store the department's
information in the employee table. That's why we link these two tables
through the primary key of one table.
DEPARTMENT
Null represents a record where data may be missing data or data for that
record may be optional.
Once not null is applied to a particular column, you cannot enter null values to
that column and restricted to maintain only some proper value other than null.
2. Unique
A primary key is a constraint in a table that uniquely identifies each row record
in a database table by enabling one or more the columns in the table as the
primary key.
Example:
UNION
Let's suppose we have a vehicle superclass, and we have two subclasses, car and
bike. These two subclasses will inherit the attributes from the vehicle superclass.
Now we have a UNION of those vehicles which are RTO registered, so we have a
UNION of cars and bikes, but they will inherit all the attributes from the vehicle
superclass.
EER - Enhanced ER Model
Relational Data Model
After designing the conceptual model of the Database using ER diagram, we need
to convert the conceptual model into a relational model which can be
implemented using any RDBMS language like Oracle SQL, MySQL, etc.
The relational model uses a collection of tables to represent both data and the
relationships among those data. Each table has multiple columns, and each
column has a unique name. Tables are also known as relations.
The relational model is an example of a record-based model.
Record-based models are so named because the database is structured in
fixed-format records of several types.
Each table contains records of a particular type. Each record type defines a fixed
number of fields, or attributes.
The columns of the table correspond to the attributes of the record type.
The relational model represents how data is stored in Relational Databases. A
relational database consists of a collection of tables, each of which is assigned a
unique name.
Relational Data Model
Attribute: The properties that
define an entity. e.g.; ROLL_NO,
NAME, ADDRESS
Column: The column represents the set of values for a particular attribute. The
column ROLL_NO is extracted from the relation STUDENT.
NULL Values: The value which is not known or unavailable is called a NULL value.
It is represented by blank space. e.g.; PHONE of STUDENT having ROLL_NO 4 is
NULL.
Relational Data Model
TUPLES
A tuple is a single row in a database that contains a single record for such a
relation. A row or record represent a set of connected data.
● The candidate key can be simple (having only one attribute) or composite as well.
● A unique constraint that has a nullable column, allows the value ‘ null ‘ in that
column only once . That’s why the STUD_PHONE attribute is a candidate here, but
can not be a ‘null’ value in the primary key attribute.
Relational Data Model
KEYS - Primary Key
There can be more than one candidate key in relation out of which one can be
chosen as the primary key.
For Example, STUD_NO, as well as STUD_PHONE, are candidate keys for relation
STUDENT but STUD_NO can be chosen as the primary key.
(only one out of many candidate keys)
A primary key is a unique key, meaning it can uniquely identify each record (tuple)
in a table.
It must have unique values and cannot contain any duplicate values.
A primary key cannot be NULL, as it needs to provide a valid, unique identifier for
every record.
A primary key does not have to consist of a single column. In some cases, a
composite primary key (made of multiple columns) can be used to uniquely
identify records in a table.
Databases typically store rows ordered in memory according to primary key for
fast access of records using primary key.
Relational Data Model
KEYS - Primary Key
Example:
Table STUDENT
An alternate key is any candidate key in a table that is not chosen as the primary
key. In other words, all the keys that are not selected as the primary key are
considered alternate keys.
An alternate key is also referred to as a secondary key because it can uniquely
identify records in a table, just like the primary key.
An alternate key can consist of one or more columns (fields) that can uniquely
identify a record, but it is not the primary key
Eg:- SNAME, and ADDRESS is Alternate keys
Example: STUD_NO, as well as PHONE both,
are candidate keys for relation STUDENT
but PHONE will be an alternate key (only one
out of many candidate keys).
Relational Data Model
KEYS - Foreign Key
A foreign key is an attribute in one table that refers to the primary key in another
table. The table that contains the foreign key is called the referencing table, and
the table that is referenced is called the referenced table.
A foreign key in one table points to the primary key in another table,
establishing a relationship between them. It helps connect two or more tables,
enabling to create relationships
between them.
Example: R Output Table : For the above relation, σ(c>3)R will select the
A B C tuples which have c more than 3.
1 2 4 A B C
2 2 3 1 2 4
3 2 3 4 3 4
4 3 4
Relational Algebra and Operations
2. Projection(π) : While Selection operation works on rows , similarly projection
operation of relational algebra works on columns.
It basically allows you to pick specific columns from a given relational table based
on the given condition and ignoring all the other remaining columns.
If we are interested in the specific columns from the relational tables, we would
prefer to use the Projection Operator.
It is used to project required column data from a relation.
Example: Suppose we want columns B and C from Relation R.
π(B,C)R will show following columns.
B C
2 4
2 3
3 4 [By Default, projection removes duplicate data.]
Relational Algebra and Operations
3. Union(U) : Used to combine the results of two queries into a single result. The
only condition is that both queries must return same number of columns with same
data types.
If in case we want a list of all the employee from two different department . Then in
that case we should use union operation to merge both the list from two different
department. Union operation in relational algebra is the same as union operation in
set theory.
Example: FRENCH GERMAN
Student_Name Roll_Number Student_Name Roll_Number
Ram 01 Vivek 13
Mohan 02 Geeta 17
Vivek 13 Shyam 21
Geeta 17 Rohan 25
Relational Algebra and Operations
Consider the following table of Students having different optional subjects in their
course.
π(Student_Name)FRENCH U π(Student_Name)GERMAN
Student_Name
Ram
Mohan
Vivek
Geeta
Shyam
Rohan
The only constraint in the union of two relations is that both relations must have the
same set of Attributes.
Relational Algebra and Operations
Consider the following table of Students having different optional subjects in their
course.
π(Student_Name)FRENCH U π(Student_Name)GERMAN
Student_Name
Ram
Mohan
Vivek
Geeta
Shyam
Rohan
The only constraint in the union of two relations is that both relations must have the
same set of Attributes.
Relational Algebra and Operations
4. Set Difference(-) : Set difference basically provides the rows that are present in
one table , but not in another tables.
Example: From the above table of FRENCH and GERMAN, Set Difference is used
as follows: π(Student_Name)FRENCH - π(Student_Name)GERMAN
Student_Name
Ram
Mohan
The only constraint in the Set Difference between two relations is that both
relations must have the same set of Attributes.
π(Student_Name) GERMAN- π(Student_Name)FRENCH
Student_Name
Shyam
Rohan
Relational Algebra and Operations
5. Set Intersection(∩) : Set Intersection basically allows to fetches only those rows
of data that are common between two sets of relational tables.
Example: From the above table of FRENCH and GERMAN, the Set Intersection is
used as follows: π(Student_Name)FRENCH ∩ π(Student_Name)GERMAN
Student_Name
Vivek
Geeta The only constraint in the Set Difference between two relations
is that both relations must have the same set of Attributes.
6. Rename(ρ) : allows you to give a temporary name to a specific relational table
or to its columns. It is very useful when we want to avoid ambiguity, especially in
complex Queries. Rename is a unary operation used for renaming attributes of a
relation. ρ(a/b)R will rename the attribute 'b' of the relation by 'a'.
Relational Algebra and Operations
7. Cross Product(X) : Cartesian product Operator combines every row of one
table with every row of another table , producing all the possible combination. It’s
mostly used as a precursor to more complex operation like joins.
Cross-product between two relations. Let’s say A and B, so the cross product
between A X B will result in all the attributes of A followed by each attribute of B.
Each record of A will pair with every record of B.
Example: A B
Name Age Sex ID Course
Ram 14 M 1 DS
Sona 15 F 2 DBMS
Kim 20 M
Relational Algebra and Operations
AXB
Name Age Sex ID Course
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Sona 15 F 2 DBMS
Kim 20 M 1 DS
Kim 20 M 2 DBMS
If A has ‘n’ tuples and B has ‘m’ tuples then A X B will have ‘ n*m ‘ tuples.
Relational Algebra and Operations
Derived Operators: These are some of the derived operators, which are derived
from the fundamental operators.
1. Natural Join(⋈): Natural join is a binary operator. Natural join between two or
more relations will result in a set of all combinations of tuples where they have an
equal common attribute. Example:
EMP DEPT
Name ID Dept_Name Dept_Name Manager
A 120 IT Sales Y
B 125 HR Production Z
C 110 Sales IT A
D 111 IT
Natural join between EMP and DEPT with condition :
EMP.Dept_Name = DEPT.Dept_Name
Relational Algebra and Operations
EMP ⋈ DEPT
Name ID Dept_Name Manager
A 120 IT A
C 110 Sales Y
D 111 IT A
2. Conditional Join: Conditional join works similarly to natural join. In natural join,
by default condition is equal between common attributes while in conditional join
we can specify any condition such as greater than, less than, or not equal.
Example: R S
ID Sex Marks ID Sex Marks
1 F 45 10 M 20
2 F 55 11 M 22
3 F 60 12 M 59
Relational Algebra and Operations
Join between R and S with condition R.marks >= S.marks
For example: {< article, page, subject > | ∈ magazine ∧ subject = 'database'}
Output: This query will yield the article, page, and subject from the relational
magazine, where the subject is a database.
Relational Database Design using ER to Relational Mapping
ER-Diagram for a
Company Database
Relational Database Design using ER to Relational Mapping
Now, let’s dive in to convert this ER-Diagram to Relational Schema…
Step 1: Figure out all the regular/strong entity from the diagram and then
create a corresponding relation that includes all the simple attributes.
● Choose one of the attributes as a primary key. If composite, the
simple attributes together form the primary key.
● For the given ER-Diagram we have Employee, Department and
Project as strong/regular
entity, as they are enclosed
in single rectangle.
● So, we create respective
relations that is depicted in
the figure.
Relational Database Design using ER to Relational Mapping
Step 2: Figure out the weak entity types from the diagram and create
a corresponding relation that includes all its simple attributes.
● Add as foreign key all of the primary key attributes in the entity
corresponding to the owner entity.
● The primary key is a combination of all the primary key attributes
from the owner and the
primary key of the weak
entity.
● For the ER-Diagram we
have Dependent as a
weak entity, as it is
enclosed in a double
rectangle that is indicative
of an entity being weak.
Relational Database Design using ER to Relational Mapping
Step 3: Now we need to figure out the entities from ER diagram for
which there exists a 1-to-1 relationship.
● The entities for which there exists a 1-to-1 relationship, choose one
relation as S, the other as T.
● Better if S has total participation.
● Then we need to add to S all the simple attributes of the relationship
if there exists any.
● After that, we add as a foreign key in S the primary key attributes of
T.
● For the given ER-Diagram there exists a 1-to-1 relationship between
Employee and Department entity.
● Here Department has total participation therefore consider it as
relation S and Employee as relation T.
Relational Database Design using ER to Relational Mapping
Step 3:
The 1-to-1 mapping between Employee and Department is depicted
in the figure below.
Relational Database Design using ER to Relational Mapping
Step 4:
Now we need to figure out the entities from ER diagram for which
there exists a 1-to-N relationship.
● The entities for which there exists a 1-to-N relationship, choose a
relation as S as the type at N-side of relationship and other as T.
● Then we add as a foreign key to S all of the primary key attributes of
T.
● In the given ER diagram there are two 1-to-N relationships that
exists between Employee-Department and Employee-Dependent
entity.
● The 1-to-N mapping between Employee-Department and
Employee-Dependent is depicted in the figure below.
Relational Database Design using ER to Relational Mapping
Step 4:
Relational Database Design using ER to Relational Mapping
Step 5:
Now we need to figure out the entities from ER diagram for which
there exists an M-to-N relationship.
● Create a new relation S.
● The primary keys of relations between which M-to-N relationship
exists, are added to the new relation S created, that acts as a
foreign key.
● Then we,add any simple attributes of the M-to-N relationship to S.
● For the given ER-Diagram there exists M-to-N relationship between
Employee and Project entity.
● The new table Works_On is created for mapping the relationship
between Employee and Project relation.
Relational Database Design using ER to Relational Mapping
Step 5:
Relational Database Design using ER to Relational Mapping
Step 6:
Now identify the relations that contain multi-valued attributes.
● Then we need to create a new relation S
● In the new relation S we add as foreign keys the primary keys of the
corresponding relation.
● Then we add the multi-valued attribute to S; the combination of all
attributes in S forms the primary key.
● For the given ER-Diagram there exists a multi-valued attribute
(Locations) in Department relation.
● So, we create a new relation called Dept_Locations. To this new
relation we add the primary key of Department Table that is
D_Number and the multi-valued attribute Locations.
Relational Database Design using ER to Relational Mapping
Step 6: