ago
149 views
1 1 vote

Consider the relations:

$\mathrm{Users(username, name, email, password, address)}$

and

$\mathrm{FriendsWith(username, username2, sincewhen)}$.

A friendship tuple indicates that the users appearing in its two username columns are friends.

Define

$\mathrm{F=\rho_{fuser\leftarrow username}(\pi_{username}(FriendsWith))\ \cup\ \rho_{fuser\leftarrow username2}(\pi_{username2}(FriendsWith))}$

Thus, $\mathrm{F}$ contains the username of every person who appears in either side of at least one friendship.

Which expression returns the $\mathrm{username}$ and $\mathrm{email}$ of exactly those users who have no friends, using only basic relational algebra operators?

  1. $\mathrm{\pi_{username,email}(Users)-\pi_{username,email}\left(\sigma_{username=fuser}(Users\times F)\right)}$
  2. $\pi_{\mathrm{username,email}}\left(\sigma_{\mathrm{username=fuser}}(\mathrm{Users}\times \mathrm{F})\right)$ $-$ $\pi_{\mathrm{username,email}}(\mathrm{Users})$

  3. $\pi_{\mathrm{username,email}}(\mathrm{Users})$ $\cup$ $\pi_{\mathrm{username,email}}\left(\sigma_{\mathrm{username=fuser}}(\mathrm{Users}\times \mathrm{F})\right)$

  4. $\pi_{\mathrm{username,email}}\left(\sigma_{\mathrm{username=fuser}}(\mathrm{Users}\times \mathrm{F})\right)$

2 Answers

2 2 votes

First understand $\mathrm{F}$.

The first part

$\rho_{\mathrm{fuser}\leftarrow \mathrm{username}}(\pi_{\mathrm{username}}(\mathrm{FriendsWith}))$

contains everyone appearing in the first friendship column.

The second part

$\rho_{\mathrm{fuser}\leftarrow \mathrm{username2}}(\pi_{\mathrm{username2}}(\mathrm{FriendsWith}))$

contains everyone appearing in the second friendship column.

Their union therefore contains every user participating in at least one friendship.

Now consider

$\mathrm{Users}\times \mathrm{F}$.

Select matching usernames:

$\sigma_{\mathrm{username=fuser}}(\mathrm{Users}\times \mathrm{F})$.

These are precisely the $\mathrm{Users}$ tuples belonging to people who have at least one friend.

Project:

$\pi_{\mathrm{username,email}}\left(\sigma_{\mathrm{username=fuser}}(\mathrm{Users}\times \mathrm{F})\right)$.

So this relation represents:

users who have at least one friend.

But we need the opposite.

Start from all users:

$\pi_{\mathrm{username,email}}(\mathrm{Users})$

and subtract users who have friends:

$\pi_{\mathrm{username,email}}(\mathrm{Users})$ $-$ $\pi_{\mathrm{username,email}}\left(\sigma_{\mathrm{username=fuser}}(\mathrm{Users}\times \mathrm{F})\right)$.

Therefore, A is correct.

ago
0 0 votes

First understand FF.

The first part

ρfuser←username(πusername(FriendsWith))ρfuser←username​(πusername​(FriendsWith))

contains everyone appearing in the first friendship column.

The second part

ρfuser←username2(πusername2(FriendsWith))ρfuser←username2​(πusername2​(FriendsWith))

contains everyone appearing in the second friendship column.

Their union therefore contains every user participating in at least one friendship.

Now consider

Users×FUsers×F.

Select matching usernames:

σusername=fuser(Users×F)σusername=fuser​(Users×F).

These are precisely the UsersUsers tuples belonging to people who have at least one friend.

Project:

πusername,email(σusername=fuser(Users×F))πusername,email​(σusername=fuser​(Users×F)).

So this relation represents:

users who have at least one friend.

But we need the opposite.

Start from all users:

πusername,email(Users)πusername,email​(Users)

and subtract users who have friends:

πusername,email(Users)πusername,email​(Users) −− πusername,email(σusername=fuser(Users×F))πusername,email​(σusername=fuser​(Users×F)).

Therefore, A is correct.

ago
Answer:
Position:
Show:

Related questions

1 1 vote
1 1 answer
89
89 views
GO Classes asked 5 days ago
89 views
Consider $\text{parts(pno, pname, price)}$.Which relational algebra expression returns exactly the names of all parts whose price is greater than $\$200$?$\pi_{\text{pnam...
1 1 vote
1 1 answer
78
78 views
GO Classes asked 5 days ago
78 views
Consider $R(a,b)$ and $S(c,d)$.Which relational algebra expression is equivalent to:SELECT a, d FROM R, S WHERE R.a 10 AND R.b = S.c;Use only the basic operators.$\pi_{a...
1 1 vote
1 1 answer
83
83 views
GO Classes asked 5 days ago
83 views
Consider the relations:$\mathrm{STUDENT(name,regno,gpa,level,dept)}$$\mathrm{COURSE(cno,cname,dept)}$$\mathrm{TAKE(regno,cno)}$Using only the basic relational algebra ope...
1 1 vote
1 1 answer
77
77 views
GO Classes asked 5 days ago
77 views
Let $R$ and $S$ be union-compatible relations.Which expression computes $R\cap S$ using only union and set difference?$(R\cup S)-((R-S)\cup(S-R))$ $(R\cup S)-(R-S)$ $(R-S...