Helper Program

Than Lwin Aung

Data-Path

Entity Relationship Diagram Vs. Directed Graph Vs. Graph Theory
Suppose if we have an entity relationship diagram like this, we can perceive the diagram as a directed graph. Each table represents a node and each primary-foreign key relationship pair represents an edge.

By running the following query:
SELECT name,object_name(parent_object_id) As Source,object_name(referenced_object_id) as Reference FROM Sys.Foreign_Key

1

Helper Program

Than Lwin Aung

Data-Path

the query returns the directed edges information between tables.

Therefore, it is possible to transverse through the ERD as it is a directed graph. However, the directed graph can only be traversed in one direction. For example, A cannot directly go to G. Fortunately, we can easily transform the ERD into general graph, in which edges have no direction. We just need to modify the query as follow:
SELECT object_name(parent_object_id) FROM Sys.Foreign_Keys WHERE object_name(referenced_object_id) = '<TableName>' UNION SELECT object_name(referenced_object_id) FROM Sys.Foreign_Keys WHERE object_name(parent_object_id) = '<TableName>'

With this query, the ERD will be the general graph, in which we can traverse more easily as long as there is a connection between each table. It does not matter whether the connection is primaryto-foreign or foreign-to-primary. By using the query, we can write a recursive graph traversal function, which will find a path from one table to another. One biggest advantage is that it will save time. Suppose we have a hundreds of tables linked to each other. In this case, looking for manually a data-path among the tables will be really tedious. Just by giving source and destination tables and letting the program figure out the path between them will be really helpful.
2

Helper Program

Than Lwin Aung

Data-Path

The ERD traversal program has 2 main parts: the first function is to look for all the links to others for a given table, and the second function is, by using the first function, to actually look for the path between two tables. Code: GetDestinations Function
private List<string> GetDestinations(string Source) { List<string> destinations = new List<string>(); SqlConnection cnx = new SqlConnection(ConnectionString); cnx.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = cnx; cmd.CommandText = "SELECT object_name(parent_object_id) FROM Sys.Foreign_Keys WHERE object_name(referenced_object_id) = '" + Source + "' UNION SELECT object_name(referenced_object_id) FROM Sys.Foreign_Keys WHERE object_name(parent_object_id) = '" + Source + "'"; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); foreach (DataRow dr in ds.Tables[0].Rows) { destinations.Add(dr.ItemArray[0].ToString()); } cnx.Close(); return destinations; }

3

Helper Program

Than Lwin Aung

Data-Path

Code: FindPath Function
private bool FindPath(string Source, string Destination) { List<string> Destinations = GetDestinations(Source); Path.Add(Source); foreach (string dest in Destinations) { if (!Path.Contains(dest)) { if (dest.Equals(Destination)) { Path.Add(Destination); return true; } } } foreach (string dest in Destinations) { if (!Path.Contains(dest)) { if (FindPath(dest, Destination)) { return true; } } } Path.Remove(Source); return false; }

4

Sign up to vote on this title
UsefulNot useful