Chapter 2: Intro To Relational Model
Chapter 2: Intro To Relational Model
Example of a Relation
attributes
(or columns)
tuples
(or rows)
Attribute Types
Each attribute of a relation has a name.
The set of allowed values for each attribute is called the domain of the
attribute
advisor
Bad design:
univ (instructor -ID, name, dept_name, salary, student_Id, ..)
results in
which one?
Should choose an attribute whose value never, or very rarely,
changes.
Foreign Keys
A relation schema may have an attribute that corresponds to the primary
key of another relation. The attribute is called a foreign key.
Categories of languages
Procedural
The user instructs the system to perform a sequence of operations on the database
to compute the desired result
Non-procedural, or declarative
The user describes the desired information without giving a specific procedure for
obtaining that information
select:
project:
union:
set difference: –
Cartesian product: x
rename:
The operators take one or two relations as inputs and produce a new
relation as a result.
Selection of tuples
Relation r
Example of selection:
Salary>85000(Instructor)
Selection of Columns (Attributes)
Relation r:
Select A and C
Projection
Π A, C (r)
Project Operation
Notation:
A1 , A2 ,, Ak (r )
where A1, A2 are attribute names and r is a relation name.
Example:
ID,Salary (Instructor)
Union of two relations
Relations r, s:
r s:
Union Operation
Notation: r s
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible
(example: 2n d column of r deals with the same type of values as
does the 2nd column of s)
r – s:
Set Difference Operation
Notation r–s
r x s:
Cartesian-Product Operation
Notation r x s
Defined as:
r x s = {t q | t r and q s}
If attributes of r(R) and s(S) are not disjoint, then renaming must
be used.
Composition of Operations
Can build expressions using multiple operations
Example: A=C(r x s)
rxs A B C D E
1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
A=C(r x s)
A B C D E
1 10 a
2 10 a
2 20 b
Rename Operation
Allows us to name, and therefore to refer to, the results of relational-
algebra expressions.
Allows us to refer to a relation by more than one name.
Example:
x (E)
returns the expression E under the name X
x( A 1 , A2 ,...,An ) (E )
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
Relational Algebra operations
Banking Example
Find the loan number for each loan of an amount greater than $1200
Find the names of all customers who have a loan, an account, or both,
from the bank
Query 1
Query 2
customer_name(loan.loan_number = borrower.loan_number
(
(branch_name = “Perryridge” (loan)) x borrower )
)
Example Queries
Find the names of all customers who have a loan at the
Perryridge branch but do not have an account at any branch of
the bank.
)
–
customer_name(depositor)
Example Queries
Find the largest account balance
Strategy:
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.
The query is:
balance(account) - account.balance
(account.balance < d.balance (account x d (account)))
Additional Operations
We define additional operations that do not add any power to the relational
algebra, but that simplify common queries.
Set intersection
Natural join
Set-Intersection Operation
Notation: rs
Defined as:
r s = { t | t r and t s }
Assume:
r, s have the same arity
attributes of r and s are compatible
Note:
r s = r – (r – s)
Set Intersection of two relations
Relation r, s:
rs
Natural Join Example
Relations r, s:
Natural Join
r s
Bank Example Queries
Find the names of all customers who have a loan and an account at
bank.
Find the name of all customers who have a loan at the bank and the
loan amount
Computes the join and then adds tuples form one relation that does not
match tuples in the other relation to the result of the join.
Relation borrower
customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155
Outer Join – Example
Join
loan borrower