SELECT a.name AS '表',
e.name AS '主键字段'
FROM sysobjects AS a --对象表,结合a.xtype='U'条件,查用户表
LEFT JOIN sysobjects AS b --对象表,结合b.xtype='PK'条件,查主键约束
ON a.id=b.parent_obj
LEFT JOIN sysindexes AS c --索引表,根据(主键)约束名称匹配,查对应字段索引
ON a.id=c.id AND b.name=c.name
LEFT JOIN sysindexkeys AS d --索引中对应键、列的表,根据索引匹配,查字段id
ON a.id=d.id AND c.indid=d.indid
LEFT JOIN syscolumns AS e --字段表,根据字段id匹配,查字段名称
ON a.id=e.id AND d.colid=e.colid
WHERE a.xtype='U'
AND b.xtype='PK'
转载于:https://www.cnblogs.com/tianyongze/p/3227538.html