relational algebra
TRANSCRIPT
Relational Algebra
Operations and Set operations
Relational Algebra
By relieving the brain of all unnecessary work, a good notation sets it free to
concentrate on more advanced problems, and, in effect,
increases the mental power of the race.
-- Alfred North Whitehead (1861 - 1947)
What is Relational Algebra
• It defines basic set of operations for manipulating relational data.
• It enables users for basic retrieval operations
• Result of any retrieval always gives another relation to manipulate
• It specifies relations to be performed on relations to derive result relations.
Relational algebra : Features• It is a procedural query language.
• It consists of a collection of operators, such as join, union, and intersect,that take relations as their operands and return relations as their result.
• It is the formal description of how a relational database operates.
• Relational algebra is the mathematics which underpin SQL operations.
• Operators in relational algebra are not necessarily the same as SQL operators, even if they have the same name.
The BIG picture
Its place in the big picture:
Declarativequery
language
Declarativequery
languageAlgebraAlgebra ImplementationImplementation
SQL,relational calculus
Relational algebra
RELATIONAL ALGEBRA OPERATORS
Specific Operators
Set Operators
Set Operators: Introduction
Relational algebra received little attention until the publication of E.F. Codd’s relational model of data in 1970. Codd proposed such an algebra as a basis for database query languages.
E.F. Codd’s Operators
Set operators from mathematical set theory
Union
Intersection
Difference
Cartesian Product
Set Operators
Relation is a set of tuples, so set operations should apply: , , (set difference)
Result of combining two relations with a set operator is a relation
All its elements must be tuples having same structure
Hence, scope of set operations limited to union union compatible relations.compatible relations.
Union Compatible Relations
Two relations are union compatibleunion compatible ifBoth have same degree (number of columns)Names of attributes are the same in bothAttributes with the same name in both relations have the same domain (i.e.the data type of the attributes should be same like character,integer etc. )
Union compatible relations can be combined using unionunion, intersectionintersection, and setset difference.difference.
Union
• Returns a relation consisting of all tuples appearing in either or both of two specified relations.
• Duplicates are eliminated• Both relations must be union compatible• It is denoted by symbol .
Example
S# SNAME STATUS CITY
S1 Smith 20 London
S4 Clark 20 London
S# SNAME STATUS CITY
S1 Smith 20 London
S4 Clark 20 London
S2 Jones 20 Paris
A U B
S# SNAME STATUS CITY
S1 Smith 20 London
S2 Jones 20 Paris
A B
Intersection
• Returns a relation consisting of all tuples appearing in both of two specified relations.
• It is denoted by symbol .• Both relations must be union compatible.
Duplicates are eliminated
Example
S # SNAME STATUS CITY
S1 Smith 20 London
S4 Clark 20 London
A
S # SNAME STATUS CITY
S1 Smith 20 London
S4 Jones 20 Paris
B
A INTERSECTION B (A B)S # SNAME STATUS CITY
S1 Smith 20 London
Difference
Returns a relation consisting of all tuples appearing in the first and not the second of two specified relations.It is denoted by symbol .
Example
S # SNAME STATUS CITY
S1 Smith 20 London
S4 Jones 20 Paris
A
B
S # SNAME STATUS CITY
S1 Smith 20 London
S4 Clark 20 London
A DIFFERENCE B (A - B)S # SNAME STATUS CITY
S4 Clark 20 London
Cartesian ProductCross product or cross join
Result relation will have degree= sum of degrees of both source relations
Result relation will have tuples= product of tuples of both source relations
Returns a relation consisting of all possible tuples that are a combination of two tuples, one from each of two specified relations.It is denoted by the symbol X.
RR and SS need not be union compatible
Example
a x a x
b * y = a y
c b x
(A) (B) b y
c x
c y
(A X B)
Cross-Product : detailed example• Each row of R is paired with each row of S.• Result schema has one field per field of S and
R, with field names `inherited’ if possible.
E 5F 4D 3B 2A 1
A 1C 2D 3E 4
A 1 A 1C 2D 3E 4
B 2 A 1C 2D 3E 4A 1C 2D 3E 4
F 4 A 1C 2D 3E 4
E 5 A 1C 2D 3E 4
D 3
A 1A 1A 1
B 2B 2B 2
D 3D 3D 3
F 4F 4F 4
E 5E 5E 5
R
S
R CROSS S
• Union and Intersection are cumulative and associative operations therefore A U B = B U A
A B = B A
A U ( B U C ) = ( A U B ) U C
A ( B C) = ( A B ) C
– But difference operation is not thereforeA – B ≠ B - A
Operators Developed Specifically For Relational Databases
Project
Select
Join
Project
Retains only wanted columns from relation.
Projection operator has to eliminate duplicates
Denoted by symbol π.
Produces table containing subset of columns of argument table
attribute list(relation)
Example
sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0
S2
sname rating
S,
( )2=
SelectSelects rows that satisfy specific conditions.No duplicates in result Result relation can be the input for another relational algebra operation (Operator composition.)Denoted by symbol σ Produce table containing subset of rows of argument table satisfying condition
condition (relation)Operators: <, , , >, =,
For multiple conditions use : and (^), or (V)
Example
sid sname rating age 28 yuppy 9 35.0 58 rusty 10 35.0
sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0
S2
rating
S82( )
Rename
• Used as alias name for result relation• Often used to assign simple name to the result
relation or attributes when it is to be used again in some other expression.
• Symbol used is ←• example:
(B400)←(bookid,bname)(price>400)(books)
• Now B400 can be used again• Example : (bookid)(B400)
Join
Joins are compound operators involving cross product, selection, and (sometimes) projection.
Most common type of join is a “natural join” R S conceptually is:
Compute R X S
Project all unique attributes and one copy of each of the common ones.
Example
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
sid bid day
22 101 10/10/96 58 103 11/12/96
R1S1
R1 S1 =sid sname rating age bid day
22 dustin 7 45.0 101 10/ 10/ 96 58 rusty 10 35.0 103 11/ 12/ 96
Division
• Not supported as a primitive operator, but useful for certain
• Let A have 2 fields, x and y; B have only field y:– A/B = – i.e., A/B contains all x tuples such that for every y
tuple in B, there is an xy tuple in A.
Examples of Division A/B
sno pnos1 p1s1 p2s1 p3s1 p4s2 p1s2 p2s3 p2s4 p2s4 p4
pnop2
pnop2p4
pnop1p2p4
snos1s2s3s4
snos1s4
snos1
A
B1B2
B3
A/B1 A/B2 A/B3
Practical Examples
Example Instances
• “Sailors” and “Reserves” relations for our examples.
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.558 rusty 10 35.0
sid sname rating age28 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0
sid bid day
22 101 10/10/9658 103 11/12/96
R1
S1
S2
Find names of sailors who’ve reserved boat #103
• Solution 1: sname bidserves Sailors(( Re ) )
103
Solution 2: ( , Re )Temp servesbid
1103
( , )Temp Temp Sailors2 1
sname Temp( )2
Solution 3: sname bidserves Sailors( (Re ))
103
Find names of sailors who’ve reserved a red boat
• Information about boat color only available in Boats; so need an extra join:
sname color redBoats serves Sailors((
' ') Re )
A more efficient solution:
sname sid bid color redBoats s Sailors( ((
' ') Re ) )
A query optimizer can find this given the first solution!
Find sailors who’ve reserved a red or a green boat
• Can identify all red or green boats, then find sailors who’ve reserved one of these boats: ( , (
' ' ' '))Tempboats
color red color greenBoats
sname Tempboats serves Sailors( Re )
Can also define Tempboats using union! (How?)
What happens if is replaced by in this query?
Find sailors who’ve reserved a red and a green boat
• Previous approach won’t work! Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (note that sid is a key for Sailors): ( , ((
' ') Re ))Tempred
sid color redBoats serves
sname Tempred Tempgreen Sailors(( ) )
( , ((' '
) Re ))Tempgreensid color green
Boats serves
Find the names of sailors who’ve reserved all boats
• Uses division; schemas of the input relations to / must be carefully chosen:
( , (,
Re ) / ( ))Tempsidssid bid
servesbid
Boats
sname Tempsids Sailors( )
To find sailors who’ve reserved all ‘Interlake’ boats:
/ (' '
) bid bname Interlake
Boats.....