Solved

ER Diagram - how to relate/join these tabels?

Posted on 2008-11-01
22
1,177 Views
Last Modified: 2012-05-05
See Attachment:

As you can see I have a "ContactInformation" table
I then have two country fields
- Country (which is the country of the account holder)
- CountryOfCitzenship (as the name suggest this is the country of citizenship)

How do I join the yellow table with the orange table?
countryerd.jpg
0
Comment
Question by:Khou
22 Comments
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
You create a second instance of Country, aliased say Country_1 or CountryCitizenship, and relate that to CountryOfCitizenship.

/gustav
0
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 100 total points
Comment Utility
With Two instances of the Country table, you join the First instance to your Contact information table, on the Country field, and the second instance (CountryCitizenship) on the CountryOfCitizenship field.

You cannot have a single instance of Country, joined to both fields.
0
 
LVL 3

Expert Comment

by:johnsmith1962
Comment Utility
I am pretty sure you can do that with ERwin.  That is use one table for both foreign keys.  This is really a question of  how your ER diagramming software works, not Oracle.  But if your software wont let you use one table for both fields, than the answer above will work.
0
 

Author Comment

by:Khou
Comment Utility
Are both diagrams correct?
See Diagram ER A or ER B


countryerdlinked.jpg
countryerdlinked2.jpg
0
 

Author Comment

by:Khou
Comment Utility
ER A:
Contactinformation table has two foreign keys
1 x FK CountryID (has 1 to 1 with Country table)
1 x FK CountryCitizenshipID (has 1 to 1 with Country table)


ER B:
Contactinformation table has two foreign keys
1 x FK CountryID (has 1 to 1 with Country table)
1 x FK CountryCitizenshipID (has 1 to 1 with CountryCitizenship table)

CountryCitizenship table has 1 foreign key
1 x FK CountryID (has 1 to 1 with Country table)
0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 350 total points
Comment Utility
Well, to be precise, you use one table but two instances of this.

I'm pretty sure Khou knows how to deal with the first instance for the country and explained how to add the second instance for the citizenship. Arthur describes the full process.

If an ER programming tool did not behave this way visually, it had to do so behind the scene.

/gustav
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
Neither is right.

Contactinformation table has two foreign keys
1 x FK CountryID (has 1 to 1 with Country table)
1 x FK CountryCitizenshipID (has 1 to 1 with Country table As CountryCitizenship)

CountryCitizenship (or any other name different from Country) is an alias for table Country.

/gustav
0
 

Author Comment

by:Khou
Comment Utility
Hi Cactus

Cant figure out how to create Alias! (Using Visual Paradigm)
can you show me how it looks like with VP? or any other ER software.
0
 

Author Comment

by:Khou
Comment Utility
why is diagram ER A and ER B wrong?
0
 

Author Comment

by:Khou
Comment Utility
Hi Experts!

This is not going to work? (see diagram ER A, ER B)



Diagram ER A:ContactInformation Table
FK CountryID = 1111 <-- is FK of countryID
FK CountryOfCitizenshipID = 1111 <--- is FK of CountryID
Country Table
PK CountryID = 1111, name = Australia
PK CountryID = 7777, name = United Kingdom



Hence, ContactInformation table
Country = Australia
CountryOfCitizenship = Australia


-----------------------------------------------------------------------------------------------------
Diagram ER B:ContactInformation Table
FK CountryID = 1111 <-- is FK of countryID
FK CountryOfCitizenshipID = 12345 <--- is FK of CountryOfCitizenship

Country Table
PK CountryID = 1111, name = Australia
PK CountryID = 7777, name = United Kingdom

CountryOfCitizenship Table
PK CountryOfCitizenshipID = 12345, FK CountryID = 1111



Hence, ContactInformation table
Country = Australia
CountryOfCitizenship = Australia
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
ER A will not do because you cannot have a single instance of Country joined to two fields in the same table (except if these for each record carried the same value which wouldn't make sense).

ER B assumes that some relation exists between country of address and country of citizenship which, I guess, is not the case neither here nor in general.

In most ER designers with a GUI you can drag and drop a table on the pane to create an instance of that table. This instance is typically given the name of the table, here Country. If you do it twice, the second instance is typically aliased with a trailing "_1", here Country_1. This alias (a property of the instance) you can typically later rename to what you prefer other than any other table name/alias already existing on the pane.

So, you should end up with something like this:

ContactInformation Table
FK CountryID = 1111 <-- is FK of countryID
FK CountryOfCitizenshipID = 77777 <--- is FK of CountryOfCitizenship

Country Table
PK CountryID = 1111, name = Australia
CountryOfCitizenship alias  <--- Country_1, alias for Country Table
PK CountryID = 7777, name = United Kingdom

/gustav
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:Khou
Comment Utility
Understood, but unable to draw this with the tool,
ie "In most ER designers with a GUI you can drag and drop a table on the pane to create an instance of that table."

Such as ERWIN?

-how would you do this in SQL Management Studio? or
-Visual Paradigm?

By the way "CountryOfCitizenship alias  <--- Country_1, alias for Country Table" this is not a physical table is it!. im unable to create this.
0
 

Author Comment

by:Khou
Comment Utility
by the way, was this a mistake? You have two Primary key for CountryID.


Country Table
PK CountryID = 1111, name = Australia
CountryOfCitizenship alias  <--- Country_1, alias for Country Table
PK CountryID = 7777, name = United Kingdom
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
In Visual Studio you can do it in the DataSet designer, but that probably won't help you.

According to this, SQL Server Management Studio 2005 has a limitation:

http://www.windows-tech.info/15/a47ed0ff53dbd825.php

I don't know about Erwin - too pricey for me - but I'm confident it has this capability.

/gustav


0
 

Author Comment

by:Khou
Comment Utility
Hi, to clarify

May I know when you create the "Country" and "Country_1" table, are they actually meaning the same underlying table, or 2 separated table actually?
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 350 total points
Comment Utility
Yes, one "physical" Country table only, but two instances.

Even Microsoft Access can do this (not for modelling though, but for defining relations), so Erwin must have this ability too, for example for creating a self-join.

/gustav
0
 

Author Comment

by:Khou
Comment Utility
I can create a self join in my software, but i thought you wanted to create a table alias?
am I understanding this wrong?

Could you draw up a simple diagram with this an alias? so I can see how it actually looks like!
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
Yes.
It is not a self-join, only that the visualization is much like this.

/gustav
countryerdlinked-1.jpg
0
 

Author Comment

by:Khou
Comment Utility
cactus_data: why wouldnt this work? (see diagram).

erd.jpg
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
I guess it would, but you would introduce a relationship between country of address and country of citizenship which I don't think exists. And if not, this setup will over-complicate things.
It may, however, be correct for your scenario. If you are unsure about this, your client must provide more information.

/gustav
0
 

Assisted Solution

by:Mariner73
Mariner73 earned 50 total points
Comment Utility
Only a suggestion:
Can you put 2 FK into ContactInformation Table,
that points to Country tabe?
like in some previous coment:
Diagram ER A:
ContactInformation Table
FK1 CountryID = ---> is FK to countryID
FK2 CountryOfCitizenshipID = ---> is FK to CountryID

Or you can create in Erwin "Logical link" without any FK at least if you don't need them explicitly.
0
 

Author Closing Comment

by:Khou
Comment Utility
Thanks guys
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now