0% found this document useful (0 votes)
48 views32 pages

Relational Algebra Operations in RDM: Tools Boot Camp

This document provides an overview of relational algebra operations used in relational database management systems (RDMs). It defines common relational algebra operators like selection, projection, join, union, difference, and intersection. It also discusses more advanced topics like outer joins, renaming, and using assignment operators to write queries as sequential programs. Examples are provided to demonstrate how different relational algebra expressions can be used to solve database queries.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
48 views32 pages

Relational Algebra Operations in RDM: Tools Boot Camp

This document provides an overview of relational algebra operations used in relational database management systems (RDMs). It defines common relational algebra operators like selection, projection, join, union, difference, and intersection. It also discusses more advanced topics like outer joins, renaming, and using assignment operators to write queries as sequential programs. Examples are provided to demonstrate how different relational algebra expressions can be used to solve database queries.
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 32

Tools Boot camp

Relational Algebra Operations in RDM


Source : http://www.ics.uci.edu/~ics184/#lectures
SQL -- Historical Perspective
Relational Algebra (invented by Ted Codd in 1973)

SQL Language

SQL Server SQL (Transact-SQL) | Oracle 10G SQL (PL-SQL) | DB2 SQL
ACCESS (SQL)
For SQL Server 2005
SSIS Tool, Analysis Services Tool, Reporting Services Tool, etc.
OLTP
OLAP
Purely, from Tools perspective
Database Programming Language (DBPL) Programming Languages (Java,
VB, C, C++, )
Outline
3
Relational Algebra (Retrieval)
A few set-based operators to manipulate relations:
Union, Intersection, Difference:
Usual set operators
Relations must have the same schema
Selection: choose rows from a relation.
Projection: choose columns from a relation.
Cartesian Product and Join: construct a new relation from
several relations
Renaming: rename a relation and its attributes
Combining basic operators to form expressions
Update operations (insert, delete and modify)

Remember: creation is done in DDL

Union , Intersection , Difference -
4
Set operators. Relations must have the same schema.
R(name, dept)
Name Dept
Jack Physics
Tom ICS

S(name, dept)
Name Dept
Jack Physics
Mary Math

Name Dept
Jack Physics
Tom ICS
Mary Math

RS
Name Dept
Jack Physics

R S
Name Dept
Tom ICS

R-S
Selection o
5
o
c
(R): return tuples in R that satisfy condition C.
Emp (name, dept, salary)
Name Dept Salary
Jane ICS 30K
Jack Physics 30K
Tom ICS 75K
Joe Math 40K
Jack Math 50K

o
salary>35K
(Emp)
Name Dept Salary
Tom ICS 75K
Joe Math 40K
Jack Math 50K

o
dept=ics and salary<40K
(Emp)
Name Dept Salary
Jane ICS 30K

Projection H
6
H
A1,,Ak
(R): pick columns of attributes A1,,Ak of R.
Emp (name, dept, salary)
H
name,dept
(Emp)
Name Dept Salary
Jane ICS 30K
Jack Physics 30K
Tom ICS 75K
Joe Math 40K
Jack Math 50K

Name Dept
Jane ICS
Jack Physics
Tom ICS
Joe Math
Jack Math

H
name
(Emp)
Name
Jane
Jack
Tom
Joe

Duplicates (Jack) eliminated.
Cartesian Product:
7
R S: pair each tuple r in R with each tuple s in S.
Emp (name, dept)
Name Dept
Jack Physics
Tom ICS

Contact(name, addr)
Name Addr
Jack Irvine
Tom LA
Mary Riverside

Emp Contact
E.name Dept C.Name Addr
Jack Physics Jack Irvine
Jack Physics Tom LA
Jack Physics Mary Riverside
Tom ICS Jack Irvine
Tom ICS Tom LA
Tom ICS Mary Riverside

Join
8
R S = o
c
(R S)


C
Join condition C is of the form:
<cond_1> AND <cond_2> AND AND <cond_k>
Each cond_i is of the form A op B, where:
A is an attribute of R, B is an attribute of S
op is a comparison operator: =, <, >, >, s, or =.
Different types:
Theta-join
Equi-join
Natural join
Theta-Join
Result
R.A R.B S.C S.D
3 4 2 7
5 7 2 7

9
R S
R.A>S.C
R(A,B) S(C,D)
R.A R.B S.C S.D
3 4 2 7
3 4 6 8
5 7 2 7
5 7 6 8

C D
2 7
6 8

A B
3 4
5 7

R S
Theta-Join
10
Result
R(A,B) S(C,D)
C D
2 7
6 8

A B
3 4
5 7

R S
R.A>S.C, R.B = S.D
R.A R.B S.C S.D
3 4 2 7

R.A R.B S.C S.D
3 4 2 7
3 4 6 8
5 7 2 7
5 7 6 8

R S
Equi-Join
11
Special kind of theta-join: C only uses the equality operator.
R S
R.B=S.D
R(A,B) S(C,D)
C D
2 7
6 8

A B
3 4
5 7

R.A R.B S.C S.D
5 7 2 7

R.A R.B S.C S.D
3 4 2 7
3 4 6 8
5 7 2 7
5 7 6 8

R S
Result
Natural-Join
12
Relations R and S. Let L be the union of their attributes.
Let A1,,Ak be their common attributes.
R S = H
L
(R S)
R.A1=S.A1,,R.Ak=S.Ak
Natural-Join
13
Emp (name, dept)
Name Dept
Jack Physics
Tom ICS

Contact(name, addr)
Name Addr
Jack Irvine
Tom LA
Mary Riverside

Name Dept Addr
Jack Physics Irvine
Tom ICS LA

Emp Contact: all employee names, dept, and addresses.
Emp.name Emp.Dept Contact.name Contact.addr
Jack Physics Jack Irvine
Jack Physics Tom LA
Jack Physics Mary Riverside
Tom ICS Jack Irvine
Tom ICS Tom LA
Tom ICS Mary Riverside

Emp Contact
Result
Same as Equi-Join,
except that one of the
duplicate columns is
eliminated
Renaming
14
Motivation: disambiguate attribute names. E.g., in R R, how to
differentiate the attributes from the two instances?

S(B1,,Bn)
(R)
A relation identical to R, with new attributes B1,,Bn.
Emp(name, dept)
Name Dept
Jack Physics
Tom ICS

emp1(name1,dept1)
(Emp)
Name1 Dept1
Jack Physics
Tom ICS

Emp1(name1, dept1)
Renaming (cont)
List employees who work in the same department as Tom.
Name Dept Name1 Dept1
Jack Physics Jack Physics
Jack Physics Tom ICS
Jack Physics Mary ICS
Tom ICS Jack Physics
Tom ICS Tom ICS
Tom ICS Mary ICS
Mary ICS Jack Physics
Mary ICS Tom ICS
Mary ICS Mary ICS

Name Dept
Jack Physics
Tom ICS
Mary ICS

15
Emp (name, dept)
Name Dept
Jack Physics
Tom ICS
Mary ICS

H
emp1.name1
(
emp1(name1,dept1)
(emp) o
name=tom
(emp))
emp1.dept1=emp.dept
Name1
Tom
Mary

Emp1(name1, dept1)
Result
Emp Emp1
Outer Joins
Motivation: join can lose information
E.g.: natural join of R and S loses info about Tom and Mary, since they
do not join with other tuples.
Called dangling tuples.
R
Name Dept
Jack Physics
Tom ICS

S
Name Addr
Jack Irvine
Mike LA
Mary Riverside

Outer join: natural join, but use NULL values to fill in dangling
tuples. Remember natural join is similar to equi join
Three types: left, right, or full
Left Outer Join
R.name R.Dept S.name S.addr
Jack Physics Jack Irvine
Jack Physics Mike LA
Jack Physics Mary Riverside
Tom ICS Jack Irvine
Tom ICS Mike LA
Tom ICS Mary Riverside

R
Name Dept
Jack Physics
Tom ICS

S
Name Addr
Jack Irvine
Mike LA
Mary Riverside

Left outer join
R S
Name Dept Addr
Jack Physics Irvine
Tom ICS NULL

Pad null value for left dangling tuples.
R S
LEFT OUTER JOIN -- It is the
relation from which we wish
all rows returned, regardless
of whether there is a matching
address in the S relation.
List all names, depts and addresses
for all names listed in R
Assumes
equality
Right Outer Join
Name Addr
Jack Irvine
Mike LA
Mary Riverside

R
Name Dept
Jack Physics
Tom ICS

S
Right outer join
R S
Name Dept Addr
Jack Physics Irvine
Mike NULL LA
Mary NULL Riverside

Pad null value for right dangling tuples.
R.name R.Dept S.name S.addr
Jack Physics Jack Irvine
Jack Physics Mike LA
Jack Physics Mary Riverside
Tom ICS Jack Irvine
Tom ICS Mike LA
Tom ICS Mary Riverside

R S
RIGHT OUTER JOIN -- It is
the relation from which we
wish all rows returned,
regardless of whether there
is a matching dept in the R
relation.
List all names, depts and addresses
for all names listed in S
Assumes
equality
Full Outer Join
Name Addr
Jack Irvine
Mike LA
Mary Riverside

R
Name Dept
Jack Physics
Tom ICS

S
Full outer join
R S
Name Dept Addr
Jack Physics Irvine
Tom ICS NULL
Mike NULL LA
Mary NULL Riverside

Pad null values for both left and right dangling tuples.
R.name R.Dept S.name S.addr
Jack Physics Jack Irvine
Jack Physics Mike LA
Jack Physics Mary Riverside
Tom ICS Jack Irvine
Tom ICS Mike LA
Tom ICS Mary Riverside

R S
List all names, depts and addresses
for all names listed in R and S
Combining Different Operations
20
Construct general expressions using basic operations.
Schema of each operation:
, , -: same as the schema of the two relations
Selection o : same as the relations schema
Projection H: attributes in the projection
Cartesian product : attributes in two relations, use prefix to
avoid confusion
Theta Join : same as
Natural Join : union of relations attributes, merge
common attributes
Renaming: new renamed attributes
C
Equivalent Expressions
21
Expressions might be equivalent.
R S = R (R S)


How about the following?
(R S) T = R (S T)?
(R S) T = R (S T)?
H
A
(R S) = H
A
(R) H
A
(S)?
H
A
(R S) = H
A
(R) H
A
(S)?
R S = H
L
(R S)
R.A1=S.A1,,R.Ak=S.Ak
Example 1
customer(ssn, name, city)
account(custssn, balance)
List account balances of Tom.
[
= =

balance
tom name ssn custssn
customer account ))) (
( ( o o
account
customer

o
ssn custssn=
H
balance
o
name=tom
Tree representation
Example 1(cont)
customer(ssn, name, city)
account(custssn, balance)
List account balances of Tom.
account
customer
H
balance
o
name=tom
ssn=custssn
Assignment Operator
24
Motivation: expressions can be complicated
Introduce names for intermediate relations, using the assignment operator
:=
Then a query can be written as a sequential program consisting of a
series of assignments
[
= =

balance
tom name ssn custssn
customer account ))) (
( ( o o
R1(ssn,name,city) := s
name=tom
(customer)
R2(ssn,name,city,custssn,balance):= s
custssn=ssn
(accountR1)
Answer(balance) := H
balance
(R2)
This sequential
program is SQL is
called script
Example 2
Find names of customers in Irvine or having a balance > 50K.
account
customer
o
city=irvine
custssn=ssn
o
balance>50K customer
H
name
H
name

customer(ssn, name, city)
account(custssn, balance)
Example 3
List the highest balance of all the customers.
account
H
acct1.balance
account

acct1
acct1.balance < account.balance
H
balance
account

account(custssn, balance)
Custssn balance
111 20K
222 15K
333 10K

Example 3
List the highest balance of all the customers.
Custssn balance
111 20K
222 15K
333 10K

Custssn balance
111 20K
222 15K
333 10K

account
H
acct1.balance
account

acct1
acct1.balance < account.balance
H
balance
account

account(custssn, balance)
Custssn balance
111 20K
222 15K
333 10K

acct1
account
Acct1.
Custssn
Acct1.balance Account.
Custssn
Account.
balance
111 20K 111 20K
111 20K 222 15K
111 20K 333 10K
222 15K 111 20K
222 15K 222 15K
222 15K 333 10K
333 10K 111 20K
333 10K 222 15K
333 10K 333 10K

Acct1.
balance
15K
10K

Account.
balance
20K
15K
10K

20K

Example 3 (cont)
List the highest balance of all the customers.
Is the following expression correct?
account
H
account.balance
account

acct1
acct1.balance<account.balance
account(custssn, balance)
Custssn balance
111 20K
222 15K
333 10K

Example 3 (cont)
How about the lowest balance?
How about the highest balance of customers in Irvine?
Example 4
List the cities and names of the customers who have the highest balance
of all customers.
account
H
acct1.balance
account

acct1
acct1.balance<account.balance
H
balance
account

custssn=ssn
customer
H
name,city
balance=acct2.balance
account

acct2
highest balance
the customers
with this balance
Example 4: another expression?
List the cities and names of the customers who have the highest balance
of all customers.
account
H
acct1.custssn
account

acct1
acct1.balance<account.balance
custssn=ssn
customer
H
name,city
H
custssn
account

customer(ssn,name,city)
Ssn Name City
222 Tom irvine

account(custssno,balance)
Custssn balance
222 20K
222 50K
333 50K

Tom has two accounts. One
of them is not the highest!
WRONG!
Limitation of Relational Algebra

Some queries cannot be represented
Example, recursive queries:
Table R(Parent,Child)
How to find all the ancestors of Tom?
Impossible to write this query in relational algebra.
More expressive languages needed:
E.g., Datalog

You might also like