Professional Documents
Culture Documents
03 Relational Algebra PDF
03 Relational Algebra PDF
Relational Algebra
constraints
Relational algebra is abstract specification of
The
operations
SQL: Implementation of relational algebra operations
Relational Algebra
Procedural language: As a meta language it
, 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
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
Relation r
A
12 3
23 10
23 10
More Examples
loan (loan_number, branch_name, amount)
Find all loans of over $1200
$10,000
Project Operation
Notation: A1, A2, Ak ( r )
where
sets.
Example: eliminate the branch_name attribute of account
account_number, balance
(account)
A,C (r)
10
20
30
40
Another Example
loan (loan_number, branch_name, amount)
Find the loan number for each loan of an
10
Union Operation
Notation: r s
Defined as: r s = {t | t r or t s}
For r s to be valid.
r,
The
!Example:
11
r s:
12
Example
depositor (customer_name, account_number)
borrower (customer_name, loan_number)
Find the names of all customers
Customers
compatible relations.
r
attribute
14
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)
x ( A ,A
1
2 ,..., An
(E )
returns
16
Cartesian-Product Background
Compute cross product r x s
r
= {Ali, Umar}
= {1010, 1020}
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)
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
customer_name (branch_name=Aabpara (
borrower.loan_number = loan.loan_number (borrower x loan)
21
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
customer_name(loan.loan_number = borrower.loan_number (
(branch_name = Aabpara (loan)) x borrower))
customer_name(depositor)
23
!Rename
Use
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
E1 E2
E1 E2
E1 x E2