0% found this document useful (0 votes)
4 views112 pages

Unit3 dbms

The document provides an overview of relational algebra, a procedural query language used in relational database systems, detailing its fundamental operations such as select, project, union, set difference, Cartesian product, and rename. It also contrasts relational algebra with relational calculus, a non-procedural query language, explaining its two forms: Tuple Relational Calculus and Domain Relational Calculus. Examples illustrate how these operations work with relations and their outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views112 pages

Unit3 dbms

The document provides an overview of relational algebra, a procedural query language used in relational database systems, detailing its fundamental operations such as select, project, union, set difference, Cartesian product, and rename. It also contrasts relational algebra with relational calculus, a non-procedural query language, explaining its two forms: Tuple Relational Calculus and Domain Relational Calculus. Examples illustrate how these operations work with relations and their outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 112

DATABASE MANAGEMENT

SYSTEM

BDA202/BDA252

Unit-3
RELATIONAL ALGEBRA
 Relational database systems are expected to be
equipped with a query language that can assist its
users to query the database instances. There are two
kinds of query languages − relational algebra and
relational calculus.

 Relational Algebra

 Relational algebra is a procedural query language,


which takes instances of relations as input and yields
instances of relations as output. It uses operators to
perform queries. An operator can be
either unary or binary. They accept relations as their
input and yield relations as their output. Relational
algebra is performed recursively on a relation and
intermediate results are also considered relations.
RELATIONAL ALGEBRA
 The fundamental operations of relational algebra
are as follows −

 Select
 Project

 Union

 Set different

 Cartesian product

 Rename

 We will discuss all these operations in the following


sections.
SELECT OPERATION
 It selects tuples that satisfy the given predicate from a
relation.

 Notation − σ p(r)

 Where σ stands for selection predicate and r stands for


relation. p is prepositional logic formula which may use
connectors like and, or, and not. These terms may use
relational operators like : =, ≠, ≥, < , >, ≤.

 For example −

 σ subject = "database"(Books)
 Output − Selects tuples from books where subject is

'database'.
SELECT OPERATION
 σ subject = "database" and price = "450"(Books)

 Output − Selects tuples from books where subject is


'database' and 'price' is 450.

 σ subject = "database" and price = "450" or year >


"2010"(Books)

 Output − Selects tuples from books where subject is


'database' and 'price' is 450 or those books
published after 2010.
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
Mianus L-13 500
Roundhill L-11 900
Perryride L-16 1300

σ BRANCH_NAME="perryride" (LOAN)

Output:

BRANCH_NAME LOAN_NO AMOUNT

Perryride L-15 1500


Perryride L-16 1300
PROJECT OPERATION (∏)
 It projects column(s) that satisfy a given predicate.

 Notation − ∏ A1, A2, An (r)

 Where A1, A2 , An are attribute names of relation r.

 Duplicate rows are automatically eliminated, as


relation is a set. For example −

 ∏ subject, author (Books)

 Selects and projects columns named as subject and


author from the relation Books.
Example: CUSTOMER RELATION

NAME STREET CITY

Jones Main Harrison


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

∏ NAME, CITY (CUSTOMER) Output:

NAME CITY

Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
UNION OPERATION (∪)
 It performs binary union between two given
relations and is defined as −
 r ∪ s = { t | t ∈ r or t ∈ s}

 Notation − r U s

 Where r and s are either database relations or

relation result set (temporary relation).


 For a union operation to be valid, the following

conditions must hold −


 r, and s must have the same number of attributes.

 Attribute domains must be compatible.

 Duplicate tuples are automatically eliminated.

 ∏ author (Books) ∪ ∏ author (Articles)

 Output − Projects the names of the authors who

have either written a book or an article or both.


CUSTOMER_NAME ACCOUNT_NO
DEPOSITOR
Johnson A-101 RELATION

Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284

CUSTOMER_NAME LOAN_NO
BORROW
Jones L-17
RELATION
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)

Output:

CUSTOMER_NAME

Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
SET DIFFERENCE (−)
 The result of set difference operation is tuples,
which are present in one relation but are not in the
second relation.

 Notation − r − s

 Finds all the tuples that are present in r but not in s.

 ∏ author (Books) − ∏ author (Articles)

 Output − Provides the name of authors who have


written books but not articles.
CARTESIAN PRODUCT (Χ)
 Combines information of two different relations into
one.

 Notation − r Χ s

 Where r and s are relations and their output will be


defined as −

 r Χ s = { q t | q ∈ r and t ∈ s}

 σ author = ‘Navathe'(Books Χ Articles)


 Output − Yields a relation, which shows all the

books and articles written by tutorialspoint.


EXAMPLE R1 R2

A B C D E
1 a p 101 y
2 b q 102 z
3 c

R1 X R2

A B C D E
1 a p 101 y
1 a q 102 z
2 b p 101 y
2 b q 102 z
3 c p 101 y
3 c q 102 z
RENAME OPERATION (Ρ)
 The results of relational algebra are also relations but
without any name. The rename operation allows us to
rename the output relation. 'rename' operation is
denoted with small Greek letter rho ρ.

 Notation − ρ x (E)

 Where the result of expression E is saved with name


of x.

 Additional operations are −

 Set intersection
 Assignment

 Natural join
RELATIONAL CALCULUS
 In contrast to Relational Algebra, Relational Calculus
is a non-procedural query language, that is, it tells
what to do but never explains how to do it.

 Relational calculus exists in two forms −

 Tuple Relational Calculus (TRC)

 Filtering variable ranges over tuples


RELATIONAL CALCULUS
 Notation − {T | Condition}

 Returns all tuples T that satisfies a condition.

 For example −

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


 Output − Returns tuples with 'name' from Author

who has written article on 'database'.


RELATIONAL ALGEBRA
 TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).

 For example −

 { R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}


 Output − The above query will yield the same result as the previous one.

 Domain Relational Calculus (DRC)


 In DRC, the filtering variable uses the domain of attributes instead of entire tuple values (as done in TRC,
mentioned above).

 Notation −

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

 Where a1, a2 are attributes and P stands for formulae built by inner attributes.

 For example −

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


 Output − Yields Article, Page, and Subject from the relation TutorialsPoint, where subject is database.

 Just like TRC, DRC can also be written using existential and universal quantifiers. DRC also involves
relational operators.

 The expression power of Tuple Relation Calculus and Domain Relation Calculus is equivalent to Relational
Algebra.
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA
RELATIONAL ALGEBRA

You might also like