0% found this document useful (0 votes)
10 views17 pages

Unit 2

Uploaded by

rudraghankute07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

Unit 2

Uploaded by

rudraghankute07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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.

What is relational domain?


Domains are data type definitions that resolve to a primitive data type or
another domain. In relational database terminology, a domain defines the
permitted range of values for an attribute of an entity.

What is domain of a relation table?


Each column of a relational table is defined on a domain. A domain is a set
of values. A datatype, which is also defined for each column, consists of a
domain of values and a set of operations on those values. The sets S1, S2,
and S3 are sets of values.

What is an example of domain relation?


Consider the relation {(0,7),(0,8),(1,7),(1,8),(1,9),(2,10)} . Here, the relation
is given as a set of ordered pairs. The domain is the set of x -coordinates,
{0,1,2} , and the range is the set of y -coordinates, {7,8,9,10} .

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.

Q1: What is a Relation?

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.

Q2: What is the Domain of a Relation?

If a relation is defined on the cartesian product of A×B satisfying a specific


condition. Then the set of values taken as input from A satisfying the relation
is called the domain of the relation.

Q3: What is the Range of a Relation?

For a relation defined on the cartesian product of A×B and satisfying a


specific condition. Then the set of values as output taken from B to build the
relation is called the range of the relation.

Q4: What is a Function?

A relation in which each element of set A is uniquely mapped to the element


of set B in the cartesian product of A×B is called a function.

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.

We defined relation as,


R = {(x, y): x ∈ A and y ∈ B}

Example: The relation R = {(1,1), (2,4), (3,9)} and it is represented using { }.

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

Domain and Range of a Relation


As we know any set of ordered pairs that are related to a unique relation we
have domain and range i.e. for R such that R(A×B) such that {(a, b) where a ∈
A and b ∈ B} we have domain and range of R.

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

Example: Take a set S = {4, 5, 6, 9, 10,11,12,13, 17} and define a relation A


from S to S such that in the ordered pair (x, y) in A, y is two more than x.
Solution:
We define R as,
R = {(9,11), (10,12), (11,13)}
Thus,
 Domain of R is (9, 10, 11)
 Range of R is (11, 12, 13)
 Codomain is (4, 5, 6, 9,10,11,12,13, 17)

Examples on Domain and Range of a Relation

Example 1: Find the domain and range of the relation R: {(a,a2) | 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, 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}

Relational Model in DBMS


Relational model can represent as a table with columns and rows. Each row is known as a
tuple. Each table of the column has a name or attribute.

Domain: It contains a set of atomic values that an attribute can take.

Attribute: It contains the name of a column in a particular table. Each attribute Ai must
have a domain, dom(Ai)

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.

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

NAME ROLL_NO PHONE_NO ADDRESS AGE


Ram 14795 7305758992 Noida 24
Shyam 12839 9026288936 Delhi 35
Laxman 33289 8583287182 Gurugram 20

Mahesh 27857 7086819134 Ghaziabad 27

Ganesh 17282 9028 9i3988 Delhi 40

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.

Types of Relational operation

1. Select Operation:
o The select operation selects tuples that satisfy a given predicate.
o 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 =, ≠, ≥, <, >, ≤.

For example: LOAN Relation

BRANCH_NAME LOAN_NO AMOUNT


Downtown L-17 1000
Redwood L-23 2000
Perryride L-15 1500
Downtown L-14 1500

9
Mianus L-13 500
Roundhill L-11 900
Perryride L-16 1300

Input:

σ BRANCH_NAME="perryride" (LOAN)

Output:

BRANCH_NAME LOAN_NO AMOUNT


Perryride L-15 1500
Perryride L-16 1300

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 ∏.

Notation: ∏ A1, A2, An (r)

Where

A1, A2, A3 is used as an attribute name of relation r.

Example: CUSTOMER RELATION

NAME STREET CITY


Jones Main Harrison
Smith North Rye
Hays Main Harrison
Curry North Rye
Johnson Alma Brooklyn
Brooks Senator Brooklyn

Input:

∏ NAME, CITY (CUSTOMER)

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

A union operation must hold the following condition:

o R and S must have the attribute of the same number.


o Duplicate tuples are eliminated automatically.

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:

1. ∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)

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

Example: Using the above DEPOSITOR table and BORROW table

Input:

∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)

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

Example: Using the above DEPOSITOR table and BORROW table

Input:

∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)

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:

EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME


1 Smith A A Marketing

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.

Why it is called Relational Calculus?


It is based on Predicate calculus, a name derived from branch of symbolic language. A
predicate is a truth-valued function with arguments. On substituting values for the
arguments, the function result in an expression called a proposition. It can be either true
or false. It is a tailored version of a subset of the Predicate Calculus to communicate with
the relational database.

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:

1. Tuple Relational Calculus (TRC)


It is a non-procedural query language which is based on finding a number of tuple variables also
known as range variable for which predicate holds true. It describes the desired information
without giving a specific procedure for obtaining that information. The tuple relational calculus
is specified to select the tuples in a relation. In TRC, filtering variable uses the tuples of a
relation. The result of the relation can have one or more tuples.

Notation:

A Query in the tuple relational calculus is expressed as following notation

{T | P (T)} or {T | Condition (T)}

Where

T is the resulting tuples

P(T) is the condition used to fetch T.

or example:

{ T.name | Author(T) AND T.article = 'database' }

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.

2. Domain Relational Calculus (DRC)


The second form of relation is known as Domain relational calculus. In domain relational
calculus, filtering variable uses the domain of attributes. Domain relational calculus uses
the same operators as tuple calculus. It uses logical connectives ∧ (and), ∨ (or) and ┓
(not). It uses Existential (∃) and Universal Quantifiers (∀) to bind the variable. The QBE or
Query by example is a query language related to domain relational calculus.

Notation:

{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}

Where

a1, a2 are attributes


P stands for formula built by inner attributes

For example:

{< article, page, subject > | ∈ javatpoint ∧ subject = 'database'}


Output: This query will yield the article, page, and subject from the relational javatpoint, where
the subject is a database.

17

You might also like