1

我正在尝试获取一些返回以下内容的查询:

TableName | ColumnName | IsColumnInRelation | RelatedTable | RelationKind | InsertUpdateSepecification | IsNull | Describtion
-------------------------------------------------------------------------------------------------------------------------------------
  Company |  Personel  |        Yes         |   Employee   |      One     |           Cascade          |  True  | 'company employees'
  Company |     ID     |        No          |   --------   |      ---     |           -------          |  False | 'company pk'
  Company |     Name   |        No          |   --------   |      ---     |           -------          |  False | 'name of company'
  Company |  Country   |        Yes         |   Contries   |      Many    |           No Action        |  False | 'companies placement'
  Company |     .      |         .          |       .      |       .      |              .             |    .   | ''
  Company |     .      |         .          |       .      |       .      |              .             |    .   | ''
  Company |     .      |         .          |       .      |       .      |              .             |    .   | ''
  Company |     .      |         .          |       .      |       .      |              .             |    .   | ''

有人可以帮忙吗?谢谢你。

4

2 回答 2

1

这应该给你你正在寻找的东西:

SELECT t.name AS [TableName]
    ,c.name AS [ColumnName]
    ,CASE WHEN COALESCE(tr.name, tr2.name) IS NULL THEN 'No' 
        ELSE 'Yes' END AS IsColumnInRelation
    ,COALESCE(tr.name, tr2.name) AS [RelatedTable]
    ,COALESCE(cr.name, cr2.name) AS [RelatedColumn] 
    ,CASE WHEN COALESCE(tr.name, tr2.name) IS NOT NULL THEN
        CASE WHEN i.object_id IS NOT NULL THEN 'One'
        ELSE 'Many' END
     END AS RelationKind
    ,COALESCE(f.update_referential_action_desc, f2.update_referential_action_desc) AS [InsertUpdateAction]
    ,COALESCE(f.delete_referential_action_desc, f2.delete_referential_action_desc) AS [DeleteAction]
    ,c.is_nullable AS [IsNull]
    ,p.value AS Description
FROM sys.columns c
    INNER JOIN sys.tables t
        ON t.object_id= c.object_id

    -- Used for the column description.
    LEFT JOIN sys.extended_properties p 
        ON c.object_id = p.major_id
        AND c.column_id = p.minor_id
        AND p.name = 'MS_Description'

    -- Used to identify if this is a primary key.
    -- If it is, then this is on the "One" side of the relationship.
    LEFT JOIN  sys.index_columns  ic
        ON ic.object_id = c.object_id 
        AND ic.column_id = c.column_id
    LEFT JOIN sys.indexes  i
        ON ic.object_id = i.object_id 
        AND i.is_primary_key = 1

    -- Used to get the tables and columns that relate to this column.
    LEFT JOIN sys.foreign_key_columns AS fc
        ON c.object_id = fc.parent_object_id
        AND c.column_id = fc.parent_column_id
    LEFT JOIN sys.columns cr
        ON cr.object_id = fc.referenced_object_id
        AND cr.column_id = fc.referenced_column_id
    LEFT JOIN sys.tables tr
        ON cr.object_id = tr.object_id

    -- Used to get the update/delete action for the [fc] relationship.
    LEFT JOIN sys.foreign_keys f
        ON f.object_id = fc.constraint_object_id

    -- Used to get the tables and columns that this column relates to.
    LEFT JOIN sys.foreign_key_columns AS fc2
        ON c.object_id = fc2.referenced_object_id
        AND c.column_id = fc2.referenced_column_id
    LEFT JOIN sys.columns cr2
        ON cr2.object_id = fc2.parent_object_id
        AND cr2.column_id = fc2.parent_column_id
    LEFT JOIN sys.tables tr2
        ON cr2.object_id = tr2.object_id

    -- Used to get the update/delete action for the [fc2] relationship.
    LEFT JOIN sys.foreign_keys f2
        ON f2.object_id = fc2.constraint_object_id
ORDER BY t.name, c.name, COALESCE(tr.name, tr2.name), COALESCE(cr.name, cr2.name)
于 2012-07-04T13:11:00.297 回答
0

这里有一些可以帮助你的东西。它显示了您数据库上的所有关系:

SELECT
K_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT ON PT.TABLE_NAME = PK.TABLE_NAME
于 2012-07-04T11:00:00.443 回答