在Sql Server 2005中,我有一个主表,以及其他几个与之相关的表 这个高手通过几个一对多的关系。 如何找到与之相关的所有表格和字段 主表中的主键,在“很多”方面?
我知道我可以通过查询INFORMATION_SCHEMA中的视图来提取它, 但我不知道到底能找到这个信息。
谢谢
答案 0 :(得分:1)
退房:
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
答案 1 :(得分:0)
我在sql server组上找到了一些帮助 我使用以下查询,它在关系的一个和多个方面返回模式名称,表和字段名称:
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