Question

Consider a social network database that has two relations: Person (pid, name) Relationship (pid1,...

Consider a social network database that has two relations:

Person (pid, name)

Relationship (pid1, rel, pid2)

Person.pid is the key for Person, and Relationship.pid1 and Relationship.pid2 are foreign keys to Person. rel is a string representing the relationship type, and it can only be ’friend’ or ’enemy’. A tuple (1,’friend’,2), means that the person with id 1 considers the person with id 2 a friend. Note that the relationship is not symmetric: if A is friends with B, it does not imply that B is friend with A.

a) Create a query that contains pairs of people who share mutual feelings for each other (i.e., they consider each other friends or enemies). The query should return (name1,name2). Note that the query should not repeat the same pair of people in different order (e.g., (A,B) and (B,A)); your query should only keep the pair, such that the name with the lowest pid appears first.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a)

(Select P1.name,P2.name from Person P1 inner join Relationship in P1.pid = Relationship.pid1 inner join Person P2 on P2.pid = Relationship.pid2 and rel = 'friend' order by P1.pid) UNION (Select P1.name,P2.name from Person P1 inner join Relationship in P1.pid = Relationship.pid1 inner join Person P2 on P2.pid = Relationship.pid2 and rel = 'enemy' order by P1.pid)

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
Consider a social network database that has two relations: Person (pid, name) Relationship (pid1,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider a database schema consisting of two tables, Employee (ID, Name, Address), Project(PID, Name, Deadline), Assign...

    Consider a database schema consisting of two tables, Employee (ID, Name, Address), Project(PID, Name, Deadline), Assign (EID, PID, Date). Assign.EID is a foreign key referencing employee's ID and Assign.PID is a foreign key reference the project. Write the SQL query for 1. Find Projects that are not assigned to any employees(PID and Name of the project).

  • Consider the SOCIAL NETWORK relational database schema description provided below which is used to manage a...

    Consider the SOCIAL NETWORK relational database schema description provided below which is used to manage a social network, where: - Persons are the users - They can have friends (a friend relation is symmetric meaning that if person1 is friend of person2, the person2 is automatically friend of person1) - They can post multiple posts on his wall or in other friend’s wall - They can like posts with different types of likes: like, love, fun, wow, sad or angry....

  • May I ask the SQL code as follows? The relational database moviedb has the following database...

    May I ask the SQL code as follows? The relational database moviedb has the following database schema: Movie(title, production year, country, run time, major genre) primary key : {title, production year} Person(id, first name, last name, year born) primary key : {id} Award(award name, institution, country) primary key : {award name} Restriction Category(description, country) primary key : {description, country} Director(id, title, production year) primary key : {title, production year} foreign keys : [title, production year] ⊆ Movie[title, production year] [id]...

  • what discuss can you make about medicalization and chronic disease and illness? Adult Lealth Nursing Ethics...

    what discuss can you make about medicalization and chronic disease and illness? Adult Lealth Nursing Ethics mie B. Butts OBJECTIVES After reading this chapter, the reader should be able to do the following: 1. Explore the concept of medicalization as it relates to the societal shift away from physician predominance of the 1970s. 2. Differentiate among the following terms: compliance, noncompliance, adherence, nonadherence, and concordance. 3. Examine cultural views with regard to self-determination, decision making, and American healthcare professionals' values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT