查找“许多”上的所有表和字段与某个表的关系的一侧

发布于 2024-09-04 22:46:04 字数 165 浏览 6 评论 0原文

在 Sql Server 2005 中,我有一个主表和其他几个与 这个主人通过几个一对多的关系。 我怎样才能找到与相关的所有表和字段 主表中的主键,在“多”方面?

我知道我可以通过查询 INFORMATION_SCHEMA 中的视图来提取它, 但我不知道在哪里可以找到这些信息。

谢谢

in Sql Server 2005, I have a master table, and several other tables which are related to
this master through several one to many relations.
How can I find all tables and fields which are in relation with the
primary key in the master table, on "many" side?

I know I can extract this by querying views from INFORMATION_SCHEMA,
but I don't know where exactly I can find this info.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再见 2024-09-11 22:46:04

查看:

INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE

Check out:

INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE

不奢求什么 2024-09-11 22:46:04

我在 sql server groups 的一些帮助下找到了答案
我使用以下查询,它返回关系的一侧和多侧的架构名称、表和字段名称:

SELECT
    SchemaParent.name AS ParentSchemaName, 
    TableParent.name AS ParentTableName, 
    ColumnParent.name AS ParentColumnName, 
    SchemaChild.name AS ChildSchemaName, 
    TableChild.name AS ChildTableName, 
    ColumnChild.name AS ChildColumnName
FROM         
    sys.foreign_key_columns AS kc INNER JOIN
        sys.objects AS TableChild ON kc.parent_object_id = TableChild.object_id INNER JOIN
        sys.schemas AS SchemaChild ON TableChild.schema_id = SchemaChild.schema_id INNER JOIN
        sys.objects AS TableParent ON kc.referenced_object_id = TableParent.object_id INNER JOIN
        sys.schemas AS SchemaParent ON TableParent.schema_id = SchemaParent.schema_id INNER JOIN
        sys.columns AS ColumnParent ON kc.referenced_object_id = ColumnParent.object_id AND kc.referenced_column_id = ColumnParent.column_id INNER JOIN
        sys.columns AS ColumnChild ON kc.parent_object_id = ColumnChild.object_id AND kc.parent_column_id = ColumnChild.column_id
ORDER BY ParentTableName, ChildTableName

I found the answer with some help on sql server groups
I use the following query, which returns me schema name, table and field name on one and many side of the relations:

SELECT
    SchemaParent.name AS ParentSchemaName, 
    TableParent.name AS ParentTableName, 
    ColumnParent.name AS ParentColumnName, 
    SchemaChild.name AS ChildSchemaName, 
    TableChild.name AS ChildTableName, 
    ColumnChild.name AS ChildColumnName
FROM         
    sys.foreign_key_columns AS kc INNER JOIN
        sys.objects AS TableChild ON kc.parent_object_id = TableChild.object_id INNER JOIN
        sys.schemas AS SchemaChild ON TableChild.schema_id = SchemaChild.schema_id INNER JOIN
        sys.objects AS TableParent ON kc.referenced_object_id = TableParent.object_id INNER JOIN
        sys.schemas AS SchemaParent ON TableParent.schema_id = SchemaParent.schema_id INNER JOIN
        sys.columns AS ColumnParent ON kc.referenced_object_id = ColumnParent.object_id AND kc.referenced_column_id = ColumnParent.column_id INNER JOIN
        sys.columns AS ColumnChild ON kc.parent_object_id = ColumnChild.object_id AND kc.parent_column_id = ColumnChild.column_id
ORDER BY ParentTableName, ChildTableName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文