Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

RELATIONAL ALGEBRA AND CALCULUS

Relational Algebra
Relational Calculus
SQL
Embedded SQL
QBE

Relational algebra is a collection of


operations to manipulate or access
relations.
It is a procedural (or abstract) language
with operations performed on one or more
existing relations to derive result(another)
relations without changing the original
relation(s).
Relational algebra consists
relational operators.

of

set

of

Each operator has one or more relations as

Eight operations were proposed by Dr.


Codd, and these eight operators are
divided into the following two categories.
1) Set-theoretic operations
2) Native relational operations.

Set-theoretic operations make use of the


fact that tables are essentially sets of rows.
There are four set-theoretical operations
1) UNION
2) INTERSECTION
3) DIFFERENCE
4) PRODUCT

Native Relational Operation focuses on the


structure of the rows.
There are four native relational operations
1) PROJECTION
2) SELECTION
3) JOIN
4) DIVISION

RELATION: WAREHOUSE
WH-ID

LOCATION

NO-OF-BINS

PHONE

WH-M01

MUMBAI

200

022-2314567

WH-M02

KOLKATA

300

033-2567819

WH-M03

MUMBAI

400

022-2315672

WH-M04

KOLKATA

200

033-2782910

WH-M05

DELHI

100

011-2894657

SELECTION OPERATOR is used to extract


(select) entire rows (tuples) from some
relation ( a table).
e.g.
SELECT WH-ID, NO-OF-BINS, PHONE
from
WAREHOUSE
where
LOCATION=MUMBAI
into
R1
or
R1 = SELECT WH-ID, NO-OF-BINS, PHONE
from
WAREHOUSE where LOCATION =
MUMBAI

RELATION: R1
WH-ID

NO-OF-BINS

PHONE

WH-M01

200

022-2314567

WH-M03

400

022-2315672

e.g.
SELECT *
from
WAREHOUSE
where
LOCATION=MUMBAI
into
R2
or
R2 = SELECT from WAREHOUSE
where LOCATION = MUMBAI

RELATION: R2
WH-ID

LOCATION

NO-OF-BINS

PHONE

WH-M01

MUMBAI

200

022-2314567

WH-M03

MUMBAI

400

022-2315672

e.g.
SELECT *
from
WAREHOUSE
where
LOCATION=MUMBAI and NO-OFBINS=200
into
R3
or
R3 = SELECT from WAREHOUSE
where LOCATION=MUMBAI and NO-OFBINS=200

RELATION: WAREHOUSE
WH-ID
WH-M01

LOCATION
MUMBAI

NO-OF-BINS
200

PHONE
022-2314567

PROJECTION OPERATION is used to extract


entire column (attributes) from some relation
( a table).
It constructs a new relation from some existing
relation by selecting only specified attributes of
the existing relation and eliminating duplicate
tuples in the newly formed relation.
e.g.
PROJECT
WAREHOUSE
ON
WH-ID,LOCATION,PHONE
INTO
R4
or
R4 = PROJECT WAREHOUSE OVER WH-ID,

RELATION: R4
WH-ID

LOCATION

PHONE

WH-M01

MUMBAI

022-2314567

WH-M02

KOLKATA

033-2567819

WH-M03

MUMBAI

022-2315672

WH-M04

KOLKATA

033-2782910

WH-M05

DELHI

011-2894657

e.g.
PROJECT
WAREHOUSE
ON
LOCATION
INTO
R5
or
R5
=
PROJECT WAREHOUSE OVER
LOCATION

RELATIO
N: R5
LOCATION

MUMBAI
KOLKATA
DELHI

The relation R5 eliminate one


duplicate tuple.

JOINING Operation is a method of


combining two or more relations in a
single relation.
It brings together rows (tuples) from
different relations (or tables) based on the
truth of some specified condition.
It is the most useful of all the relational
algebra operations.

Considering the following two relations.


RELATION: WAREHOUSE
WH-ID

LOCATION

NO-OF-BINS

PHONE

WH-M01

MUMBAI

200

022-2314567

WH-M02

KOLKATA

300

033-2567819

WH-M03

MUMBAI

400

022-2315672

WH-M04

KOLKATA

200

033-2782910

WH-M05

DELHI

100

011-2894657

RELATION: ITEMS
WH-ID

ITEM-NO

QTY

WH-M01

25

65

WH-M02

26

50

WH-M03

27

30

WH-M04

25

55

WH-M05

28

45

Now, we can join the two relations,


WAREHOUSE and ITEMS, using the
common attributes WH-ID.
JOIN
WITH
ON
Into

WAREHOUSE
ITEMS
WH-ID
R6

or
R6
=
WH-ID

JOIN WAREHOUSE,ITEMS OVER

WH-ID

LOCATIO
N

NO-OFBINS

PHONE

ITEM-NO

QTY

WHM01

MUMBAI

200

0222314567

25

65

WHM02

KOLKATA

300

0332567819

26

50

WHM03

MUMBAI

400

0222315672

27

30

WHM04

KOLKATA

200

0332782910

25

55

WHM05

DELHI

100

0112894657

28

45

UNION OPERATION of two table (or


relations) is the every row (tuple) that
appears in either( or both) of the tables.
The tables (or relations) on which it operates
must contain the same number of columns
(attributes).
If R and S have K and L tuples, respectively,
UNION is obtained by concatenating them
into one relation with maximum of (K+L)
tuples.

RELATION: R8
WH-ID

LOCATION

PHONE

WH-M01

MUMBAI

022-2314567

WH-M04

KOLKATA

033-2782910

WH-M05

DELHI

011-2894657

RELATION: R9
WH-ID

LOCATION

PHONE

WH-M01

MUMBAI

022-2314567

WH-M02

KOLKATA

033-2567819

WH-M04

KOLKATA

033-2782910

UNION
Into

R8,R9
R10

OR
R10

UNION R8,R9

RELATION: R9
WH-ID

LOCATION

PHONE

WH-M01

MUMBAI

022-2314567

WH-M02

KOLKATA

033-2567819

WH-M04

KOLKATA

033-2782910

WH-M05

DELHI

011-2894657

DIFFERENCE Operator subtracts from


the first named relation (or table) those
tuples (rows) that appear in the second
named relation (or table) and create a
new relation.
DIFFERENCE
R8,R9
Into
R10
or
R10

DIFFERENCE R8,R9

RELATION: R8
WH-ID

LOCATION

PHONE

WH-M01

MUMBAI

022-2314567

WH-M04

KOLKATA

033-2782910

WH-M05

DELHI

011-2894657

RELATION: R9
WH-ID

LOCATION

PHONE

WH-M01

MUMBAI

022-2314567

WH-M02

KOLKATA

033-2567819

WH-M04

KOLKATA

033-2782910

RELATION: R10
WH-ID
WH-M05

LOCATION
DELHI

PHONE
011-2894657

INTERSECTION Operator selects only


those rows (tuples) that appear in both of
the named relations (tables) .
INTERSECTION
R8,R9
Into
R11
or
R11

INTERSECTION R8, R9

RELATION: R8
WH-ID

LOCATION

PHONE

WH-M01

MUMBAI

022-2314567

WH-M04

KOLKATA

033-2782910

WH-M05

DELHI

011-2894657

RELATION: R9
WH-ID

LOCATION

PHONE

WH-M01

MUMBAI

022-2314567

WH-M02

KOLKATA

033-2567819

WH-M04

KOLKATA

033-2782910

RELATION: R11
WH-ID

LOCATION

PHONE

WH-M01

MUMBAI

022-2314567

WH-M04

KOLKATA

033-2782910

CARTESIAN PRODUCT Operator (also


called Cross-Product), It takes each tuple
(row) from the first named table (relation)
and concatenates it with every row (tuple)
of the second table (relation).
CARTESIAN PRODUCT
Into
R14

R12, R13

or
R14

CARTESIAN PRODUCT R12,R13

RELATION: R12
WH-ID

LOCATION

WH-M01

MUMBAI

WH-M04

KOLKATA

RELATION: R13
PART-NAME

QTY

DRUMPER

10

DRILL

40

WELDING

25

RELATION: R14
WH-ID

LOCATION

PART-NAME

QTY

WH-M01

MUMBAI

DRUMPER

10

WH-M01

MUMBAI

DRILL

40

WH-M01

MUMBAI

WELDING

25

WH-M04

KOLKATA

DRUMPER

10

WH-M04

KOLKATA

DRILL

40

WH-M04

KOLKATA

WELDING

25

DIVISION Operation is useful for a


particular type of query that occurs quite
frequently in database applications.
Considering the two relations R15, R16,
DIVISION
INTO

R15, R16
R17

OR
R17 = DIVISION R15, R16

RELATION: R15
EMPLOYEE

LOCATION

KLY SYSTEMS

JAMSHEDPUR

TRINITY LTD.,

KOLKATA

KLY SYSTEMS

MUMBAI

CONCEPT SHAPER

JAMSHEDPUR

TRINITY LTD.,

DELHI

CONCEPT SHAPER

DELHI

CONCEPT SHAPER

KOLKATA

RELATION:
R16
LOCATION
JAMSHEDPUR
DELHI

RELATION:
R17
EMPLOYER

CONCEPT SHAPER

You might also like