Adapted for SEN2104 – DBMS
Week 5: Relational Algebra (Part III)
Database System Concepts
©Silberschatz, Korth and Sudarshan
Modification of the Database
▪ The content of the database may be modified using the following
operations:
▪ Deletion
▪ Insertion
▪ Updating
▪ All these operations are expressed using the assignment
operator.
2
Deletion
▪ A delete request is expressed similarly to a query, except instead
of displaying tuples to the user, the selected tuples are removed
from the database.
▪ Can delete only whole tuples; cannot delete values on only
particular attributes
▪ A deletion is expressed in relational algebra by:
rr–E
where r is a relation and E is a relational algebra query.
3
Quick Review: Set Difference Operation
▪ Notation r – s
▪ Defined as:
r – s = {t | t r and t s}
▪ For r – s to be valid:
1. r, s must have the same arity
2. The attribute domains must be compatible
4
Quick Review: Set Difference Operation
▪ Relations r, s:
A B A B
1 2
2 s
1
r
▪ r – s:
A B
1
1
5
Banking Enterprise Schema Diagram
branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
6
6
Deletion Examples
▪ Delete all account records in the Perryridge branch.
account account – branch_name = “Perryridge” (account )
▪ Delete all loan records with amount in the range of 0 to 50
loan loan – amount 0 and amount 50 (loan)
▪ Delete all accounts at branches located in Needham.
r1 branch_city = “Needham” (account branch )
r2 account_number, branch_name, balance (r1)
r3 customer_name, account_number (r2 depositor)
account account – r2
depositor depositor – r3
7
Insertion
▪ To insert data into a relation, we either:
▪ specify a tuple to be inserted
▪ write a query whose result is a set of tuples to be inserted
▪ in relational algebra, an insertion is expressed by:
r r E
where r is a relation and E is a relational algebra expression.
▪ The insertion of a single tuple is expressed by letting E be a constant
relation containing one tuple.
8
Quick Review: Set 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
2. The attribute domains must be compatible
9
Quick Review: Set Union Operation
▪ Relations r, s: A B A B
1 3
2 s
1
r
A B
▪ r s: 1
2
1
3
10
Insertion Examples
▪ Insert information in the database specifying that Smith has $1200 in
account A-973 at the Perryridge branch.
account account {(“A-973”, “Perryridge”, 1200)}
depositor depositor {(“Smith”, “A-973”)}
▪ Provide as a gift for all loan customers in the Perryridge branch, a
$200 savings account. Let the loan number serve as the account
number for the new savings account.
r1 (branch_name = “Perryridge” (borrower loan))
account account loan_number, branch_name, 200 (r1)
depositor depositor customer_name, loan_number (r1)
11
Updating
▪ 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
12
Update Examples
▪ Make interest payments by increasing all balances by 5 percent.
account account_number, branch_name, balance * 1.05 (account)
▪ Pay all accounts with balances over $10,000 6 percent interest
and pay all others 5 percent.
account account_number, branch_name, balance * 1.06 (balance 10000 (account))
account_number, branch_name, balance * 1.05 (balance 10000 (account))
13
Update Examples
▪ Pay a 10 percent bonus to all accounts with balances over $25,000.
Do not make bonus payments to other accounts.
account account_number, branch_name, balance * 1.10 (balance 25000 (account))
account_number, branch_name, balance (balance 25000 (account))
14
Exercises
Given relational schema:
Sailor (sid, sname, city, birthdate)
Boat (bid, bname, color)
SailorBoat (sid, bid, reservationdate)
1) Find the names of sailors who’ve reserved boat #103.
2) Find the names of sailors who’ve reserved a red boat.
3) Find the sailors who’ve reserved a red or a green boat.
4) Find the sailors who’ve reserved a red and a green boat.
5) Find the names of sailors who’ve reserved all boats.
15
1) Find the names of sailors who’ve reserved boat #103
Solution 1: πsname(σbid = 103 (SailorBoat ⋈ Sailor))
Solution 2: temp1 σbid = 103 (SailorBoat)
temp2 temp1⋈ Sailor
πsname(temp2)
Solution 3: πsname((σbid = 103 SailorBoat) ⋈ Sailor)
Sailor (sid, sname, city, birthdate)
Boat (bid, bname, color)
SailorBoat (sid, bid, reservationdate)
16
2) Find the names of sailors who’ve reserved a red boat
▪ Boat (bid, bname, color)
▪ Information about boat color only available in Boat relation; so, need an
extra join:
πsname((σcolor = ‘red’ Boat) ⋈ SailorBoat ⋈ Sailor)
A more efficient solution -- why more efficient?
πsname(πsid(πbid(σcolor = ‘red’ Boat) ⋈ SailorBoat) ⋈ Sailor)
A query optimizer can find this, given the first solution!
Sailor (sid, sname, city, birthdate)
Boat (bid, bname, color)
SailorBoat (sid, bid, reservationdate)
17
3) Find the sailors who’ve reserved a red or a green boat
▪ Can identify all red or green boats, then find sailors who’ve reserved one
of these boats:
TempBoats (σcolor = ‘red’ color = ‘green’ (Boat))
πsname(TempBoats ⋈ SailorBoat ⋈ Sailor)
What happens if is replaced by in this query?
Sailor (sid, sname, city, birthdate)
Boat (bid, bname, color)
SailorBoat (sid, bid, reservationdate)
18
4) Find the sailors who’ve reserved a red and a green boat
▪ Previous approach won’t work! Why?
▪ Logically incorrect, an attribute can’t contain two different values
at the same time.
▪ Solution: Must identify sailors who’ve reserved red boats and
sailors who’ve reserved green boats.
▪ Then find the intersection (note that sid is a key for Sailor relation).
Sailor (sid, sname, city, birthdate)
Boat (bid, bname, color)
SailorBoat (sid, bid, reservationdate)
19
4) Find the sailors who’ve reserved a red and a green boat
TempRed πsid(σcolor = ‘red’ (Boat) ⋈ SailorBoat)
TempGreen πsid(σcolor = ‘green’ (Boat) ⋈ SailorBoat)
πsname((TempRed ∩ TempGreen) ⋈ Sailor)
Sailor (sid, sname, city, birthdate)
Boat (bid, bname, color)
SailorBoat (sid, bid, reservationdate)
20
5) Find the names of sailors who’ve reserved all boats
▪ Uses division; schemas of the input relations to division (/ or )
must be carefully chosen:
TempSids πsid, bid(SailorBoat) πbid(Boat)
πsname(TempSids ⋈ Sailor)
Sailor (sid, sname, city, birthdate)
Boat (bid, bname, color)
SailorBoat (sid, bid, reservationdate)
21
5) Find the names of sailors who’ve reserved all boats
▪ Uses division; schemas of the input relations to division (/ or )
must be carefully chosen:
ρTempSids(πsid, bid(SailorBoat) πbid(Boat)) Uses rename operator
instead of assignment
operator.
πsname(TempSids ⋈ Sailor)
Sailor (sid, sname, city, birthdate)
Boat (bid, bname, color)
SailorBoat (sid, bid, reservationdate)
22