03 Relational Algebra
03 Relational Algebra
03 Relational Algebra
“Retrieve all tuples for accounts in the Los Angeles branch.” acct_id branch_name balance
σbranch_name=“Los Angeles”(account) A-318 Los Angeles 550
A-322 Los Angeles 275
“Retrieve all tuples for accounts in the Los Angeles branch, acct_id branch_name balance
with a balance under $300.” A-322 Los Angeles 275
σbranch_name=“Los Angeles” ∧ balance<300(account)
www.its.ac.id INSTITUT TEKNOLOGI SEPULUH NOPEMBER, Surabaya - Indonesia
Project Operation
Written as: Πa,b,…(r)
• Result relation contains only specified attributes of r
• Specified attributes must actually be in schema of r
• Result’s schema only contains the specified attributes
• Domains are same as source attributes’ domains
• Important note:
• Result relation may have fewer rows than input relation!
• Why?
• Relations are sets of tuples, not multisets
“Retrieve all branch names that have at least one account.” branch_name
Πbranch_name(account) New York
Seattle
Los Angeles
• Result only has three tuples, even though input has five
• Result schema is just (branch_name)
Written as: r ∪ s
• Result contains all tuples from r and s
• Each tuple is unique, even if it’s in both r and s
• Constraints on schemas for r and s?
• r and s must have compatible schemas:
• r and s must have same arity (same number of attributes)
• For each attribute i in r and s, r[i] must have the same domain as s[i]
• (Our examples also generally have same attribute names, but not required! Arity and
domains are what matter.)
• Note:
• Even though these expressions are equivalent, order of join operations can
dramatically affect query cost!
• (Keep this in mind for later…)
• account is actually technically a relation variable, as are all our named relations so far
www.its.ac.id INSTITUT TEKNOLOGI SEPULUH NOPEMBER, Surabaya - Indonesia
Assignment Operation
• Can assign a relation-value to a relation-variable
• Written as: 𝑟𝑒𝑙𝑣𝑎𝑟 ← 𝐸
• E is an expression that evaluates a relation
• Unlike ρ, the name relvar persists in the database
• Often used for temporary relation-variables:
temp1 ⟵ ΠR–S(r)
temp2 ⟵ ΠR–S((temp1 × s) – ΠR–S,S(r))
result ⟵ temp1 – temp2
• Query evaluation becomes a sequence of steps
• (This is an implementation of the ÷ operator)
• Can also use assignment operation to modify data
• More about updates next time…
www.its.ac.id INSTITUT TEKNOLOGI SEPULUH NOPEMBER, Surabaya - Indonesia
Guest (GuestID, GuestName, GuestAddress)
Hotel (HotelID, HotelName, HotelAddress)
Room (RoomID, HotelID, Type, Price)
Booking (HotelID, GuestID, RoomID, DateFrom, DateTo)
For each question, write the relational algebra that will fulfill the request. Please describe your answer!
1. List full details of all hotels.
2. List full details of all hotels in Surabaya.
3. List the number of rooms in each hotel in Surabaya.
4. List the names and addresses of all guests in Surabaya.
5. List all double or family rooms with a price below Rp 1,500,000 per night.
For each question, write the relational algebra that will fulfill the request. Please describe your answer!
1. Find the names of all black products.
2. Find all prices for products that are black or white.
3. Find the StoreID of all stores who sell a product that is black or white.
4. Find the StoreID of all stores who sell a product that is black and white.
5. Find the names of all stores who supply a product that is black or white.