1. For example, to find the branch-name, loan number, customer name and amount for loans over $1200:
{t︱t å½2 borrow ∧ t[amount] > 1200}
This gives us all attributes, but suppose we only want the customer names. (We would use project in the
algebra.) We need to write an expression for a relation on scheme (cname).
{t︱ å½2 borrow (t[cname] = s[cname] ∧ s[amount] > 1200)}
In English, we may read this equation as the set of all tuples t such that there exists a tuple s in the relation borrow for which the values of t and s for the cname attribute are equal, and the value of s for the amount attribute is greater than 1200."
The notation 9t 2 r(Q(t)) means \there exists a tuple t in relation r such that predicate Q(t) is true". How did we get the above expression? We needed tuples on scheme cname such that there were tuples in borrow pertaining to that customer name with amount attribute > 1200.
The tuples t get the scheme cname implicitly as that is the only attribute t is mentioned with. Let's look at a more complex example.
Find all customers having a loan from the SFU branch, and the the cities in which they live:
{t|å½s ∈ borrow(t[cname] = s[cname] ∧ s[bname] = "SFU"
∧å½u ∈ customer(u[cname] = s[cname] ∧ t[ccity] = u[ccity]))}
In English, we might read this as \the set of all (cname,ccity) tuples for which cname is a borrower at the SFU branch, and ccity is the city of cname".
Tuple variable s ensures that the customer is a borrower at the SFU branch. Tuple variable u is restricted to pertain to the same customer as s, and also ensures that ccity is the city of the customer.
The logical connectives ^ (AND) and _ (OR) are allowed, as well as : (negation). We also use the existential quanti er 9 and the universal quantifier ∀.
Some more examples:
1. Find all customers having a loan, an account, or both at the SFU branch:
{t|å½s ∈ borrow(t[cname] = s[cname] ∧ s[bname] = "SFU")
å½u ∈ deposit(t[cname] = u[cname] ∧ u[bname] = "SFU")}
Note the use of the _ connective.
As usual, set operations remove all duplicates.
2. Find all customers who have both a loan and an account at the SFU branch.
Solution: simply change the ∨ connective in 1 to a ∧.
3. Find customers who have an account, but not a loan at the SFU branch.
{t|å½u ∈ deposit(t[cname] = u[cname] ∧ u[bname] = "SFU")
∧ï¹å½s ∈ borrow(t[cname] = s[cname] ∧ s[bname] = "SFU")g
4. Find all customers who have an account at all branches located in Brooklyn. (We used division in relational algebra.)
For this example we will use implication, denoted by a pointing nger in the text, but by ) here. The
formula P → Q means P implies Q, or, if P is true, then Q must be true.
{t ∀u 2 branch (u[bcity] = "Brooklyn" )
å½s ∈deposit(t[cname] = s[cname] ∧ u[bname] = s[bname]))}
In English: the set of all cname tuples t such that for all tuples u in the branch relation, if the value of u
on attribute bcity is Brooklyn, then the customer has an account at the branch whose name appears in the bname attribute of u.
Division is diffcult to understand. Think it through carefully.
