Retrieving Table Relationships or Joins
Hello everybody,
Is there any way to obtain the relation between the tables of a relational database ?
For example, I have two tables from the Northwind database:
Employees and EmployeeTerritories
They are related or joined by the field:
EmployeeID
I would like to know if ADO or any other tool cant tell me automatically that Employees and EmployeeTerritories are related or joined by the field EmployeeID.
Thank you in advance.
[487 byte] By [
Michael] at [2007-11-11 10:23:30]

# 1 Re: Retrieving Table Relationships or Joins
You may be able to obtain that information using SQLDMO or ADOX. The relationship is called a "foreign key constraint."
# 2 Re: Retrieving Table Relationships or Joins
Hello Phil,
I want to do this using .Net 2003 and C#, can I use ADOX and SQLDMO with C# too ? or is there a way to use ADO.NET to do this ? If I can use ADO.NET I would appreciate to have some sample code.
Thank you very much
# 3 Re: Retrieving Table Relationships or Joins
Yes, you may use ADOX or SQLDMO with .NET. I do not know of any way to obtain this information via ADO.NET, but you might try searching the Web.
# 4 Re: Retrieving Table Relationships or Joins
Thank you for your help Phil.
Best Regards
# 5 Re: Retrieving Table Relationships or Joins
It appears to me that you're looking for a "reverse engineering" tool that can generate an entity-relationship diagram for your database. There are several products on the market that can attempt to do this; ERwin by Computer Associates comes to mind. Be aware that this kind of reverse-engineering becomes quite difficult if the corresponding foreign key and primary key field names don't match. This happens even on commercial-grade systems. Good luck.
# 6 Re: Retrieving Table Relationships or Joins
Thank you very much bschaettle, in fact that's my problem.
The field names are different so it's hard to understand which one is the primary keys on the other tables.
Thanks again
# 7 Re: Retrieving Table Relationships or Joins
Hello guys,
I found some code that solved my problem so I thought this can be useful.
This is the code:
DataTable schemaTable;
//This code retrieves the foreign keys of all the tables in a Schema
schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Foreign_Keys, new Object[] {null, theSchema, null});
foreach(DataRow myRow in schemaTable.Rows)
{
String PK_TABLE_NAME = myRow ["PK_TABLE_NAME"] as String;
String PK_COLUMN_NAME = myRow ["PK_COLUMN_NAME"] as String;
String FK_TABLE_NAME = myRow ["FK_TABLE_NAME"] as String;
String FK_COLUMN_NAME = myRow ["FK_COLUMN_NAME"] as String;
}
Where:
PK_TABLE_NAME = The table that contains a Primary Key
PK_COLUMN_NAME = The Field used as Primary Key
FK_TABLE_NAME = The table that has a relation with the table that contains the Primary key (PK_TABLE_NAME)
FK_COLUMN_NAME = The field name that is used as a foreign key and is contained in the related table (FK_TABLE_NAME)
Hope this could help others.
:WAVE:
# 8 Re: Retrieving Table Relationships or Joins
There is an article explanning relationships in databases, hope it will help you but i donno if i should be giving the link here or not...i think i should not, anyways go to alachisoft.com and search for many to many relationships in O/R Mapping...article from there. Hope it will help you with your basic understanding of relationships.
smarz at 2007-11-11 23:50:26 >
