Module No - 3 DBMS
Module No - 3 DBMS
➢ Relation
➢ Attribute
➢ Tuple
➢ Domain
➢ Relational Schema
Relational Schema Terms
Relational Model Concepts in DBMS
1.Relation – In the Relational model the, relations are saved in the table format. It is stored
along with its entities. A table has two properties rows and columns. Rows represent
records/tuple and columns represent attributes.
Relational Schema Terms
Relational Model Concepts in DBMS
2.Attribute: Each column in a Table. Attributes are the properties which define a relation.
e.g., Student_Rollno, NAME,etc.
Relational Schema Terms
Relational Model Concepts in DBMS
3.Tuple – It is nothing but a single row of a table, which contains a single record.
Relational Schema Terms
Relational Model Concepts in DBMS
4.Domain– Every attribute has some pre-defined value and scope (data type) which is
known as attribute domain. It contains a set of atomic values that an attribute can take.
Relational Schema Terms
Relational Model Concepts in DBMS
5.Relation Schema: A relation schema represents the name of the relation with its attributes.
Relational Schema Terms
Relational Model Concepts in DBMS
5.Relation Schema: A relation schema represents the name of the relation with its attributes.
Relational Schema Terms
Relational Model Concepts in DBMS
4.Relation Schema: A relation schema represents the name of the relation with its attributes.
Relational Schema Terms
Relational Model Concepts in DBMS
5.Database Schema:
Relational Schema Terms
Relational Model Concepts in DBMS
6.Degree: The total number of attributes which in the relation is called the degree of the
relation.
7.Cardinality: Total number of rows present in the Table.
Relational Schema Terms
Relational Model Concepts in DBMS
Relational Schema Terms
Relational Model Concepts in DBMS
Relation instance – Relation instance is a finite set of tuples in the RDBMS system.
Relation instances never have duplicate tuples.
Relational Schema Terms
MODULE NO:-3 Relational Model and Relational Algebra :
KEYS in DBMS is an attribute or set of attributes which helps you to identify a row(tuple) in
a relation(table).
They allow you to find the relation between two tables.
Keys help you uniquely identify a row in a table by a combination of one or more columns
in that table.
Relational Model has following keys
•Primary Key
•Foreign Key
•Candidate Key
•Alternate Key
•Super Key
Concept of Keys
Super Key
•A super key is a group of single or multiple keys which uniquely identifies rows in a table.
• A Super key may have additional attributes that are not needed for unique identification.
Example of the super key
Consider the student table with columns [ID,NAME,PHONE]
•[ID]: As no two students will have the same Id, it will help to uniquely access the student
details, hence it is a super keyNow we will identify all the Super Keys present in the table that is
the set of attributes that will help to uniquely access a record
•[NAME, ID]: Even if more than one student has the same name then the combination name
will help to recognize the record, as student id will break the tie and this is combination is a
super key
Concept of Keys
Super Key
Example of the super key
Consider the student table with columns
[ID,NAME,PHONE]
•[ID]: As no two students will have the same Id, it will
help to uniquely access the student details, hence it is a
super key .
Composite Key – A key that consists of more than one attribute to uniquely
identify rows (also known as records & tuples) in a table is called composite key.
Concept of Keys
Foreign Key – Foreign keys are the columns of a table that points to the
primary key of another table. They act as a cross-reference between tables.
Employee table Department table
Primary key
Foreign key
Concept of Keys
32
MODULE NO:-3 Relational Model and Relational Algebra :
➢ Choose one of the relations, say S, and include as foreign key in S the primary key of T.
➢ If both participations are total, we may merge the two entity types and the relationship
into a single relation.
Mapping of ER and EER to Relational Model
Foreign key approach
Mapping of ER and EER to Relational Model Foreign key approach
Mapping of ER and EER to Relational Model Merge relation approach
When total participation both ways
Mapping of ER and EER to Relational Model Cross Reference approach
For each binary 1:1 relationship type R in the ER schema, identify the relations S and T that correspond to the
entity types participating in R.
Mapping of ER and EER to Relational Model
➢ Include as foreign key attributes in S the primary keys of the relations that
represent the participating entity types; their combination will form the primary
key of S.
➢ Also, include any simple attributes of the M:N relationship type as attributes of S.
Mapping of ER and EER to Relational Model
Mapping of ER and EER to Relational Model
➢ Include as foreign key attributes in the S the primary keys of the relations that
represent the participating entity types. •
➢ Also include any simple attributes of the nary relationship types as attributes of S. •
➢ The primary key for S is usually a combination of all the foreign keys that reference
the relations representing the participating entity types.
Mapping of ER and EER to Relational Model
Mapping of EER to Relational Model
➢ EER model = ER Model + hierarchical relationships
Entities that are member of one entity type (the superclass) may be grouped into
meaningful subsets (the subclasses)
Mapping of EER to Relational Model
Step8: Options for Mapping Specialization or Generalization.
Convert each specialization with m subclasses {S1, S2,….,Sm} and generalized superclass C,
where the attributes of C are {k,a1,…an} and k is the (primary) key, into relational schemas
using one of the four following options:
➢ Option 8A: Multiple relations-Superclass and subclasses
➢ Option 8B: Multiple relations-Subclass relations only
➢ Option 8C: Single relation with one type attribute
➢ Option 8D: Single relation with multiple type attributes
Mapping of EER to Relational Model
Step8: Options for Mapping Specialization or Generalization.
Option 8A: Multiple relations-Superclass and subclasses
➢ Create a relation L for C with attributes Attrs(L) = {k,a1,…an} and PK(L) = k.
➢ Create a relation Li for each subclass Si, 1 < i < m, with the attributes
Attrs(Li) = {k} U {attributes of Si} and PK(Li)=k.
➢ This option works for any specialization (total or partial, disjoint of over-
lapping).
Mapping of EER to Relational Model
Step8: Options for Mapping Specialization or Generalization.
Option 8A: Multiple relations-Superclass and subclasses
Mapping of EER to Relational Model
Step8: Options for Mapping Specialization or Generalization.
Option 8B: Multiple relations-Subclass relations only
➢ Create a relation Li for each subclass Si, 1 < i < m, with the attributes
Attr(Li) = {attributes of Si} U {k,a1…,an} and PK(Li) = k.
➢ This option only works for a specialization whose subclasses are total (every entity in the
superclass must belong to (at least) one of the subclasses).
➢ The attribute t is called a type (or discriminating) attribute that indicates the subclass
to which each tuple belongs. Subclasses are disjoint
Mapping of EER to Relational Model
Step8: Options for Mapping Specialization or Generalization.
Option
➢ 8C:a Single
Create relationLwith
single relation withone type attribute
attributes
Attrs(L) = {k,a1,…an} U {attributes of S1} U…U {attributes of Sm} U {t} and PK(L) = k.
Mapping of EER to Relational Model
Step8: Options for Mapping Specialization or Generalization.
Option 8D: Single relation with multiple type attributes
The SELECT operation is a filter that keeps only those tuples that satisfy a qualifying
condition/selection condition.
The SELECT operation can also be visualized as a horizontal partition of the relation into
two sets of tuples-those tuples that satisfy the condition and are selected, and those tuples
that do not satisfy the condition and are discarded
RELATIONAL ALGEBRA
Important Points-
Point-01:
•We may use logical operators like ∧ , ∨ , ! and relational operators like = , ≠ , > , < , <= , >=
with the selection condition.
Point-02:
•Selection operator only selects the required tuples according to the selection condition.
•It discards the unselected tuples.
•Its called Horizontal Partition.
RELATIONAL ALGEBRA
Point-03:
Selection operator is commutative in nature i.e.
σ A ∧ B (R) = σ B ∧ A (R)
OR
σ B (σ A(R)) = σ A (σ B(R))
Point-04:
Degree of the relation from a selection operation is same as degree of the input relation.
Point-05:
The number of rows returned by a selection operation is obviously less than or equal to the
number of rows in the original table.
Thus,Minimum Cardinality = 0 and Maximum Cardinality = |R|
RELATIONAL ALGEBRA
σ<selection-condition>(R)
<Attribute name><comparison operator><Constant value>
or
<Attribute name><comparison operator><Attribute name>
σ<selection-condition>or<selection-condition>(R)
• Boolean operators like:-AND ,OR,NOT
RELATIONAL ALGEBRA
Example 1:-Retrieve Person with stamp collection as his hobby from the given relation Person
RELATIONAL ALGEBRA
Example 2:-Retrieve customer who lives in USA
𝝈
Country=USA Customer
RELATIONAL ALGEBRA
Example 3:-Retrieve customer who lives in USA and has credit limit over 5000
𝝈 Customer
Country=USA and creditLimit>5000
RELATIONAL ALGEBRA
Projection Operator-
➢ The PROJECT operation, selects certain columns from the table and discards the other
columns.
➢ The result of the PROJECT operation can hence be visualized as a vertical partition of
the relation. The PROJECT operation is denoted by
RELATIONAL ALGEBRA
Important Points-
Point-01:
➢ The degree of output relation (number of columns present) is equal to the number of
attributes mentioned in the attribute list.
Point-02:
➢ Projection operator automatically removes all the duplicates while projecting the output
relation.
➢ So, cardinality of the original relation and output relation may or may not be same.
➢ If there are no duplicates in the original relation, then the cardinality will remain same
otherwise it will surely reduce.
RELATIONAL ALGEBRA
Point-03:
➢ If attribute list is a super key on relation R, then we will always get the same number of
tuples in the output relation.
➢ This is because then there will be no duplicates to filter.
Point-04:
➢ Projection operator does not obey commutative property i.e.
π <list2> (π <list1> (R)) ≠ π <list1> (π <list2> (R))
Point-05:
➢ Following expressions are equivalent because both finally projects columns of list-1
π <list1> (π <list2> (R)) = π <list1> (R)
RELATIONAL ALGEBRA
Example 4:- Retrieve persons name and hobby
RELATIONAL ALGEBRA
Example 5:- Retrieve the first name, last name, and salary of all employees who work in department number 5
RELATIONAL ALGEBRA
Example 5:- Retrieve the first name, last name, and salary of all employees who work in department number 5
RELATIONAL ALGEBRA
Example 5:- Retrieve the first name, last name, and salary of all employees who work in department number 5
RELATIONAL ALGEBRA
RENAME operation-which can rename either the relation name or the attribute names, or
both-in a manner similar to the way we defined SELECT and PROJECT.
The general RENAME operation is denoted by symbol P (rho) any of the following three
forms
𝝆S(B1,B2…….Bn) ( R )
S is the new relation name, and Bl , B2, •• ., Bn are the new attribute names. This expression
renames both the relation and its attributes
𝝆S ( R ) This renames the relation only, and
𝝆(B1,B2,B3……Bn) ( R ) This renames the attributes only. If the attributes of R are (AI' A2, ••• ,
An) in that order, then each Ai is renamed as Bj •
83
84
RELATIONAL
ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
R1 EMPLOYEE ⋈ DEPARTMENT
<EMPLOYEE . DNO=DEPARTMENT.DNO> AND< SALARY>25000>
RELATIONAL ALGEBRA INNER JOIN-EQUI
The most common use of JOIN involves join conditions with equality
comparisons only. Such a JOIN, where the only comparison operator used
is =, is called an EQUIJOIN.
R1 CUTOMER ⋈ SHIPPER
<CUSTOMER . COUNTRY=SHIPPER.COUNTRY>
RELATIONAL ALGEBRA EQUI JOIN
Retrieve employee department names where salary is greater than 25000
DEPARTMENT
EMPLOYEE
RELATIONAL ALGEBRA EQUI JOIN
Retrieve employee department names where salary is greater than 25000
RELATIONAL ALGEBRA NATURAL JOIN
➢ Notice that in the result of an EQUI-JOIN we always have one or more pairs of attributes
that have identical values in every tuple R because of the equality join condition specified
on these two attributes.
➢ Because one of each pair of attributes with identical values is superfluous, a new
operation called NATURAL JOIN-denoted by *-was created to get rid of the second
(superfluous) attribute in an EQUI]OIN condition’s.
➢ The standard definition of NATURAL JOIN requires that the two join attributes (or each
pair of join attributes) have the same name in both relations. If this is not the case, a
renaming operation is applied first.
RELATIONAL ALGEBRA NATURAL JOIN
Table_a ∗ table_b
RELATIONAL ALGEBRA NATURAL JOIN
EMPLOYEE * DEPENDENTS
RELATIONAL ALGEBRA DIVISION OPERATOR
RELATIONAL ALGEBRA DIVISION OPERATOR
RESULT T
A
RELATIONAL ALGEBRA DIVISION OPERATOR
RELATIONAL ALGEBRA DIVISION OPERATOR
RELATIONAL ALGEBRA DIVISION OPERATOR
RELATIONAL ALGEBRA DIVISION OPERATOR
RELATIONAL ALGEBRA DIVISION OPERATOR
÷
RELATIONAL ALGEBRA DIVISION OPERATOR
÷
RELATIONAL ALGEBRA JOIN
RELATIONAL ALGEBRA OUTER JOIN
➢ Theta Join, Equijoin, and Natural Join are called inner joins.
➢ An inner join includes only those tuples with matching attributes and the rest are
discarded in the resulting relation.
➢ Therefore, we need to use outer joins to include all the tuples from the participating
relations in the resulting relation.
➢ There are three kinds of outer joins − left outer join, right outer join, and full outer join.
RELATIONAL ALGEBRA OUTER JOIN
RELATIONAL ALGEBRA OUTER JOIN
RELATIONAL ALGEBRA OUTER JOIN
Left Outer Join(R S)
➢ All the tuples from the Left relation, R, are included in the resulting relation.
➢ If there are tuples in R without any matching tuple in the Right relation S, then the
S-attributes of the resulting relation are made NULL.
RELATIONAL ALGEBRA OUTER JOIN
RELATIONAL ALGEBRA OUTER JOIN
RELATIONAL ALGEBRA OUTER JOIN
Right outer join:
➢ Right outer join contains the set of tuples of all combinations in R and S that are equal on
their common attribute names.
➢ In right outer join, tuples in S have no matching tuples in R.
➢ It is denoted by ⟖.
RELATIONAL ALGEBRA OUTER JOIN
RELATIONAL ALGEBRA OUTER JOIN
RELATIONAL ALGEBRA OUTER JOIN
RELATIONAL ALGEBRA OUTER JOIN