首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么查看一个数据库中所有的主键,就是各个表中的主键,多谢

2012-01-28 
如何查看一个数据库中所有的主键,就是各个表中的主键,谢谢。如何查看一个数据库中所有的主键,能显示出字段

如何查看一个数据库中所有的主键,就是各个表中的主键,谢谢。
如何查看一个数据库中所有的主键,能显示出字段名和字段类型,这个如何写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))))

热点排行