--SQL SERVER 2000中各表外键名,主键名的获取
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'
/*
--以下为查询SQL SERVER 2000中自带库PUBS的结果
外键表ID 外键表名称 外键列ID 外键列名 主键表ID 主键表名 主键列ID 主键列名 级联更新 级联删除
---------- ------------ --------- --------- ----------- ----------- -------- --------- -------- ---------
2121058592 titles 4 pub_id 2057058364 publishers 1 pub_id 0 0
53575229 titleauthor 1 au_id 1977058079 authors 1 au_id 0 0
53575229 titleauthor 2 title_id 2121058592 titles 1 title_id 0 0
149575571 sales 1 stor_id 117575457 stores 1 stor_id 0 0
149575571 sales 6 title_id 2121058592 titles 1 title_id 0 0
213575799 roysched 1 title_id 2121058592 titles 1 title_id 0 0
245575913 discounts 2 stor_id 117575457 stores 1 stor_id 0 0
357576312 pub_info 1 pub_id 2057058364 publishers 1 pub_id 0 0
405576483 employee 5 job_id 277576027 jobs 1 job_id 0 0
405576483 employee 7 pub_id 2057058364 publishers 1 pub_id 0 0
(所影响的行数为 10 行)
*/