sql-server-2000 – 查询以获取SQL Server 2000中的所有外键约束

前端之家收集整理的这篇文章主要介绍了sql-server-2000 – 查询以获取SQL Server 2000中的所有外键约束前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要查询sql Server 2000以获取所有外键的列表.

特别是所有指向特定列的外键.

我该如何编写此查询

解决方法

select * from sysobjects
where xtype = 'F'

我希望这应该是诀窍并与sql Server 2000兼容!

如果您还需要sql Server 2000中的表和列信息,它会涉及更多;您需要加入sysforeignkeys和syscolumns目录视图,如下所示:

select
  so.name 'foreign key name',OBJECT_NAME(parent_obj) 'table',OBJECT_NAME(sf.fkeyid) 'referencing table',sc1.name 'referencing column',OBJECT_NAME(sf.rkeyid) 'referenced table',sc2.name 'referenced column'
from sysobjects so
inner join sysforeignkeys sf on so.id = sf.constid
inner join syscolumns sc1 on sf.fkeyid = sc1.id and sf.fkey = sc1.colid
inner join syscolumns sc2 on sf.rkeyid = sc2.id and sf.fkey = sc2.colid
where so.xtype in ('F','PK')

如果您想利用sql Server 2000中确实可用的INFORMATION_SCHEMA视图,请使用以下查询

SELECT
    rc.CONSTRAINT_NAME,rcu.TABLE_NAME 'Referencing Table',rcu.COLUMN_NAME 'Referencing Column',rcu1.TABLE_NAME 'Referenced Table',rcu1.COLUMN_NAME 'Referenced Column'
FROM
    INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
INNER JOIN 
    INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE rcu 
      ON rc.CONSTRAINT_CATALOG = rcu.CONSTRAINT_CATALOG 
         AND rc.CONSTRAINT_NAME = rcu.CONSTRAINT_NAME
INNER JOIN 
    INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE rcu1 
      ON rc.UNIQUE_CONSTRAINT_CATALOG = rcu1.CONSTRAINT_CATALOG 
         AND rc.UNIQUE_CONSTRAINT_NAME = rcu1.CONSTRAINT_NAME

渣子

原文链接:https://www.f2er.com/mssql/83733.html

猜你在找的MsSQL相关文章