My exercise answer heavily relies on relational algebra procedural query language.
Consider the following relational database schema:
person(ID, firstName, lastName, nickName) follows(pID1, pID2)
The relational instance person stores information for each person. Each person is identified by their ID. The relational instance follows keeps track of who follows whom, i.e., pID1 follows pID2. The attributes pID1 and pID2 are both foreign keys to the attribute ID in the relation person
Write a relational algebra query that finds all the pairs of nickNames (name1, name2), such that name1 follows name2.
How do the attributes name1
and name2
suddenly came into play since the exercise statement didn't specify these two at the start?
What do name1
and name2
represent (for instance, attributes or sub constituents of nickName
)?
How am I supposed to use these two brand-new attributes while writing the relational algebra query?
My answer:
π nickName (σ pID1=ID ∧ pID2=ID (person ⨝ follows))
Breaking it down:
person ⨝ follows
- Used the natural join operation (also named Cartesian product?) to combine the tables and ease the filtering process.σ pID1=ID ∧ pID2=ID
- Used the selection operation to filter out the names that don't respect the aforementioned order--"name 1 follows name 2"--associating the constraint to the ID attribute.
c) π nickName
- Retrieved just the column `nickName and its associated domains which are its encapsulated char-type values.In a standard SQL database you're looking at something like
select distinct p1.nickName name1, p2.nickName name2
from follows f
join person p1 on p1.ID = f.pID1
join person p2 on p2.ID = f.pID2