如何查找索引对应字段

首先了解一下sql server系统表:
sys.indexes 索引
sys.objects 对象(表...)
sys.columns 字段
sys.index_columns 索引_字段
sys.tables 自定义表
【具体信息不多解释】

 

已知有employeeinfo表,字段名为userid是唯一键(索引名为IX_employeeinfo)
问题1,如何查找索引对应字段?
select * from sys.columns where column_id in(
select column_id from sys.index_columns where index_id in(
select index_id from sys.indexes where object_id in(
select object_id from sys.tables where name='employeeinfo'
) and name='IX_employeeinfo'
) and object_id=object_id('employeeinfo')
) and object_id=object_id('employeeinfo')

反过来,如何查找字段对应索引?
select * from sys.indexes where index_id in(
select index_id from sys.index_columns where column_id in(
select column_id from sys.columns where object_id in(
select object_id from sys.tables where name='employeeinfo'
) and name='userid'
) and object_id=object_id('employeeinfo')
) and object_id=object_id('employeeinfo')


注:
object_id('employeeinfo')--获取对象id

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值