5

Query a Table's Foreign Key Relationship とほぼ同じですが、SQL Server 2000の場合です。

特定のテーブル 'foo' に対して、foo を指す外部キーを持つ一連のテーブルを生成するためのクエリが必要です。

4

4 に答える 4

7
SELECT o2.name
FROM sysobjects o
INNER JOIN sysforeignkeys fk on o.id = fk.rkeyid
INNER JOIN sysobjects o2 on fk.fkeyid = o2.id
WHERE o.name = 'foo'
于 2009-01-14T20:39:23.873 に答える
3

このT-SQLを試してください:

select col_name(fkeyid, fkey) as column_name, object_name(rkeyid) as referenced_table_name, col_name(rkeyid, rkey) as referenced_column_name from sysforeignkeys where object_name(fkeyid) = 'tableNameHere' order by constid

特定のテーブルに依存する他のすべてのテーブルを提供するために、クエリを少し書き直しました。

select object_name(fkeyid),
col_name(fkeyid, fkey) as column_name,
col_name(rkeyid, rkey) as referenced_column_name
from sysforeignkeys
where object_name(rkeyid) = 'tableNameHere'
order by constid
于 2009-01-14T20:31:20.147 に答える
2

親と子

/* this will find out all of the foreign key references for a table*/

DECLARE @tableName varchar(128)
SET @tableName = 'tCounter'

SELECT   
    pt.[name] AS 'parentTable',  
    ct.[name] AS 'childTable',  
    fk.[name] AS 'fkName', 
*   
FROM sys.foreign_keys fk        
    INNER JOIN sys.tables pt              
        ON pt.object_ID = fk.parent_object_id        
    INNER JOIN sys.tables ct              
        ON ct.object_ID = fk.referenced_object_id   
WHERE pt.name = @tableName      
    OR ct.name = @tableName   
ORDER BY pt.name, ct.name
于 2009-01-14T20:36:42.070 に答える
1

ここから始める

    SELECT cons.TABLE_NAME
    , cons.CONSTRAINT_NAME PK_NAME
    , cols.COLUMN_NAME 
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS cons
LEFT join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cols
ON cons.CONSTRAINT_NAME = cols.CONSTRAINT_NAME
WHERE cons.CONSTRAINT_TYPE = 'foreign KEY'
ORDER BY cons.TABLE_NAME
    , cons.CONSTRAINT_NAME
    , cols.COLUMN_NAME

[編集] フォーマットがめちゃくちゃ

[編集2] もはや

于 2009-01-14T20:29:31.257 に答える