+ -
当前位置:首页 → 问答吧 → 怎么样能查找表的外键关系

怎么样能查找表的外键关系

时间:2011-12-02

来源:互联网

如题,在MS SQL中。怎么样能快速的查找外键关系呢?
就是不要用鼠标操作,有没有sql语句可以一次性把表的外键关系查找出来!
做等高人的出现!
等待中~~~~~~~~~~~

作者: lrl1286219215   发布时间: 2011-12-02

SQL code
CREATE FUNCTION dbo.f_foreignKeys(@tableName nvarchar(max))
RETURNS TABLE
AS
RETURN(
select * from (
 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  
) as t
where K_Table = @tableName
or PK_Table = @tableName
)
GO

作者: ssp2009   发布时间: 2011-12-02

SQL code
select a.name as foreign_columns_name,b.name foreign_key_namea
from sys.columns a inner join sys.foreign_keys b on a.object_id=b.parent_object_id

作者: qianjin036a   发布时间: 2011-12-02

SQL code
SELECT 主键列ID=b.rkey 
    ,主键列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid) 
    ,外键表ID=b.fkeyid 
    ,外键表名称=object_name(b.fkeyid) 
    ,外键列ID=b.fkey 
    ,外键列名=(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid) 
    ,级联更新=ObjectProperty(a.id,'CnstIsUpdateCascade') 
    ,级联删除=ObjectProperty(a.id,'CnstIsDeleteCascade') 
FROM sysobjects a 
    join sysforeignkeys b on a.id=b.constid 
    join sysobjects c on a.parent_obj=c.id 
where a.xtype='f' AND c.xtype='U' 
--    and object_name(b.rkeyid)='titles'如果单独查询一个表,则加上这个条件.

作者: dawugui   发布时间: 2011-12-02

SQL code
--查询外键约束
select FK_Name as [外键名],Parent_Tab_Name as [外键表],
    [外键列]=stuff((select ''+[Parent_Col_Name] from (
        select    FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name,
                Referenced_Tab.Name as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name
        from sys.foreign_keys FK
        inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id
        inner join sys.objects Parent_Tab ON  Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U'
        inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id 
                                            and  Col.parent_column_id = Parent_Col.column_id
        inner join sys.objects Referenced_Tab ON  Col.referenced_object_id = Referenced_Tab.Object_ID and Referenced_Tab.TYPE = 'U'
        inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id 
                                            and  Col.referenced_column_id = Referenced_Col.column_id
    )t where FK_Name=tb.FK_Name  and Parent_Tab_Name = tb.Parent_Tab_Name and Referenced_Tab_Name = tb.Referenced_Tab_Name   for xml path('')), 1, 1, ''),
    Referenced_Tab_Name as  [主键表],
    [主键列]=stuff((select ''+[Referenced_Col_Name] from (
        select    FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name,
                Referenced_Tab.Name as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name
        from sys.foreign_keys FK
        inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id
        inner join sys.objects Parent_Tab ON  Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U'
        inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id 
                                            and  Col.parent_column_id = Parent_Col.column_id
        inner join sys.objects Referenced_Tab ON  Col.referenced_object_id = Referenced_Tab.Object_ID and Referenced_Tab.TYPE = 'U'
        inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id 
                                            and  Col.referenced_column_id = Referenced_Col.column_id
    )t where FK_Name=tb.FK_Name  and Parent_Tab_Name = tb.Parent_Tab_Name and Referenced_Tab_Name = tb.Referenced_Tab_Name   for xml path('')), 1, 1, '')
    --as [外键列]
from (
    select    FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name,
            Referenced_Tab.Name as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name
    from sys.foreign_keys FK
    inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id
    inner join sys.objects Parent_Tab ON  Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U'
    inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id 
                                        and  Col.parent_column_id = Parent_Col.column_id
    inner join sys.objects Referenced_Tab ON  Col.referenced_object_id = Referenced_Tab.Object_ID and Referenced_Tab.TYPE = 'U'
    inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id 
                                        and  Col.referenced_column_id = Referenced_Col.column_id
)tb
group by FK_Name,Parent_Tab_Name,Referenced_Tab_Name

作者: fredrickhu   发布时间: 2011-12-02

SQL code
select name  
from  sys.foreign_key_columns f join sys.objects o on f.constraint_object_id=o.object_id 

作者: q465897859   发布时间: 2011-12-02

--查看表所有的约束
sp_helpconstraint stock_serial

作者: gandeng   发布时间: 2011-12-02