Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Case Study Dbms Final

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Analysing and Optimizing Database Queries Using

Relational Calculus.
A Case Study

Submitted by

Mohd Talib Siddiqi (22BCS13509)

in partial fulfillment for the award of the


degree of

BACHELOR OF ENGINEERING

IN

COMPUTER SCIENCE

Chandigarh University

March & 2024


Introduction -

Relational Calculus is a different approach to query formulation. A non-procedural query language is


relational calculus. The user is concerned with the specifics of how to get the desired outcomes while using
the non-procedural query language. Though it never explains how to do anything, the relational calculus
instructs. Generally speaking, a "procedure language" is a programming language that lets users create
functions, procedures, or routines—that is, repeatable sets of instructions. These programming languages
offer a methodical approach to group code into modular, reusable sections, enhancing readability,
maintainability, and modularity.

Background-

Relational calculus, a foundational area of mathematical logic, is the source of relational calculus, a formal
language used in relational databases. Propositions are the subject of predicate calculus, where predicates
are truth-valued functions with arguments that produce statements known as propositions that may or may
not be true. comprehension relational calculus's goal—to give users an organized way to express queries in
relational databases—requires a solid understanding of its foundation.
Relational calculus is based on the relational model of data, which was first presented by Edgar F. Codd in
1970. With this structure, data is arranged into sets of tuples within relations, much like in database tables.
A record is represented by each tuple, and an attribute is a particular property. Relational calculus functions
within this architecture as its foundation.

Theoretical Framework -

The relational calculus is not the same as that of differential and integral calculus in mathematics but takes
its name from a branch of symbolic logic termed as predicate calculus.
When applied to databases, it is found in two forms. These are

• Tuple relational calculus which was originally proposed by Codd in the year 1972 and
• Domain relational calculus which was proposed by Lacroix and Pirotte in the year 1977

In first-order logic or predicate calculus, a predicate is a truth-valued function with arguments. When we
replace with values for the arguments, the function yields an expression, called a proposition, which will
be either true or false.
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

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

Where T is the resulting tuples P(T) is the condition used to fetch T.

For example:

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

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

Where a1,a2 are attributes P stands for formula built by inner attributes.

For example:

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

Implementation

Tuple Relational Calculus


TRC is a declarative language, meaning that it specifies what data is required from the database, rather
than how to retrieve it. TRC queries are expressed as logical formulas that describe the desired tuples.

Let's take an example of a Customer Database and try to see how TRC expressions work.

Customer Table
Customer_id Name Zip
code
1 Rohit 12345
2 Rahul 13245
3 Rohit 56789
4 Amit 12345.
Example 1: Write a TRC query to get all the data of customers whose zip
code is 12345.

TRC Query: {t \| Customer(t) t[Zipcode] = 12345 }

Workflow of query - The tuple variable "t" will go through every tuple of
the Customer table. Each row will check whether the Cust_Zipcode is 12345
or not and only return those rows that satisfies the Predicate expression
condition.

The TRC expression above can be read as "Return all the tuple which
belongs to the Customer Table and whose Zipcode is equal to 12345."

Result of the TRC expression above:


Customer_id Name Zip
code
1 Rohit 12345
4. Amit 12345
Example 2: Write a TRC query to get the customer id of all the Customers.

TRC query: { t.Customer_id|Customer(t)) }

Result of the TRC Query:

Customer_id
1
2
3
4
Domain Relational Calculus (DRC)

Domain Relational Calculus uses domain Variables to get the column values required from the
database based on the predicate expression or condition.

The Domain realtional calculus expression syntax:

{<x1,x2,x3,x4...> \| P(x1,x2,x3,x4...)}
where,

<x1,x2,x3,x4...> are domain variables used to get the column values required, and P(x1,x2,x3...) is
predicate expression or condition.

Let's take the example of Customer Database and try to understand DRC queries with some examples.
Customer Table
Customer_id Name Zip_code
1 Rohit 12345
2 Rahul 13245
3 Rohit 56789
4 Amit 12345

Example 1: Write a DRC query to get the data of all customers with Zip_code 12345.

DRC query: {<Customer_id,Name,Zip_code> ∈ Customer ∧ Zip_Code3 = 12345 }

Workflow of Query: In the above query x1,x2,x3 (ordered) refers to the attribute or column which we
need in the result, and the predicate condition is that the first two domain variables x1 and x2 should be
present while matching the condition for each row and the third domain variable x3 should be equal to
12345.

Result of the DRC query will be:


Customer_id Name Zip code
1 Rohit 12345
4 Amit 12345

You might also like