mssql 数据库
(1)查询所有的外键
select fk.name fkname , ftable.name ptablename, cn.name fkcol, rtable.name ftablename from sysforeignkeys
join sysobjects fk on sysforeignkeys.constid = fk.id
join sysobjects ftable on sysforeignkeys.fkeyid = ftable.id
join sysobjects rtable on sysforeignkeys.rkeyid = rtable.id
join syscolumns cn on sysforeignkeys.fkeyid = cn.id and sysforeignkeys.fkey = cn.colid
(2)查询某个表的外键关系
select fk.name fkname , ftable.name ptablename, cn.name fkcol, rtable.name ftablename from sysforeignkeys
join sysobjects fk on sysforeignkeys.constid = fk.id
join sysobjects ftable on sysforeignkeys.fkeyid = ftable.id
join sysobjects rtable on sysforeignkeys.rkeyid = rtable.id
join syscolumns cn on sysforeignkeys.fkeyid = cn.id and sysforeignkeys.fkey = cn.colid
where rtable.name =‘表名’