0% found this document useful (0 votes)
3 views

DBMS Unit-4 (2)

The document discusses formal relational query languages, focusing on relational algebra, tuple relational calculus, and domain relational calculus as foundational concepts for database query languages like SQL. It outlines the types of relational query languages, procedural and non-procedural, and details various operations in relational algebra such as selection, projection, union, and joins. Additionally, it explains the principles of relational calculus, including its use of quantifiers and examples of queries using tuple relational calculus.

Uploaded by

Moon Knight
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)
3 views

DBMS Unit-4 (2)

The document discusses formal relational query languages, focusing on relational algebra, tuple relational calculus, and domain relational calculus as foundational concepts for database query languages like SQL. It outlines the types of relational query languages, procedural and non-procedural, and details various operations in relational algebra such as selection, projection, union, and joins. Additionally, it explains the principles of relational calculus, including its use of quantifiers and examples of queries using tuple relational calculus.

Uploaded by

Moon Knight
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/ 51

Unit-4

Formal Relational Query Languages

Prepared By:
Dr. Aayushi Chaudhari
Assistant Professor, CE, CSPIT,
CHARUSAT

9 January 2025| U & P U. Patel Department of Computer Engineering 1


Agenda
• Relational Algebra
• Tuple Relational Calculus
• Domain Relational Calculus

9 January 2025| U & P U. Patel Department of Computer Engineering 2


Relational Query languages
● Relational query languages are used as a theoretical foundation for designing
and understanding database query languages like SQL.
● It allows developers to express complex data retrieval operations in a
structured and mathematically precise way, even though most users interact
with databases through a more user-friendly interface based on these formal
languages.
Types of Relational Query Language

There are two types of relational query language:


● Procedural Query Language
● Non-Procedural Language
9 January 2025| U & P U. Patel Department of Computer Engineering 3
Procedural Query language
● In Procedural Language, the user instructs the system to perform a series of
operations on the database to produce the desired results.
● Users tell what data to be retrieved from the database and how to retrieve it.
● Procedural Query Language performs a set of queries instructing the DBMS
to perform various transactions in sequence to meet user requests.
● In a Procedural Query Language, the user species both what data is required
and how to obtain it. This type of language requires users to write a sequence
of operations to retrieve the desired result. The emphasis is on the procedure
or steps needed to achieve the output.

9 January 2025| U & P U. Patel Department of Computer Engineering 4


Relational Algebra
● Relational Algebra is a Procedural Query Language
● Relational Algebra could be defined as the set of operations on relations.
● Relational Algebra is a foundational Procedural Query Language for relational databases. It
consists of a set of operations that take one or two relations as input and produce a new
relation as output. Some common relational algebra operations include:
Basic operators: select select, project project, union union, set set difference difference,
Cartesian Cartesianproduct product
Derived operators: set intersection set intersection, division division, join

9 January 2025| U & P U. Patel Department of Computer Engineering 5


Role of relational Algebra in DBMS

9 January 2025| U & P U. Patel Department of Computer Engineering 6


9 January 2025| U & P U. Patel Department of Computer Engineering 7
Basic Operators of Relational Algebra
1. Select (σ):
Denition: Filters rows based on a given predicate.
Notation: σ_condition(R)
Example: σ_age > 25(STUDENT), retrieves all students older than 25.

2. Projection (π):
Denition: Selects specific columns from a relation.
Notation: π_column1, column2,…(R)
Example: π_name, phone_no(STUDENT), retrieves only the names and phone
numbers of students.

9 January 2025| U & P U. Patel Department of Computer Engineering 8


Cont..
3. Set Difference (-):
Denition: Returns the difference between two relations.
Notation: R – S
Example: STUDENT – ENROLLED_STUDENT retrieves students who are not enrolled in any course.
4. Cartesian Product (×):
Denition: Combines each tuple of one relation with every tuple of another relation.
Notation: R × S
Example: STUDENT × COURSE produces a relation combining every student with every course.
5. Union (∪):
Denition: Combines tuples from two relations, eliminating duplicates.
Notation: R ∪ S
Example: STUDENT ∪ GRADUATED_STUDENT retrieves all students who are either current or graduated.

9 January 2025| U & P U. Patel Department of Computer Engineering 9


Select Operation
• Produce table containing subset of rows of argument table satisfying condition
• Notation: σ p(r)
• p is called the selection predicate
• Defined as:

σp(r) = {t | t ∈ r and p(t)}

Where p is a formula in propositional calculus consisting of terms connected by : ∧ (and), ∨ (or), ¬ (not)
Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, ≠, >, ≥, <, ≤

• Example of selection:

σ dept_name=“Physics”(instructor)

9 January 2025| U & P U. Patel Department of Computer Engineering 10


Select example

9 January 2025| U & P U. Patel Department of Computer Engineering 11


Projection Operation
• Notation:
∏A1,A2 (r)
where A1, A2 are attribute names and r is a relation name.
• The result is defined as the relation of k columns obtained by erasing the columns that are
not listed.
• Duplicate rows removed from result, since relations are sets
• Example: To eliminate the dept_name attribute of instructor and display ID,name,salary
column.
∏ID, name, salary (instructor)

9 January 2025| U & P U. Patel Department of Computer Engineering 12


Project Operator Example

9 January 2025| U & P U. Patel Department of Computer Engineering 13


Expressions (Select + Project)

9 January 2025| U & P U. Patel Department of Computer Engineering 14


Set Operators
● Relation is a set of tuples, so set operations should apply: ∩, ∪, −(set
difference)
● Result of combining two relations with a set operator is a relation =>
all its elements must be tuples having same structure
● Hence, scope of set operations limited to union compatible relations

9 January 2025| U & P U. Patel Department of Computer Engineering 15


Union Operation
• Two relations are union compatible union compatible if–
• Both have same number of columns
• Names of attributes are the same in both
• Attributes with the same name in both relations have the same domain
• Union compatible relations can be combined using union, intersection, and set difference
• Notation: r ∪ s
• Defined as:
r ∪ s = {t | t ∈ r or t ∈ s}
• Example: to find all courses taught in the Fall 2009 semester, or in the Spring 2010 semester, or in both
∏course_id (σ semester=“Fall” Λ year=2009 (section)) ∪
∏course_id (σ semester=“Spring” Λ year=2010 (section))

9 January 2025| U & P U. Patel Department of Computer Engineering 16


Union compatibility

9 January 2025| U & P U. Patel Department of Computer Engineering 17


Set Difference Operation
• Notation r – s
• Defined as:
r – s = {t | t ∈ r and t ∉ s}

• Set differences must be taken between compatible relations.


• r and s must have the same arity
• attribute domains of r and s must be compatible

• Example: to find all courses taught in the Fall 2009 semester, but not in the Spring 2010 semester

∏course_id (σ semester=“Fall” Λ year=2009 (section)) −

∏course_id (σ semester=“Spring” Λ year=2010 (section))

9 January 2025| U & P U. Patel Department of Computer Engineering 18


Cartesian-Product Operation
• 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.

• Notation r x s
• Defined as:
r x s = {t q | t ∈ r and q ∈ s}

• Assume that attributes of r(R) and s(S) are disjoint. (That is, R ∩ S = ∅).
• If attributes of r(R) and s(S) are not disjoint, then renaming must be used.

9 January 2025| U & P U. Patel Department of Computer Engineering 19


Rename Operation
• Rename operator basically 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.
• Example:
ρ(a/b)R will rename the attribute 'b' of the relation by 'a'.

9 January 2025| U & P U. Patel Department of Computer Engineering 20


Derived Operators : Join

9 January 2025| U & P U. Patel Department of Computer Engineering 21


Inner joins
Three types of inner joins include:
Theta
Equi
Natural

9 January 2025| U & P U. Patel Department of Computer Engineering 22


Theta join

9 January 2025| U & P U. Patel Department of Computer Engineering 23


Equijoin Join

9 January 2025| U & P U. Patel Department of Computer Engineering 24


Natural Join

9 January 2025| U & P U. Patel Department of Computer Engineering 25


Division

9 January 2025| U & P U. Patel Department of Computer Engineering 26


Division

9 January 2025| U & P U. Patel Department of Computer Engineering 27


Example - Division

9 January 2025| U & P U. Patel Department of Computer Engineering 28


Example of Division
A B1 B2 B3
sno pno pno pno pno

p2 p2 p1
s1 p1 p4 p2
s1 p2 p4
s1 p3
s1 p4
s2 p1
s2 p2
s3 p2 sno
s4 p2
s4 p4 s1 sno
s2 sno
s3 s1
s4 s4 s1

A/B1 A/B2 A/B3

9 January 2025| U & P U. Patel Department of Computer Engineering 29


Set-Intersection Operation
• Notation: r ∩ s
• Defined as:
• r ∩ s = { t | t ∈ r and t ∈ s }
• Assume:
• r, s have the same arity
• attributes of r and s are compatible
• Note: r ∩ s = r – (r – s)

9 January 2025| U & P U. Patel Department of Computer Engineering 30


Set Intersection
1. The INTERSECTION operation is commutative, that is :

A∩B=B∩A
2. The INTERSECTION is associative, that means it is applicable to any number of relation.

A∩(B∩C)=(A∩B)∩C
3. INTERSECTION can be formed using UNION and MINUS as follows:

A ∩ B = ((A ∪ B) - (A - B)) - (B - A)

9 January 2025| U & P U. Patel Department of Computer Engineering 31


Example
Consider a relation Student(FIRST, LAST) and Faculty(FIRSTN, LASTN) given below :
Student Faculty Student ∪ Faculty Student ∩ Faculty Student - Faculty
First Last FirstN LastN First Last First Last First Last

Aisha Arora Raj Kumar Aisha Arora Makku Singh Aisha Arora

Bikash Dutta Honey Chand Bikash Dutta Bikash Dutta

Makku Singh Makku Singh


Makku Singh Raju Chopra

Raju Chopra
Raju Chopra Karan Rao
Raj Kumar

Honey Chand

Karan Rao

9 January 2025| U & P U. Patel Department of Computer Engineering 32


Relational Calculus
• Relational Calculus is an alternative way for expressing queries
• Main feature: specify what you want, not how to get it
• Many equivalent algebra “implementations” possible for a given calculus
expression
• In short: SQL query without aggregation = relational calculus expression
• Relational algebra expression is similar to program, describing what
operations to perform in what order

9 January 2025| U & P U. Patel Department of Computer Engineering 33


What is Relational Calculus?
• It is a formal language based upon predicate calculus
• It allows you to express the set of tuples you want from a database using a formula
• Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus
(DRC)
• TRC: Variables range over (i.e., get bound to) tuples.
• DRC: Variables range over domain elements (= attribute values)
• Expressions in the calculus are called formulas

9 January 2025| U & P U. Patel Department of Computer Engineering 34


Rules
When using relational calculus, two common quantifiers are often employed:
Universal Quantifier ( ∀ ): This means "for all." It indicates that all tuples in a given set must
satisfy a particular condition.
Existential Quantifier ( ∃ ): This means "there exists." It shows that at least one tuple in the
given set satisfies the specified condition.
To better understand these, we also need to be familiar with the terms Free
Variables and Bound Variables:
A variable is considered bound if it is quantified (i.e., linked to either ∀ or ∃).
A free variable is not linked to any quantifier, much like global variables in programming.

9 January 2025| U & P U. Patel Department of Computer Engineering 35


Types of Relational Calculus

9 January 2025| U & P U. Patel Department of Computer Engineering 36


Tuple Relational Calculus
• Tuple Relational Calculus (TRC) is a non-procedural query language where we work
with tuples (rows) of a relation. In TRC, we express the desired result by specifying
conditions, without defining how to obtain the result.
• A nonprocedural query language, where each query is of the form
{t | P (t ) }
 t represents the resulting tuples.
 P(t) represents the condition or predicate used to filter those tuples.

9 January 2025| U & P U. Patel Department of Computer Engineering 37


Example
1. Suppose we have an EMPLOYEE table with EMP_ID, NAME, and SALARY attributes.
We want to retrieve the names of employees who earn a salary greater than 50,000.
EMP_ID NAME SALARY
101 Alice 60,000
102 Bob 45,000
103 Charlie 55,000
Query:
{T.NAME | EMPLOYEE(T) AND T.SALARY > 50,000}
Output:
NAME
Alice
Charlie

9 January 2025| U & P U. Patel Department of Computer Engineering 38


Using Quantifiers in TRC
TRC allows the use of Existential (∃) and Universal (∀) quantifiers to refine queries further.
We can use quantifiers to find employees who earn more than 50,000 and also work in the HR
department. Suppose we add a DEPARTMENT column to our EMPLOYEE table:
EMP_ID NAME SALARY DEPARTMENT

101 Alice 60,000 HR


102 Bob 45,000 IT
103 Charlie 55,000 SUPPORT
Query:
{T.NAME | ∃T ∈ EMPLOYEE (T.SALARY > 50,000 AND T.DEPARTMENT = 'HR')}
Output: NAME
Alice

9 January 2025| U & P U. Patel Department of Computer Engineering 39


Examples
Customer Account Branch Loan Branch
Street City Balance Amount
name number name number name

Saurabh A7 Patiala 1111 ABC 50000 L33 ABC 10000

Mehak B6 Jalandhar 1112 DEF 10000 L35 DEF 15000

Sumiti D9 Ludhiana 1113 GHI 9000 L49 GHI 9000

Ria A5 Patiala 1114 ABC 7000 L98 DEF 65000

Customer Account Loan


Find the loan number, branch, and amount of loans greater than or equal Loan number Branch name Amount

to 10000 amount. L33 ABC 10000

L35 DEF 15000


{t| t ∈ loan ∧ t[amount]>=10000}
L98 DEF 65000

9 January 2025| U & P U. Patel Department of Computer Engineering 40


Examples
Customer
Street City Customer name Account number
name Customer name Loan number

Saurabh A7 Patiala Saurabh 1111


Saurabh L33
Mehak B6 Jalandhar
Mehak 1113
Mehak L49
Sumiti D9 Ludhiana

Ria A5 Patiala Ria L98 Suniti 1114

Customer Borrower Depositor


Find the names of all customers who have a loan and an account at the bank.
Customer name

{t | ∃ s ∈ borrower( t[customer-name] = s[customer-name]) Saurabh

∧ ∃ u ∈ depositor( t[customer-name] = u[customer-name])} Mehak

9 January 2025| U & P U. Patel Department of Computer Engineering 41


Examples
Customer Loan Branch
Street City Customer name Loan number Amount
name number name

Saurabh A7 Patiala L33 ABC 10000


Saurabh L33
Mehak B6 Jalandhar L35 DEF 15000
Mehak L49
Sumiti D9 Ludhiana L49 GHI 9000

Ria A5 Patiala Ria L98 L98 DEF 65000

Customer Borrower Loan


Find the names of all customers having a loan at the “ABC” branch.
Customer name

Saurabh
{t | ∃ s ∈ borrower(t[customer-name] = s[customer-name]
∧ ∃ u ∈ loan(u[branch-name] = “ABC” ∧ u[loan-number] = s[loan-number]))}
9 January 2025| U & P U. Patel Department of Computer Engineering 42
Universal Quantification
• Find all students who have taken all courses offered in the Biology department
• {t | ∃ r ∈ student (t [ID] = r [ID]) ∧
(∀ u ∈ course (u [dept_name]=“Biology” ⇒
∃ s ∈ takes (t [ID] = s [ID ] ∧
s [course_id] = u [course_id]))}

9 January 2025| U & P U. Patel Department of Computer Engineering 43


Safety of Expressions (Cont.)
• Consider again that query to find all students who have taken all courses offered in the Biology department
• {t | ∃ r ∈ student (t [ID] = r [ID]) ∧
(∀ u ∈ course (u [dept_name]=“Biology” ⇒
∃ s ∈ takes (t [ID] = s [ID ] ∧
s [course_id] = u [course_id]))}
• Without the existential quantification on student, the above query would be unsafe if the Biology department
has not offered any courses.

9 January 2025| U & P U. Patel Department of Computer Engineering 44


Domain Relational Calculus
• Domain Relational Calculus (DRC) is another type of non-procedural query language. Instead of
working with tuples, DRC works with individual attribute values. In DRC, conditions are applied
to domains (i.e., attributes of the table), and the result consists of specific attributes that satisfy those
conditions.
• Notation:
• A query in DRC is expressed as:

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

• a1, a2, ... are attributes of the relation.


• P represents the condition or predicate applied to the attributes.
9 January 2025| U & P U. Patel Department of Computer Engineering 45
Example
1. Suppose we have an EMPLOYEE table with EMP_ID and NAME attributes. We want to
retrieve the names of employees who earn a salary greater than 50,000.
EMP_ID NAME SALARY
101 Alice 60,000
102 Bob 45,000
103 Charlie 55,000
DRC Query:
{EMP_ID, NAME | EMPLOYEE(EMP_ID, NAME, SALARY) AND SALARY > 50,000}
Output: EMP_ID NAME
101 Alice
103 Charlie

9 January 2025| U & P U. Patel Department of Computer Engineering 46


Using Quantifiers in DRC
Similar to TRC, DRC also supports Existential (∃) and Universal (∀) quantifiers to refine
queries. EMP_ID NAME SALARY DEPARTMENT

101 Alice 60,000 HR


102 Bob 45,000 IT
103 Charlie 55,000 SUPPORT
Let’s say we want to find employees who earn more than 50,000 and work in the HR
department. Using quantifiers, we can achieve this as follows:
Query:
{EMP_ID, NAME | ∃(EMP_ID, NAME, SALARY, DEPARTMENT) ∈ EMPLOYEE
(SALARY > 50,000 AND DEPARTMENT = 'HR')} EMP_ID NAME
101 Alice
103 Charlie

9 January 2025| U & P U. Patel Department of Computer Engineering 47


Example
Loan number Branch name Amount

L01 Main 200

L03 Main 150

L10 Sub 90

L08 Main 60

Find the loan number, branch, amount of loans of greater than or equal to 100 amount.
{≺l, b, a≻ | ≺l, b, a≻ ∈ loan ∧ (a ≥ 100)} Loan number Branch name Amount

L01 Main 200

L03 Main 150

9 January 2025| U & P U. Patel Department of Computer Engineering 48


Example
Loan number Branch name Amount

L01 Main 200 Customer name Loan number

L03 Main 150 Ritu L01

L10 Sub 90 Debomit L08

L08 Main 60 Soumya L03

Find the names of all customers having a loan at the “Main” branch and find the loan amount.
{≺c, a≻ | ∃ l (≺c, l≻ ∈ borrower ∧ ∃ b (≺l, b, a≻ ∈ loan ∧ (b = “Main”)))}
Customer Name Amount

Ritu 200

Debomit 60

Soumya 150

9 January 2025| U & P U. Patel Department of Computer Engineering 49


Universal Quantification
• Find all students who have taken all courses offered in the Biology department
• {< i > | ∃ n, d, tc ( < i, n, d, tc > ∈ student ∧
(∀ ci, ti, dn, cr ( < ci, ti, dn, cr > ∈ course ∧ dn =“Biology”
⇒ ∃ si, se, y, g ( <i, ci, si, se, y, g> ∈ takes ))}
• Note that without the existential quantification on student, the above query would be unsafe if the
Biology department has not offered any courses.

9 January 2025| U & P U. Patel Department of Computer Engineering 50

You might also like