Upload
sk-munaf
View
219
Download
0
Embed Size (px)
Citation preview
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 1/36
RELATIONAL ALGEBRA AND CALCULUS
Relational Algebra Relational Calculus SQL
Embedded SQL QBE
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 2/36
Relational algebra is a collection ofoperations to manipulate or access
relations.
It is a procedural (or abstract) language
with operations performed on one or moreexisting relations to derive result(another)relations without changing the originalrelation(s).
Relational algebra consists of set ofrelational operators.
Each operator has one or more relations as
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 3/36
Eight operations were proposed by Dr.
Codd and these eight operators aredivided into the following two categories.
!) "et#theoretic operations
$) %ative relational operations.
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 4/36
"et#theoretic operations ma&e use of thefact that tables are essentially sets of rows.
'here are four set#theoretical operations
!) %I%
$) I%'ER"EC'I%
*) DI++ERE%CE
,) -RDC'
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 5/36
%ative Relational peration focuses on the
structure of the rows.
'here are four native relational operations
!) -REC'I%
$) "E/EC'I%
*) I%
,) DI0I"I%
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 6/36
!"ID LOCATION NO"O#"BINS $!ONE
12#34! 3356I $44 4$$#$*!,789
12#34$ :/:6'6 *44 4**#$789;!<
12#34* 3356I ,44 4$$#$*!789$
12#34, :/:6'6 $44 4**#$9;$<!4
12#347 DE/2I !44 4!!#$;<,879
RELATION% ARE!OUSE
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 7/36
SELECTION O$ERATOR is used to extract(select) entire rows (tuples) from somerelation ( a table).
e.g.
"E/EC' 12#ID %#+#5I%" -2%Efrom 16RE2"Ewhere /C6'I%=>3356I? into R!
orR! = "E/EC' 12#ID %#+#5I%" -2%E
from 16RE2"E where /C6'I% =>3356I?
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 8/36
!"ID NO"O#"BINS $!ONE
12#34! $44 4$$#$*!,789
12#34* ,44 4$$#$*!789$
RELATION% R&
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 9/36
e.g."E/EC' @
from 16RE2"Ewhere /C6'I%=>3356I? into R$
or
R$ = "E/EC' from 16RE2"Ewhere /C6'I% = >3356I?
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 10/36
!"ID LOCATION NO"O#"BINS $!ONE
12#34! 3356I $44 4$$#$*!,789
12#34* 3356I ,44 4$$#$*!789$
RELATION% R'
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 11/36
e.g."E/EC' @
from 16RE2"Ewhere /C6'I%=>3356I? and %#+#
5I%"=$44 into R*
orR* = "E/EC' from 16RE2"E
where /C6'I%=>3356I? and %#+#5I%"=$44
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 12/36
!"ID LOCATION NO"O#"BINS $!ONE
12#34! 3356I $44 4$$#$*!,789
RELATION% ARE!OUSE
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 13/36
$RO(ECTION O$ERATION is used to extractentire column (attributes) from some relation
( a table).
It constructs a new relation from some existingrelation by selecting only speciAed attributes of
the existing relation and eliminating duplicatetuples in the newly formed relation.
e.g.
-REC' 16RE2"E% 12#ID/C6'I%-2%EI%' R,
or
R, = -REC' 16RE2"E 0ER 12#ID
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 14/36
!"ID LOCATION $!ONE
12#34! 3356I 4$$#$*!,789
12#34$ :/:6'6 4**#$789;!<
12#34* 3356I 4$$#$*!789$
12#34, :/:6'6 4**#$9;$<!4
12#347 DE/2I 4!!#$;<,879
RELATION% R)
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 15/36
e.g.-REC' 16RE2"E
% /C6'I%I%' R7or
R7 = -REC' 16RE2"E 0ER
/C6'I%
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 16/36
LOCATION
3356I
:/:6'6
DE/2I
RELATION% R*
'he relation R7 eliminate one
duplicate tuple.
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 17/36
(OINING O+eration is a method ofcombining two or more relations in asingle relation.
It brings together rows (tuples) from
diBerent relations (or tables) based on thetruth of some speciAed condition.
It is the most useful of all the relational
algebra operations.
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 18/36
!"ID LOCATION NO"O#"BINS $!ONE
12#34! 3356I $44 4$$#$*!,789
12#34$ :/:6'6 *44 4**#$789;!<
12#34* 3356I ,44 4$$#$*!789$
12#34, :/:6'6 $44 4**#$9;$<!4
12#347 DE/2I !44 4!!#$;<,879
RELATION% ARE!OUSE
Considering the following two relations.
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 19/36
!"ID ITE,"NO QT-
12#34! $7 87
12#34$ $8 74
12#34* $9 *4
12#34, $7 77
12#347 $; ,7
RELATION% ITE,S
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 20/36
%ow we can oin the two relations
16RE2"E and I'E3" using thecommon attributes 12#ID.
I% 16RE2"E
1I'2 I'E3"% 12#IDInto R8
or
R8 = I% 16RE2"EI'E3" 0ER12#ID
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 21/36
!"ID LOCATION
NO"O#"BINS
$!ONE ITE,"NO QT-
12#34!
3356I $44 4$$#$*!,789
$7 87
12#34$
:/:6'6 *44 4**#$789;!<
$8 74
12#34*
3356I ,44 4$$#$*!789$
$9 *4
12#
34,
:/:6'6 $44 4**#
$9;$<!4
$7 77
12#347
DE/2I !44 4!!#$;<,879
$; ,7
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 22/36
UNION O$ERATION of two table (orrelations) is the every row (tuple) that
appears in either( or both) of the tables.
'he tables (or relations) on which it operatesmust contain the same number of columns(attributes).
If R and " have : and / tuples respectively
%I% is obtained by concatenating theminto one relation with maximum of (:/)tuples.
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 23/36
!"ID LOCATION $!ONE
12#34! 3356I 4$$#$*!,789
12#34, :/:6'6 4**#$9;$<!4
12#347 DE/2I 4!!#$;<,879
RELATION% R.
!"ID LOCATION $!ONE
12#34! 3356I 4$$#$*!,789
12#34$ :/:6'6 4**#$789;!<
12#34, :/:6'6 4**#$9;$<!4
RELATION% R/
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 24/36
%I% R;R<Into R!4
R
R!4 = %I% R;R<
!"ID LOCATION $!ONE
12#34! 3356I 4$$#$*!,789
12#34$ :/:6'6 4**#$789;!<
12#34, :/:6'6 4**#$9;$<!4
12#347 DE/2I 4!!#$;<,879
RELATION% R/
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 25/36
DI##ERENCE O+erator subtracts fromthe Arst named relation (or table) those
tuples (rows) that appear in the secondnamed relation (or table) and create anew relation.
DI++ERE%CE R;R<Into R!4
or
R!4 = DI++ERE%CE R;R<
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 26/36
!"ID LOCATION $!ONE
12#34! 3356I 4$$#$*!,789
12#34, :/:6'6 4**#$9;$<!4
12#347 DE/2I 4!!#$;<,879
RELATION% R.
!"ID LOCATION $!ONE
12#34! 3356I 4$$#$*!,789
12#34$ :/:6'6 4**#$789;!<
12#34, :/:6'6 4**#$9;$<!4
RELATION% R/
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 27/36
!"ID LOCATION $!ONE
12#347 DE/2I 4!!#$;<,879
RELATION% R&0
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 28/36
INTERSECTION O+erator selects only
those rows (tuples) that appear in both ofthe named relations (tables) .
I%'ER"EC'I% R;R<
Into R!!
or
R!! = I%'ER"EC'I% R; R<
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 29/36
!"ID LOCATION $!ONE
12#34! 3356I 4$$#$*!,789
12#34, :/:6'6 4**#$9;$<!4
12#347 DE/2I 4!!#$;<,879
RELATION% R.
!"ID LOCATION $!ONE
12#34! 3356I 4$$#$*!,789
12#34$ :/:6'6 4**#$789;!<
12#34, :/:6'6 4**#$9;$<!4
RELATION% R/
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 30/36
!"ID LOCATION $!ONE
12#34! 3356I 4$$#$*!,789
12#34, :/:6'6 4**#$9;$<!4
RELATION% R&&
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 31/36
CARTESIAN $RODUCT perator (also
called Cross#-roduct) It ta&es each tuple(row) from the Arst named table (relation)and concatenates it with every row (tuple)of the second table (relation).
C6R'E"I6% -RDC' R!$ R!*Into R!,
or
R!, = C6R'E"I6% -RDC' R!$R!*
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 32/36
!"ID LOCATION
12#34! 3356I
12#34, :/:6'6
RELATION% R&'
$ART"NA,E QT-
DR3-ER !4
DRI// ,4
1E/DI% $7
RELATION% R&1
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 33/36
!"ID LOCATION $ART"NA,E QT-
12#34! 3356I DR3-ER !4
12#34! 3356I DRI// ,4
12#34! 3356I 1E/DI% $7
12#34, :/:6'6 DR3-ER !4
12#34, :/:6'6 DRI// ,4
12#34, :/:6'6 1E/DI% $7
RELATION% R&)
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 34/36
DI2ISION O+eration is useful for a
particular type of Fuery that occurs FuitefreFuently in database applications.
Considering the two relations R!7 R!8
DI0I"I% R!7 R!8I%' R!9
RR!9 = DI0I"I% R!7 R!8
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 35/36
E,$LO-EE LOCATION
:/G "G"'E3" 63"2ED-R
'RI%I'G /'D. :/:6'6
:/G "G"'E3" 3356I
C%CE-' "26-ER 63"2ED-R
'RI%I'G /'D. DE/2I
C%CE-' "26-ER DE/2I
C%CE-' "26-ER :/:6'6
RELATION% R&*
LOCATION
63"2ED-R
DE/2I
RELATION%R&3
8/9/2019 8-dbms
http://slidepdf.com/reader/full/8-dbms 36/36
E,$LO-ERC%CE-' "26-ER
RELATION%R&4