Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

CS271 Database Systems

Relational Algebra

Relational Model vs. Algebra


Relational data model defines database structure and

constraints
Relational algebra is abstract specification of

operations (e.g. retrieve certain tuples)


Operations

take one or two relations as input and


produce a new relation as a result.

The

new relation, can be further manipulated using


operations of the same algebra

Relational Algebra Expression: Sequence of

operations
SQL: Implementation of relational algebra operations

Relational Algebra
Procedural language: As a meta language it

forms underlying basis of SQL query language.


Six basic operators
Unary
select:

, project: , rename:

Binary
union:

, set difference:

Cross

product: x

Select Operation
Notation: p(r)
p is called the selection predicate/condition
Defined as: p(r) = {t | t r and p(t)}
Where

p is a formula in propositional
calculus of the form:
!<attribute>

op [<attribute> or <constant>]
!where op is one of: =, , >, . <.

Example:

(account)
What would be included in the result set?
branch_name=G10

Select Operation Example


Relation r

D > 5 (r)

12 3

23 10

A=B (r)

12 3

23 10

23 10

Selection Predicate
Selection predicate is a formula in propositional

calculus consisting of terms connected by : (and),


(or), (not)
Each

term is one of:


!<attribute> op [ <attribute> or <constant> ]
!where op is one of: =, , >, . <.

Relation r
A

Tuples where A and B are


same; and D is greater than 5

A=B ^ D > 5 (r)


A

12 3

23 10

23 10

More Examples
loan (loan_number, branch_name, amount)
Find all loans of over $1200

amount > 1200 (loan)


Find all loans of over $1200 but less than

$10,000

amount > 1200 ^ amount <10000 (loan)


10000 <amount > 1200 (loan)
amount < 10000 (amount > 1200 (loan))
7

Project Operation
Notation: A1, A2, Ak ( r )
where

A1, A2 are attribute names and r is a relation.

The result is defined as a modified form of relation r with

k columns obtained by omitting the columns that are not


listed.
Duplicate rows removed from result, since relations are

sets.
Example: eliminate the branch_name attribute of account
account_number, balance

(account)

Project Operation Example


Relation r:

A,C (r)

10

20

30

40

Another Example
loan (loan_number, branch_name, amount)
Find the loan number for each loan of an

amount greater than $1200

loan_number ( amount > 1200 (loan) )


amount > 1200 (loan_number (loan))

10

Union Operation
Notation: r s
Defined as: r s = {t | t r or t s}
For r s to be valid.
r,

s must have the same arity (same number of


attributes)

The

attribute domains must be compatible

2nd column of r deals with the


same type of values as does the 2nd column
of s

!Example:

11

Union Operation Example


Relations r, s:

r s:

12

Example
depositor (customer_name, account_number)
borrower (customer_name, loan_number)
Find the names of all customers
Customers

have a loan or an account or both

customer_name ( borrower depositor )


customer_name (borrower) customer_name (depositor)
13

Set Difference Operation


Notation r s
Defined as: r s = {t | t r and t s}
Set differences must be taken between

compatible relations.
r

and s must have the same arity

attribute

domains of r and s must be


compatible

14

Set Difference Operation Example


Relations r, s:

r s:

15

Rename Operation
Allows us to name, and therefore refer to, the results of

relational-algebra expressions.
Allows us to refer to a relation by more than one name.
Rename relation:
returns

x (E)

the expression E under the name X

Rename relation and attributes:

x ( A ,A
1

2 ,..., An

(E )

returns

the result of expression E under the name X


and with the attributes renamed to A1 , A2 , ., An

16

Cartesian-Product Background
Compute cross product r x s
r

= {Ali, Umar}

= {1010, 1020}

Now again compute a cross product r x s


r

= {(Ali, Lahore), (Umar, Karachi)}

= {(1010, Ali), (1020, Umar)}

17

Cartesian-Product Operation
Notation r x s
Defined as: r x s = {t q | t r and q s}
Assume that attributes of r(R) and s(S)

are disjoint. (That is, R S = ).


If attributes of r(R) and s(S) are not

disjoint, then renaming must be used.

18

Cartesian-Product Example
Borrower x Loan
C
Borrower
C

Loan

AM

N
x

G9 1

AM

G9 1

AM

F7 2

AM

E6 3

BA

F7 2

BA

G9 1

UF

E6 3

BA

F7 2

BA

E6 3

UF

G9 1

UF

F7 2

UF

E6 3

19

Composition of Operations
Can build expressions using multiple operations
Borrower x Loan

A=N(Borrower x Loan)

A=N

AM

G9 1

AM

F7 2

AM

E6 3

BA

G9 1

AM

G9 1

BA

F7 2

BA

F7 2

UF

E6 3

BA

E6 3

UF

G9 1

UF

F7 2

UF

E6 3

20

Operation Composition Example


loan (loan_number, branch_name, amount)
borrower (customer_name, loan_number)
Find the names of all customers who have taken

a loan from the Aabpara branch.

customer_name (branch_name=Aabpara (
borrower.loan_number = loan.loan_number (borrower x loan)

21

Which Expression is Optimal?


Find the names of all customers who have taken a

loan from the Aabpara branch.

Query 1
customer_name (branch_name = Aabpara (
borrower.loan_number = loan.loan_number (borrower x loan)))
Query 2

customer_name(loan.loan_number = borrower.loan_number (
(branch_name = Aabpara (loan)) x borrower))

22

Another Example
Find the names of all customers who have taken

a loan from the Aabpara branch but do not have


an account at any branch of the bank.
depositor (customer_name, account_number)

customer_name(loan.loan_number = borrower.loan_number (
(branch_name = Aabpara (loan)) x borrower))
customer_name(depositor)

23

Yet Another Example


account (account_number, branch_name, balance)
Find the account with largest balance
Find

those balances that are not the largest

!Rename

account relation as d so that we can


compare each account balance with all others

Use

set difference to find those account balances


that were not found in the earlier step.

account_number(account) - account.account_number (
account.balance < d.balance (account x d (account))
)
24

Formal Definition
A basic expression in the relational algebra consists of either one

of the following:

A constant relation

A relation in the database

Let E1 and E2 be relational-algebra expressions; the following are

all relational-algebra expressions:

E1 E2

E1 E2

E1 x E2

p (E1), P is a predicate on attributes in E1

s(E1), S is a list consisting of some of the attributes in E1

x (E1), x is the new name for the result of E1


25

You might also like