Unit 2
Unit 2
Relational Model:
Structure of relational databases, Domains, Relations,
Relational algebra – fundamental operators and syntax, relational
algebra queries, tuple relational calculus
………………………………………………………………
What are domains and relations in DBMS?
Domain: It contains a set of atomic values that an attribute can take.
Relational instance: In the relational database system, the relational instance
is represented by a finite set of tuples. Relation instances do not have
duplicate tuples.
1
What is the difference between relation and domain?
An ordered pair (x,y) is called a relation in x and y. The first element in an
ordered pair is called the domain, and the set of second elements is called
the range of the relation.
What are relations in DBMS?
Similarly, a relationship in DBMS is the way in which two or more data sets
are linked, i.e., any association between two entity types is called a
relationship. So entity takes part in the relationship, and it is represented by
a diamond shape.
The relation is defined as the subset of the cartesian product A×B which
satisfies any specific condition. It links the values of set A to the specific
values of set B.
2
Q5: How to determine if a Relation is a Function?
If for each input value, we get a specific and unique output value in a relation
then this relation is called the function. Every function is a relation but not
every relation is a function.
--------------------------------------------------------------------------------------------------------------
What is a Relation?
For any two non-empty sets A and B, we define the relation R as the subset of
the Cartesian product of A × B where each member of set A is related to the
member of set B through some unique rule.
Types of Relations
There are the following types of relations between two sets:
Universal relation
Empty relation
Identity relation
Symmetric relation
Inverse relation
Reflexive relation
3
Transitive relation
Universal Relations
A relation is called a universal connection if each element of set A is related to
another element of set A i.e. R = A × A.
Empty Relations
The relation wherein there is no connection between any components of a set is
called an empty relation. An empty relation is also called void relation.
Consider if set A = {2, 3} then, empty relation is R = {m,n} where, |m-n| = 8.
Identity Relations
The relation where each component of a set is identified with itself is called
Identity Relations.
Consider a set A = {1,2,3}, the identity relation will be I = {1,1}, {2,2}, {3,3}
I = {(m, m), m ∈ A}
Symmetric Relations
A relation is symmetric if a = b holds true then b = a also holds true. A
connection R is symmetric just if (b, a) ∈ R is then (a,b) ∈ R.
R = {(1, 2), (2, 1)} is symmetric relation set for a set A = {1, 2}.
Inverse Relations
Inverse relation occurs when a set has inverse pairs of another set. i.e. if R ∈ A
× B then the inverse relation is R-1 = {(b,a) such that (a,b) ∈ R}
Consider if set A = {(1,2),(3,4)}, then inverse relation will be R-1 = {(2,1), (4,3)}.
Reflexive Relations
A relation where each component of a set A is mapped to itself is called
reflexive relation, i.e. for every x ∈ A, (a, a) ∈ R
Example: For set A = {-1, 4}. Reflexive relation is R = {(-1, -1), (4, 4), (-1, 4), (4,
-1)}
Transitive Relations
If for any relation (m, n) ∈ R and (n, p) ∈ R, then if (m, p) ∈ R is
Consider aRb and bRc ⇒ aRc ∀ a, b, c ∈ A
4
Here the set of values of A is called the domain and the set of values of b is
called the range.
Domain of a Relation
Domain of any Relation is the set of input values of the relation. For example, if
we take two sets A and B, and define a relation R: {(a, b): a ∈ A, b ∈ B} then the
set of values of A is called the domain of the function.
The image given below represents the domain of a relation.
Range of a Relation
Range of any Relation is the set of output values of the relation. For example, if
we take two sets A and B, and define a relation R: {(a, b): a ∈ A, b ∈ B} then the
set of values of B is called the domain of the function.
The image given below represents the range of a relation.
5
Codomain of Relation
We define the codomain of the relation R as the set B of the cartesian product
A×B on which the relation is defined. Now it is clear that the range of the
function is the proper subset of Codomain.
Range ⊆ Codomain
Solution:
Relation R is defined as,
R = {(1, 1), (2, 4), (3, 9)}
Domain of R = {1,2,3}
Range of R = {1,4,9}
Codomain of R = Set A = = {1,2,3,4,5,6,7,8,9}
6
Example 2: Find the domain and range of the relation R: {(a, a+3) | a ∈ A,
a2 ∈ A} which is defined on A×A and the set A = {1, 2, 3, 4, 5, 6, 7, 8, 9}.
Solution:
Relation R is defined as,
R = {(1,4), (2,5), (3,6), (4,7), (5,8), (6,9)}
Domain of R = {1, 2, 3, 4, 5}
Range of R = {4, 5, 6, 7, 8, 9}
Codomain of R = Set A = = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Example 3: Find the domain and range of the relation R: {(a, b) | a+b = 7 a
∈ A, b ∈ B} which is defined on A×B and the set A = {1, 2, 3, 4} B = {5, 6, 7,
8, 9}.
Solution:
Relation R is defined as,
R = {(1,6), (2,5)}
Domain of R = {1, 2}
Range of R = {6, 5}
Codomain of R = Set B = {5, 6, 7, 8, 9}
Attribute: It contains the name of a column in a particular table. Each attribute Ai must
have a domain, dom(Ai)
Relational schema: A relational schema contains the name of the relation and name of
all columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It can identify
the row in the relation uniquely.
7
Example: STUDENT Relation
o In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are the attributes.
o The instance of schema STUDENT has 5 tuples.
o t3 = <Laxman, 33289, 8583287182, Gurugram, 20>
Properties of Relations
o Name of the relation is distinct from all other relations.
o Each relation cell contains exactly one atomic (single) value
o Each attribute contains a distinct name
o Attribute domain has no significance
o tuple has no duplicate value
o Order of tuple can have a different sequence
8
Relational 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.
1. Select Operation:
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ).
Notation: σ p(r)
Where:
9
Mianus L-13 500
Roundhill L-11 900
Perryride L-16 1300
Input:
σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
o 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.
o It is denoted by ∏.
Where
Input:
10
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
o 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.
o It eliminates the duplicate tuples. It is denoted by ∪.
Notation: R ∪ S
Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
11
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples
that are in both R & S.
o It is denoted by intersection ∩.
1. Notation: R ∩ S
Input:
Output:
12
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples
that are in R but not in S.
o It is denoted by intersection minus (-).
Notation: R - S
Input:
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
o 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.
o It is denoted by X.
Notation: E X D
Example:
EMPLOYEE
13
EMP_ID EMP_NAME EMP_DEPT
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
EMPLOYEE X DEPARTMENT
Output:
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
14
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
ρ(STUDENT1, STUDENT)
Relational Calculus
There is an alternate way of formulating queries known as Relational Calculus. Relational
calculus is a non-procedural query language. In the non-procedural query language, the
user is concerned with the details of how to obtain the end results. The relational calculus
tells what to do but never explains how to do. Most commercial relational languages are
based on aspects of relational calculus including SQL-QBE and QUEL.
Many of the calculus expressions involves the use of Quantifiers. There are two types
of quantifiers:
o Universal Quantifiers: The universal quantifier denoted by ∀ is read as for all which means
that in a given set of tuples exactly all tuples satisfy a given condition.
o Existential Quantifiers: The existential quantifier denoted by ∃ is read as for all which
means that in a given set of tuples there is at least one occurrences whose value satisfy a
given condition.
Before using the concept of quantifiers in formulas, we need to know the concept of Free
and Bound Variables.
A tuple variable t is bound if it is quantified which means that if it appears in any occurrences a
variable that is not bound is said to be free.
Free and bound variables may be compared with global and local variable of programming
languages.
15
Types of Relational calculus:
Notation:
Where
or example:
Output: This query selects the tuples from the AUTHOR relation. It returns a tuple with
'name' from Author who has written an article on 'database'.
TRC (tuple relation calculus) can be quantified. In TRC, we can use Existential (∃) and
Universal Quantifiers (∀).
For example:
16
{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}
Output: This query will yield the same result as the previous one.
Notation:
Where
For example:
17