一佳一

记录像1+1一样简洁的代码

导航

SQL查询语句获取主键和外键

Posted on 2011-11-18 10:42  一佳一  阅读(4740)  评论(0编辑  收藏  举报
SELECT 
外键表ID   = b.fkeyid ,
外键表名称 = object_name(b.fkeyid) ,
外键列ID   = b.fkey ,
外键列名   = (SELECT name FROM syscolumns WHERE colid = b.fkey AND id = b.fkeyid) ,
主键表ID   = b.rkeyid ,
主键表名   = object_name(b.rkeyid) ,
主键列ID   = b.rkey ,
主键列名   = (SELECT name FROM syscolumns WHERE colid = b.rkey AND id = b.rkeyid) ,
级联更新   = 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'