CH 2
CH 2
Example of a Relation
Basic Structure
● Formally, given sets D1, D2, …. Dn a relation r is a subset of
D1 x D2 x … x Dn
Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai ∈ Di
● Example: If
customer_name = {Jones, Smith, Curry, Lindsay}
customer_street = {Main, North, Park}
customer_city = {Harrison, Rye, Pittsfield}
Then r = { (Jones, Main, Harrison),
(Smith, North, Rye),
(Curry, North, Rye),
(Lindsay, Park, Pittsfield) }
is a relation over
customer_name x customer_street x customer_city
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
● Attribute values are (normally) required to be atomic; that is, indivisible
● Note: multivalued attribute values are not atomic
● Note: composite attribute values are not atomic
● The special value null is a member of every domain
● The null value causes complications in the definition of many operations
Relation Schema
● A1, A2, …, An are attributes
attributes
(or columns)
customer_name customer_street customer_city
customer
Relations are Unordered
α α 1 7
α β 5 7
β β 12 3
β β 23 10
α α 1 7
β β 23 10
Select Operation
● Notation: σ p(r)
● p is called the selection predicate
● Defined as:
● Example of selection:
σ branch_name=“Perryridge”(account)
Project Operation – Example
● Relation r: A B C
α 10 1
α 20 1
β 30 1
β 40 2
∏A,C (r) A C A C
α 1 α 1
α 1 = β 1
β 1 β 2
β 2
Project Operation
● Notation:
…
α 1 α 2
α 2 β 3
β 1 s
r
A B
● r ∪ s: α 1
α 2
β 1
β 3
Union Operation
● Notation: r ∪ s
● Defined as:
r ∪ s = {t | t ∈ r or t ∈ s}
● For r ∪ s to be valid.
1. r, s must have the same arity (arity means number of attributes)
2. The attribute domains must be compatible (example: 2nd column of r deals with the same
type of values as does the 2nd column of s)
● Example: to find all customers with either an account or a loan
∏customer_name (depositor) ∪ ∏customer_name (borrower)
Set Difference Operation – Example
● Relations r, s:
A B A B
α 1 α 2
α 2 β 3
β 1 s
r
● r – s:
A B
α 1
β 1
Set Difference Operation
● Notation r – s
● Defined as:
r – s = {t | t ∈ r and t ∉ s}
α 1 α 10 a
β 10 a
β 2
β 20 b
r γ 10 b
s
● r x s:
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
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.
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: Symbol name is rho
ρ x (E)
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
Banking Example
branch (branch_name, branch_city, assets)
● Find the loan number for each loan of an amount greater than $1200
● Find the names of all customers who have a loan and an account at bank.
∏customer_name (σbranch_name=“Perryridge”
(σborrower.loan_number = loan.loan_number(borrower x loan)))
● Query 1
● Query 2
∏customer_name(σloan.loan_number = borrower.loan_number (
(σbranch_name = “Perryridge” (loan)) x borrower))
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)))
Formal Definition
● A basic expression in the relational algebra consists of either one of the following:
● A relation in the database
● A constant relation
● Let E1 and E2 be relational-algebra expressions; the following are all relational-algebra
expressions:
● E1 ∪ E2
● E1 – E2
● E1 x E2
● Set intersection
● Natural join
● Assignment
Set-Intersection Operation
● Notation: r ∩ s
● 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 Operation – Example
● Relation r, s:
A B A B
α 1 α 2
α 2 β 3
β 1
r s
● r∩s
A B
α 2
Natural-Join Operation
● Notation: r s
➔ Let r and s be relations on schemas R and S respectively.
Then, r s is a relation on schema R ∪ S obtained as follows:
◆ Consider each pair of tuples tr from r and ts from s.
◆ If tr and ts have the same value on each of the attributes in R ∩ S, add a tuple t to the result,
where
● t has the same value as t on r
r
● t has the same value as t on s
s
➔ Example:
R = (A, B, C, D)
S = (E, B, D)
◆ Result schema = (A, B, C, D, E)
◆ r s is defined as:
∏r.A, r.B, r.C, r.D, s.E (σr.B = s.B ∧ r.D = s.D (r x s))
Natural Join Operation – Example
● Relations r, s:
A B C D B D E
α 1 α a 1 a α
β 2 γ a 3 a β
γ 4 β b 1 a γ
α 1 γ a 2 b δ
δ 2 β b 3 b ∈
r s
● r s
A B C D E
α 1 α a α
α 1 α a γ
α 1 γ a α
α 1 γ a γ
δ 2 β b δ
Assignment Operation
➔ The assignment operation (←) provides a convenient way to express complex queries.
◆ Write query as a sequential program consisting of
● a series of assignments
● followed by an expression whose value is displayed as a result of the query.
◆ Assignment must always be made to a temporary relation variable.
➔ Example:
temp1 ← ∏R-S (r )
temp2 ← ∏R-S ((temp1 x s ) – ∏R-S,S (r ))
result = temp1 – temp2
◆ The result to the right of the ← is assigned to the relation variable on the left of the ←.
◆ May use variable in subsequent expressions.
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
Extended Relational-Algebra-Operations
● Generalized Projection
● Aggregate Functions
● Outer Join
Generalized Projection
● Extends the projection operation by allowing arithmetic functions to be used in the projection list.
… …
α α 7
α β 7
β β 3
β β 10
27
Aggregate Operation – Example
● Relation account grouped by branch-name:
branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
Aggregate Functions (Cont.)
● Result of aggregation does not have a name
● Can use rename operation to give it a name
● For convenience, we permit renaming as part of aggregate operation
● Relation borrower
customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155
Outer Join – Example
● Inner Join
loan Borrower
● A mechanism to change a value in a tuple without charging all values in the tuple
● Use the generalized projection operator to do this task
● Each Fi is either
● the I th attribute of r, if the I th attribute is not updated, or,
● if the attribute is to be updated Fi is an expression, involving only constants and the
attributes of r, which gives the new value for the attribute
Update Examples