2つのクエリを使用したいと思います。1つは、指定されたテーブルのすべての外部キーを次の情報とともにリストするものですSchema name, foreign table name
およびその他のすべての主キーをリストします。
主キーをリストするこのクエリは正しいですか?
select syssc.name as schemaname , cast(c.name as varchar(255)) as foreign_table ,
cast(p.name as varchar(255)) as primary_table
from sysobjects f
inner join sysobjects c on f.parent_obj = c.id
inner join sysreferences r on f.id = r.constid
inner join sysobjects p on r.rkeyid = p.id
inner join syscolumns rc on r.rkeyid = rc.id and r.rkey1 = rc.colid
inner join syscolumns fc on r.fkeyid = fc.id and r.fkey1 = fc.colid
left join syscolumns rc2 on r.rkeyid = rc2.id and r.rkey2 = rc.colid
left join syscolumns fc2 on r.fkeyid = fc2.id and r.fkey2 = fc.colid
inner join sys.tables syst on rc.id=syst.object_id
inner join sys.schemas syssc on syst.schema_id=syssc.schema_id
where f.type = 'F' AND c.name in ('table name' )
これは、外部キーを取得するために現在使用しているクエリです
select syssc.name as schemaname ,cast(c.name as varchar(255)) as foreign_table ,
cast(p.name as varchar(255)) as primary_table
from sysobjects f
inner join sysobjects c on f.parent_obj = c.id
inner join sysreferences r on f.id = r.constid
inner join sysobjects p on r.rkeyid = p.id
inner join syscolumns rc on r.rkeyid = rc.id and r.rkey1 = rc.colid
inner join syscolumns fc on r.fkeyid = fc.id and r.fkey1 = fc.colid
left join syscolumns rc2 on r.rkeyid = rc2.id and r.rkey2 = rc.colid
left join syscolumns fc2 on r.fkeyid = fc2.id and r.fkey2 = fc.colid
inner join sys.tables syst on rc.id=syst.object_id
inner join sys.schemas syssc on syst.schema_id=syssc.schema_id
where f.type = 'F' and p.name in ('table name')
これらのクエリが適切で効率的なクエリかどうかを確認してください。
新しいカタログビューに慣れる必要があります。古いsysobjects、syscolumnsなどは非推奨になり、下位互換性のためにのみ提供されています。これらは新しい開発に使用しないでください。古いコードは最終的に新しいオブジェクトに移行するはずです。
以下は、外部キーに関係する列の両側を取得するクエリです。
SELECT
constraint_name = OBJECT_NAME(fkc.constraint_object_id),
foreign_schema = SCHEMA_NAME(child.[schema_id]),
foreign_table = child.name,
foreign_column = child_cols.name,
referenced_schema = SCHEMA_NAME(referenced.[schema_id]),
referenced_table = referenced.name,
referenced_column = referenced_cols.name
FROM sys.foreign_key_columns AS fkc
INNER JOIN sys.tables AS child
ON fkc.parent_object_id = child.[object_id]
INNER JOIN sys.tables AS referenced
ON fkc.referenced_object_id = referenced.[object_id]
INNER JOIN sys.columns AS referenced_cols
ON fkc.parent_column_id = referenced_cols.column_id
AND referenced_cols.[object_id] = referenced.[object_id]
INNER JOIN sys.columns AS child_cols
ON fkc.referenced_column_id = child_cols.column_id
AND child_cols.[object_id] = child.[object_id]
WHERE referenced.name = 'table name'
-- if you want the tables that a child table references, use this instead:
-- WHERE child.name = 'table name'
ORDER BY fkc.parent_column_id;
そして、これは関係するテーブルについてだけ、はるかに簡単です:
SELECT
constraint_name = name,
foreign_schema = OBJECT_SCHEMA_NAME(parent_object_id),
foreign_table = OBJECT_NAME(parent_object_id),
referenced_schema = OBJECT_SCHEMA_NAME(referenced_object_id),
referenced_table = OBJECT_NAME(referenced_object_id)
FROM sys.foreign_keys
WHERE OBJECT_NAME(referenced_object_id) = 'table name'
-- if you want the tables that a child table references, use this instead:
-- WHERE OBJECT_NAME(parent_object_id) = 'table name';
[〜#〜] edit [〜#〜]ドキュメントへのリンクを追加します(これらはほとんどSQL Server 2008 R2階層内にありますが、おおまかにSQL Server 2008にも相当):