5 The Relational Algebra And Relational Calculus

Database Management System · Unit 5 · 5 hrs

The Relational Algebra and Relational Calculus

Exam-focused notes for The Relational Algebra and Relational Calculus (Database Management System, CSC265): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.

The Relational Algebra and Relational Calculus in short

This unit develops relational algebra and relational calculus, the two formal query languages that give the relational model its mathematical foundation and against which every practical language, including SQL, can be checked for correctness.

The Relational Algebra and Relational Calculus: notes

A query language lets a user request information from a database. Procedural languages, such as relational algebra, have the user specify a sequence of operations that produce the desired result; non-procedural (declarative) languages, such as relational calculus and SQL, have the user describe only what result is wanted, leaving the DBMS to work out how to compute it.

All the examples below use one running relation,

Student

stu_idstu_namestu_addressdept_idage
101MayaPalpa122
102AbinKathmandu217
103AanavKathmandu121
104AshnaPalpa324
105AmyPokhara423

Unary Relational Operations: SELECT and PROJECT

Relational algebra is a procedural language whose operators take one or two relations as input and produce a new relation as output. The unary operators act on a single relation.

The SELECT operator, $\sigma$, chooses the subset of tuples of a relation that satisfy a given condition: $$\sigma_{\text{condition}}(R).$$ Comparison operators ($<, >, \leq, \geq, =, \neq$) build the condition, and logical connectives ($\land$ for AND, $\lor$ for OR, $\lnot$ for NOT) combine several conditions. For example, "find all students from Palpa" is $$\sigma_{stu_address = \text{'Palpa'}}(\text{Student}),$$ equivalent to the SQL

SELECT *
FROM Student
WHERE stu_address = 'Palpa';
stu_idstu_namestu_addressdept_idage
101MayaPalpa122
104AshnaPalpa324

"Find all students older than 20 or living in Kathmandu" is $\sigma_{age > 20 \lor stu_address = \text{'Kathmandu'}}(\text{Student})$. Checking each row against the two conditions: Maya (22, Palpa) qualifies on age, Abin (17, Kathmandu) qualifies on address, Aanav (21, Kathmandu) qualifies on both, Ashna (24, Palpa) qualifies on age, and Amy (23, Pokhara) qualifies on age; every row satisfies at least one disjunct, so here the selection happens to return the whole table.

The PROJECT operator, $\pi$, keeps only the named columns and discards the rest, additionally removing any duplicate rows that result: $$\pi_{\text{attribute list}}(R).$$ "Display the id and name of every student" is $\pi_{stu_id,\ stu_name}(\text{Student})$, equivalent to SELECT stu_id, stu_name FROM Student;.

SELECT and PROJECT combine into a single expression, for example "name, address and age of students aged 25 or under": $$\pi_{stu_name,\ stu_address,\ age}\big(\sigma_{age \leq 25}(\text{Student})\big),$$ which is the same as the SQL

SELECT stu_name, stu_address, age
FROM Student
WHERE age <= 25;

Relational algebra expressions can be written in-line, nesting operations as above, or as a sequence of assignments, giving each intermediate relation a name with the assignment operator $\leftarrow$, which is often clearer for a multi-step computation: $$\text{PALPA_STUD} \leftarrow \sigma_{stu_address = \text{'Palpa'}}(\text{Student}), \qquad \text{RESULT} \leftarrow \pi_{stu_name,\ age}(\text{PALPA_STUD}).$$ The RENAME operator, $\rho$, renames a relation, its attributes, or both: $\rho_{S(B_1, \ldots, B_n)}(R)$ renames $R$ to $S$ and its attributes to $B_1, \ldots, B_n$.

What this unit covers

  • Unary Relational Operations: SELECT and PROJECT
  • Relational Algebra Operations from Set Theory
  • Binary Relational Operations: JOIN and DIVISION
  • Additional Relational Operations
  • the Tuple Relational Calculus
  • the Domain Relational Calculus

the Tuple Relational Calculus

2080 · 5 marks

What is tuple relational calculus? Explain. [5]

Tuple Relational Calculus (TRC) is a non-procedural query language used to query relational databases. Unlike relational algebra (which is procedural), tuple relational calculus only describes what data is needed, not how to retrieve it. It provides the description of the query but does not provi...

Open this answer on its own page →

2080.1 · 5 marks

What is tuple realtion calculus ? Given the following schema, write tuple relational calculus for selecting name and address of employee who are working in a company having Cid=E01. Employee(Eid, Ename, Address, Cid) Company(Cid, CName) [5]

Schema: - Employee(Eid, Ename, Address, Cid) - Company(Cid, CName) Requirement: Retrieve Ename and Address of employees working in a company with Cid = E01. No numeric matrices or missing data; this is a conceptual/query question. --- Tuple Relational Calculus (TRC) is a non-procedural (declarati...

Open this answer on its own page →

Binary Relational Operations

2080 · 5 marks

Write short notes on: a. Natural join b. Shadow paging [5]

Show solved answer

Short Notes


a. Natural Join

Natural Join is a binary relational operation that combines two relations based on their common attributes (columns that share the same name and same data type).

Key Characteristics:

  • It is denoted by the join symbol (⋈)
  • The common attribute(s) must have the same name and same data type in both relations
  • It automatically performs an equi-join on all matching attribute names
  • It eliminates duplicate attributes from the result, keeping only one copy of the common column(s)
  • It effectively combines selection and Cartesian product into a single operation

Example:

If relation R(A, B, C) and relation S(C, D, E) share attribute C:

R ⋈ S  produces tuples where R.C = S.C, with C appearing only once
Result schema: (A, B, C, D, E)

Important Note on Lossless Decomposition:

A decomposition {R1, R2, R3} of a relation R is called a lossless decomposition if the natural join of R1, R2, R3 produces exactly the original relation R.

However, if a "fat relation" (one with too many grouped attributes) is split and then rejoined using natural join, it may generate more tuples than the original, making it impossible to recover the original table. This is known as a lossy decomposition.


b. Shadow Paging

Shadow paging is a recovery technique used in database systems to handle transaction failures without using a traditional log.

Basic Concept:

  • The database is considered to be made up of a number of fixed-sized disk blocks (say n blocks)
  • A directory with n entries is constructed, where the i-th entry points to the i-th database page on disk
  • This directory is kept in main memory if it is not too large

How It Works:

ComponentDescription
Shadow DirectoryThe original, stable directory pointing to pages before the transaction
Current DirectoryA working copy that reflects changes made during the transaction

During Transaction Execution:

  1. The shadow directory is never modified during transaction execution
  2. When a write operation is performed on a page:
    • A new copy of the modified page is created on a previously unused disk block
    • The old copy is NOT overwritten
  3. For every page updated by the transaction, two versions are maintained:
    • Old version referenced by the shadow directory
    • New version referenced by the current directory

Recovery:

  • On transaction failure: simply discard the current directory and restore the shadow directory. The old pages remain intact.
  • On successful commit: the current directory replaces the shadow directory permanently.

Advantage:

Simple recovery with no need for redo/undo logs for the modified pages.

Disadvantage:

Requires extra disk space to maintain two versions of modified pages simultaneously.

Open this answer on its own page →

2079 · 5 marks

Retrieve the TName, and No_of_priod of teachers who teach in 'ABC' school using Relational Algebra.TEACHER (TID, TName, TAddress, TQualification), SCHOOL (SID, SName, SAddress, SPhone), SCHOOL_TEACHER (SID, TID, No_of_Period) [5]

Relation Attributes ------ TEACHER (TID, TName, TAddress, TQualification) SCHOOL (SID, SName, SAddress, SPhone) SCHOOLTEACHER (SID, TID, NoofPeriod) --- Apply the Selection (σ) operation to filter the school with name 'ABC': $$\sigma{SName = 'ABC'} (SCHOOL)$$ This gives us the tuple(s) for 'ABC' ...

Open this answer on its own page →

2078 · 5 marks

Explain the difference between 'Join' and 'Natural Join' of algebraic operations with example. [5]

A Join operation combines tuples from two relations based on a specified condition (called a join predicate). The condition is explicitly stated by the user and can involve any comparison operator (=, <, , <=, =, !=). - The joining attribute(s) need not have the same name in both relations. - Dup...

Open this answer on its own page →