0% found this document useful (0 votes)
5 views18 pages

Relational Algebra and Relational Tuple Calculus

The document provides an overview of relational algebra, a procedural query language that operates on relations through various operations such as selection, projection, union, intersection, difference, and Cartesian product. It also discusses join operations, including inner and outer joins, and introduces relational calculus, a non-procedural query language that focuses on what to retrieve rather than how to retrieve it. Examples illustrate the application of these concepts in database queries.

Uploaded by

Priksha Sharma
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)
5 views18 pages

Relational Algebra and Relational Tuple Calculus

The document provides an overview of relational algebra, a procedural query language that operates on relations through various operations such as selection, projection, union, intersection, difference, and Cartesian product. It also discusses join operations, including inner and outer joins, and introduces relational calculus, a non-procedural query language that focuses on what to retrieve rather than how to retrieve it. Examples illustrate the application of these concepts in database queries.

Uploaded by

Priksha Sharma
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/ 18

Relational Algebra

The relational algebra is a relation-at-a-time (or set) language where all tuples are controlled
in one statement without the use of a loop. 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.
There are several variations of syntax for relational algebra commands, and you use a
common symbolic notation for the commands and present it informally.

Types of Relational operation

1. Select Operation:

o The select operation selects tuples that satisfy a given predicate.


o It is denoted by sigma (σ).

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

ence between JDK, JRE, and JVM


BRANCH_NAME LOAN_NO AMOUNT

Downtown L-17 1000

Redwood L-23 2000

Perryride L-15 1500

Downtown L-14 1500

Mianus L-13 500

Roundhill L-11 900

Perryride L-16 1300

Input:

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

1. 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:

1. ∏ NAME, CITY (CUSTOMER)

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

1. 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
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:

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

Output:

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 (-).

1. Notation: R - S

Example: Using the above DEPOSITOR table and BORROW table

Input:

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

1. Notation: E X D

Example:

EMPLOYEE

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:

1. EMPLOYEE X DEPARTMENT

Output:

EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT

1 Smith A A Marke

1 Smith A B Sales

1 Smith A C Legal

2 Harry C A Marke

2 Harry C B Sales

2 Harry C C Legal

3 John B A Marke

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 (ρ).

Example: We can use the rename operator to rename STUDENT relation to STUDENT1.

1. ρ(STUDENT1, STUDENT)

Join Operations
Join operation is essentially a cartesian product followed by a selection criterion.
Join operation denoted by ⋈.

JOIN operation also allows joining variously related tuples from different relations.

Types of JOIN:

Various forms of join operation are:

Inner Joins:

• Theta join
• EQUI join
• Natural join

Outer join:

• Left Outer Join


• Right Outer Join
• Full Outer Join

Theta (θ) Join


Theta join combines tuples from different relations provided they satisfy the theta condition.
The join condition is denoted by the symbol θ.

Notation

R1 ⋈θ R2
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn) such that the
attributes don’t have anything in common, that is R1 ∩ R2 = Φ.
Theta join can use all kinds of comparison operators.

Student

SID Name Std

101 Alex 10

102 Maria 11

Subjects
Class Subject

10 Math

10 English

11 Music

11 Sports

Student_Detail −
STUDENT ⋈ Student.Std = Subject.Class SUBJECT
Student_detail

SID Name Std Class Subject

101 Alex 10 10 Math

101 Alex 10 10 English

102 Maria 11 11 Music

102 Maria 11 11 Sports

Equijoin
When Theta join uses only equality comparison operator, it is said to be equijoin. The above
example corresponds to equijoin.
Natural Join (⋈)
Natural join does not use any comparison operator. It does not concatenate the way a Cartesian
product does. We can perform a Natural Join only if there is at least one common attribute
that exists between two relations. In addition, the attributes must have the same name and
domain.
Natural join acts on those matching attributes where the values of attributes in both the
relations are same.
Courses

CID Course Dept

CS01 Database CS

ME01 Mechanics ME

EE01 Electronics EE

HoD

Dept Head

CS Alex

ME Maya

EE Mira

Courses ⋈ HoD

Dept CID Course Head

CS CS01 Database Alex

ME ME01 Mechanics Maya

EE EE01 Electronics Mira

Outer Joins
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.
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.

Left

A B

100 Database

101 Mechanics

102 Electronics

Right

A B

100 Alex

102 Maya

104 Mira

Courses HoD

A B C D

100 Database 100 Alex


101 Mechanics --- ---

102 Electronics 102 Maya

Right Outer Join: ( R S)


All the tuples from the Right relation, S, are included in the resulting relation. If there are
tuples in S without any matching tuple in R, then the R-attributes of resulting relation are
made NULL.

Courses HoD

A B C D

100 Database 100 Alex

102 Electronics 102 Maya

--- --- 104 Mira

Full Outer Join: ( R S)


All the tuples from both participating relations are included in the resulting relation. If there
are no matching tuples for both relations, their respective unmatched attributes are made
NULL.

Courses HoD

A B C D

100 Database 100 Alex

101 Mechanics --- ---

102 Electronics 102 Maya


--- --- 104 Mira

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.

Tuple Relational Calculus (TRC) in DBMS


Tuple Relational Calculus is a non-procedural query language unlike relational algebra.
Tuple Calculus provides only the description of the query but it does not provide the methods
to solve it. Thus, it explains what to do but not how to do.

In Tuple Calculus, a query is expressed as

{t| P(t)}
where t = resulting tuples,
P(t) = known as Predicate and these are the conditions that are used to fetch t
Thus, it generates set of all tuples t, such that Predicate P(t) is true for t.
P(t) may have various conditions logically combined with OR (∨), AND (∧), NOT(¬).
It also uses quantifiers:

∃ t ∈ r (Q(t)) = ”there exists” a tuple in t in relation r such that predicate Q(t) is true.
∀ t ∈ r (Q(t)) = Q(t) is true “for all” tuples in relation r.
Example:

Table-1: Customer
Customer name Street City

Saurabh A7 Patiala

Mehak B6 Jalandhar

Sumiti D9 Ludhiana
Customer name Street City

Ria A5 Patiala

Table-2: Branch
Branch name Branch city

ABC Patiala

DEF Ludhiana

GHI Jalandhar

Table-3: Account
Account number Branch name Balance

1111 ABC 50000

1112 DEF 10000

1113 GHI 9000

1114 ABC 7000

Table-4: Loan
Loan number Branch name Amount

L33 ABC 10000

L35 DEF 15000

L49 GHI 9000


Loan number Branch name Amount

L98 DEF 65000

Table-5: Borrower
Customer name Loan number

Saurabh L33

Mehak L49

Ria L98

Table-6: Depositor
Customer name Account number

Saurabh 1111

Mehak 1113

Sumiti 1114

Queries-1: Find the loan number, branch, amount of loans of greater than or equal to 10000
amount.

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


Resulting relation:

Loan number Branch name Amount

L33 ABC 10000

L35 DEF 15000


Loan number Branch name Amount

L98 DEF 65000

In the above query, t[amount] is known as tupple variable.


Queries-2: Find the loan number for each loan of an amount greater or equal to 10000.
{ t | ∃ s ∈ loan(t[loan number] = s[loan number]
∧ s[amount]>=10000)}
Resulting relation:

Loan number

L33

L35

L98

Queries-3: Find the names of all customers who have a loan and an account at the bank.
{t | ∃ s ∈ borrower( t[customer-name] = s[customer-name])
∧ ∃ u ∈ depositor( t[customer-name] = u[customer-name])}
Resulting relation:

Customer name

Saurabh

Mehak

Queries-4: Find the names of all customers having a loan at the “ABC” branch.

{t | ∃ s ∈ borrower(t[customer-name] = s[customer-name]
∧ ∃ u ∈ loan(u[branch-name] = “ABC” ∧ u[loan-number] = s[loan-number]))}

Resulting relation:
Customer name

Saurabh

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy