seph
seph

Reputation: 694

Visualize relationships between tables

I'm trying to accomplish the following:

I have several tables, some of them containing primary keys from other tables.
I tried to prepare a SQLFiddle for clarification, you can find it here http://sqlfiddle.com/#!3/38ea2/1

+-----------+    +-----------+-----------+    +-----------+-----------+    +-----------+-----------+    
|   tblA    |    |          tblB         |    |          tblC         |    |          tblD         |
+-----------+    +-----------+-----------+    +-----------+-----------+    +-----------+-----------+
|   ID_A    |    |   ID_B    |   ID_A    |    |   ID_C    |   ID_B    |    |   ID_D    |   ID_C    |
+-----------+    +-----------+-----------+    +-----------+-----------+    +-----------+-----------+
|   1       |    |     2     |     1     |    |     3     |     2     |    |     4     |     3     |
+-----------+    +-----------+-----------+    +-----------+-----------+    +-----------+-----------+


My goal is to create a procedure which returns all the available relationships between the tables and visualize that at the end (using VB.NET).
The way I want to visualize this would be something like this:

ID_A(1)
    ID_B(2)
        ID_C(3)<-- current
            ID_D(4)

All of the items in this list are hyperlinked and lead to a site with their respective "journal", i.e. if you click on ID_B, then it would print:

ID_A(1)
    ID_B(2)<-- current
        ID_C(3)
            ID_D(4)

(I hope you guys know now where I'm coming from, since I'm struggling a bit with describing my problem.)

So I need a procedure/function with (at least) 2 parameters, @iID and @sFieldName, I think.
So calling GetRelationships(3, 'ID_C') should give back 2 (ID_B), 1 (ID_A) and 4 (ID_D) back, since that's how they are connected.

I think a good way to start would be collecting which tables (and their PKs) are connected and storing them in a temporary table, I am still playing around with this and will edit the opening post when I got something useful.

I would really appreciate some hints or ideas.

ps. I'm using SQL Server 2005

Upvotes: 0

Views: 328

Answers (2)

M.Ali
M.Ali

Reputation: 69594

DECLARE @TableName NVARCHAR(1000)= 'TableName'
DECLARE @ColumnName NVARCHAR(1000)= 'ColumnName'


SELECT f.name AS ForeignKey,
 SCHEMA_NAME(f.SCHEMA_ID) SchemaName,
 OBJECT_NAME(f.parent_object_id) AS TableName,
 COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,
 SCHEMA_NAME(o.SCHEMA_ID) ReferenceSchemaName,
 OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
 COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName
 FROM sys.foreign_keys AS f
 INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
 INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
WHERE OBJECT_NAME(f.parent_object_id) =  @TableName AND COL_NAME(fc.parent_object_id,fc.parent_column_id) = @ColumnName
 GO 

Upvotes: 1

Mike Dinescu
Mike Dinescu

Reputation: 55760

You should consider a recursive approach. Basically write a stored procedure which resolves immediate dependencies at one level (i.e. foreign keys into your table, and foreign keys from your table). Then once you get the results, invoke again on each result. Repeat until you build the full hierarchy.

The foreign relations can be queried in SQL server via the sys.foreign_key_columns and sys.objects tables.

Also have a look at this question: How can I list all foreign keys referencing a given table in SQL Server?

Upvotes: 1

Related Questions