I have a bunch of tables in a relational database which, obviously, are dependent upon one another due to foreign key relationships. I want to build a dependency tree, traverse it, and output INSERT SQL statements. I need to first output SQL for foreign key tables in my dependency tree first because parent tables will depend on values from their foreign key identifier tables.
我在关系数据库中有一堆表,显然,由于外键关系,它们彼此依赖。我想构建一个依赖树,遍历它,并输出INSERT SQL语句。我需要首先在我的依赖关系树中为外键表输出SQL,因为父表将依赖于其外键标识符表中的值。
Does a binary tree, traversed in postorder, seem suitable for this task?
以后序遍历的二叉树是否适合此任务?
2 个解决方案
#1
If a table can be dependent on more than two tables, a binary tree will be insufficient. Let table A be dependent on tables B, C and D. Then you would have to insert into B, C and D first, i.e. A should have three child nodes in your tree.
如果表可以依赖于两个以上的表,则二叉树将不足。让表A依赖于表B,C和D.然后你必须首先插入B,C和D,即A应该在树中有三个子节点。
I think you need to use a more general tree structure which allows an arbitrary number of child nodes. Traversing this tree structure in post-order should yield the desired results, as you suggested.
我认为你需要使用更通用的树结构,它允许任意数量的子节点。如您所建议的那样,按顺序遍历此树结构应产生所需的结果。
Things will start to get messy when your dependency graph contains cycles and you need to defer constraint checking ;)
当您的依赖图包含循环并且您需要延迟约束检查时,事情将开始变得混乱;)
#2
Take a look at the following:
看看以下内容:
Microsoft.SqlServer.Management.Smo.Server
Microsoft.SqlServer.Management.Smo.Database
Microsoft.SqlServer.Management.Smo.Scripter
Microsoft.SqlServer.Management.Smo.Server Microsoft.SqlServer.Management.Smo.Database Microsoft.SqlServer.Management.Smo.Scripter
Microsoft.SqlServer.Management.Smo.DependencyTree
Microsoft.SqlServer.Management.Smo.DependencyWalker
Microsoft.SqlServer.Management.Smo.DependencyCollection
Microsoft.SqlServer.Management.Smo.DependencyCollectionNode
Microsoft.SqlServer.Management.Smo.DependencyTree Microsoft.SqlServer.Management.Smo.DependencyWalker Microsoft.SqlServer.Management.Smo.DependencyCollection Microsoft.SqlServer.Management.Smo.DependencyCollectionNode
There's examples on MSDN on how to use all this.
MSDN上有关如何使用这一切的例子。
Essentially you want something like
基本上你想要的东西
Server server = new Server(SOURCESERVER);
Database database = server.Databases[SOURCEDATABASE];
Scripter sp = new Scripter(server);
...
UrnCollection col = new UrnCollection();
foreach (Table table in database.Tables)
{
col.Add(table.Urn);
}
....
DependencyTree tree = sp.DiscoverDependencies(col, DependencyType.Parents);
DependencyWalker walker = new DependencyWalker(server);
DependencyCollection depends = walker.WalkDependencies(tree);
//Iterate over each table in DB in dependent order...
foreach (DependencyCollectionNode dcn in depends)
...
#1
If a table can be dependent on more than two tables, a binary tree will be insufficient. Let table A be dependent on tables B, C and D. Then you would have to insert into B, C and D first, i.e. A should have three child nodes in your tree.
如果表可以依赖于两个以上的表,则二叉树将不足。让表A依赖于表B,C和D.然后你必须首先插入B,C和D,即A应该在树中有三个子节点。
I think you need to use a more general tree structure which allows an arbitrary number of child nodes. Traversing this tree structure in post-order should yield the desired results, as you suggested.
我认为你需要使用更通用的树结构,它允许任意数量的子节点。如您所建议的那样,按顺序遍历此树结构应产生所需的结果。
Things will start to get messy when your dependency graph contains cycles and you need to defer constraint checking ;)
当您的依赖图包含循环并且您需要延迟约束检查时,事情将开始变得混乱;)
#2
Take a look at the following:
看看以下内容:
Microsoft.SqlServer.Management.Smo.Server
Microsoft.SqlServer.Management.Smo.Database
Microsoft.SqlServer.Management.Smo.Scripter
Microsoft.SqlServer.Management.Smo.Server Microsoft.SqlServer.Management.Smo.Database Microsoft.SqlServer.Management.Smo.Scripter
Microsoft.SqlServer.Management.Smo.DependencyTree
Microsoft.SqlServer.Management.Smo.DependencyWalker
Microsoft.SqlServer.Management.Smo.DependencyCollection
Microsoft.SqlServer.Management.Smo.DependencyCollectionNode
Microsoft.SqlServer.Management.Smo.DependencyTree Microsoft.SqlServer.Management.Smo.DependencyWalker Microsoft.SqlServer.Management.Smo.DependencyCollection Microsoft.SqlServer.Management.Smo.DependencyCollectionNode
There's examples on MSDN on how to use all this.
MSDN上有关如何使用这一切的例子。
Essentially you want something like
基本上你想要的东西
Server server = new Server(SOURCESERVER);
Database database = server.Databases[SOURCEDATABASE];
Scripter sp = new Scripter(server);
...
UrnCollection col = new UrnCollection();
foreach (Table table in database.Tables)
{
col.Add(table.Urn);
}
....
DependencyTree tree = sp.DiscoverDependencies(col, DependencyType.Parents);
DependencyWalker walker = new DependencyWalker(server);
DependencyCollection depends = walker.WalkDependencies(tree);
//Iterate over each table in DB in dependent order...
foreach (DependencyCollectionNode dcn in depends)
...