databaseoracleoracle-apexdatabase-normalizationnatural-join

SQL query to join these 3 tables to remove spurious rows - OWNER, TYPE, PERSON


I'm having some trouble joining these 3 tables.

Table1
OWNER     TYPE
O1        T1
O1        T2
O2        T1

Table2
OWNER     PERSON
O1        P1
O1        P2
O2        P1

Table3
TYPE     PERSON
T1       P1
T2       P2
T1       P2

I want to do a join on all 3 tables so my output would be:

FinalTable
OWNER     TYPE     PERSON
O1        T1       P1
O1        T2       P2
O2        T1       P2
O1        T1       P2

I'm basically demonstrating how 5NF removes spurious rows. When I join any 2 combinations of the table I get extra data (which is what I expect).

I'm having difficulty joining the third table though, I've never had to work with joining all 3.

This is my query so far:

select r1.*, r2.PERSON from table1 r1, table2 r2, table3 r3
where r1.OWNER = r2.OWNER and r2.PERSON = r3.PERSON 

My output returns 8 rows and some are duplicated. I've tried playing around with the query but it only increases the rows further.

My query is based on an ORACLE APEX tutorial book I have but it isn't joining 3 tables that are laid out like mine.

How do I join these 3 tables to remove spurious rows that I get after joining 2?


Solution

  • I put 1 2 3 to every column name to see which table they were under before. First 3 Query are your approaches and 1 of the relations is ignored in each. 4th approach all 3 relations are put in.

    Unwanted output occured when a relation ignored (for example person.when person2-person3 relation ignored) person2s via table2.owner2 and person3s via table3.type3 will all be added to result as if they are irrelevant fields.

    http://sqlfiddle.com/#!9/52075d/23