日期:2014-05-18  浏览次数:20824 次

如何查看一个数据库中所有的主键,就是各个表中的主键,谢谢。
如何查看一个数据库中所有的主键,能显示出字段名和字段类型,这个如何写SQL语句,谢谢!

------解决方案--------------------
查询一个表中所有的主键
select * from sysobjects where xtype= 'PK ' and parent_obj=object_id( '表名 ')
查询一个表中的所有列
select * from syscolumns where id=object_id( '表名 ')
------解决方案--------------------
select [主键]=a.name,[表名]=b.name --如果同一个表名有两个以上的列为复合主键
from syscolumns a inner join sysobjects b on a.id=b.id

where
a.name in (select name from syscolumns where id=a.id and colid in(
select colid from sysindexkeys where a.id=id and indid in(
select indid from sysindexes where a.id=id and name in(
select name from sysobjects where xtype= 'PK ' and parent_obj=a.id))))