查看表的索引,通过一些工具我们可以很简单的获取到对应的索引信息,但是怎么通过SQL获取呢?这里介绍两种方法

1.通过存储过程sp_helpindex获取

内置存储过程sp_helpindex可获取表对应的索引。
如下(UpgradeInfo是表名):

EXEC sp_helpindex 'UpgradeInfo';
index_name	index_description	index_keys
PK_UpgradeInfo	clustered, unique, primary key located on PRIMARY	Item

2.通过系统视图sys.indexes获取

通过 sys.indexes 视图 可获取表对应的索引。
如下(UpgradeInfo是表名):

SELECT * FROM sys.indexes where object_id = (select object_id  from sys.all_objects where name = 'UpgradeInfo');
object_id	name	index_id	type	type_desc	is_unique	data_space_id	ignore_dup_key	is_primary_key	is_unique_constraint	fill_factor	is_padded	is_disabled	is_hypothetical	allow_row_locks	allow_page_locks	has_filter	filter_definition	compression_delay
741577680	PK_UpgradeInfo	1	1	CLUSTERED	1	1	0	1	0	0	0	0	0	1	1	0		

获取object_id 也可以通过select OBJECT_ID('UpgradeInfo');
所以也可以这样:

SELECT * FROM sys.indexes where object_id = (select OBJECT_ID('UpgradeInfo'));
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐