sqlserver查看数据库下所有index,每条index占一行,有index名,表名,index包含的列名,排除主键索引
时间: 2024-01-25 08:13:54 浏览: 28
可以使用以下SQL语句来查看SQL Server数据库下所有的索引(index),每条index占一行,包括索引名、表名、索引包含的列名,排除主键索引:
```
SELECT
i.name AS index_name,
t.name AS table_name,
c.name AS column_name
FROM
sys.indexes i
INNER JOIN
sys.tables t ON i.object_id = t.object_id
INNER JOIN
sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN
sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE
i.is_primary_key = 0 -- 排除主键
ORDER BY
i.name, t.name, ic.key_ordinal;
```
该语句会返回每个索引的索引名、表名、索引包含的列名等信息。其中,`sys.indexes` 存储索引的信息,`sys.tables` 存储表的信息,`sys.index_columns` 存储索引包含的列的信息,`sys.columns` 存储表中列的信息。在查询中,使用了 `INNER JOIN` 连接这些表,通过关联每个表的 object_id 和 column_id 等字段,可以得到完整的索引信息。
注意,上述查询结果中排除了主键索引,如果需要查看主键索引,可以将 `i.is_primary_key = 0` 修改为 `i.is_primary_key = 1`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)