欢迎访问艾立兹站 Aliz- 专注建站教程与技术分享
您当前的位置:首页 > 建站教程 > 数据库

SQL server查看各表的索引(sql语句大全)

时间:2023-12-28 15:26:31  来源:未知  作者:阿里技术  阅读:
1、要查看 SQL Server 数据库中的索引,可以使用如下 SQL 语句:
SELECT
    TableName = t.name,
    IndexName = ind.name,
    ind.type_desc,
    ind.is_unique,
    ind.is_primary_key,
    ColumnNames =
        stuff(
            (
                select ', ' + col.name +
                case when ic.is_descending_key = 1 then ' desc' else '' end
                from sys.index_columns ic
                    inner join sys.columns col on ic.object_id = col.object_id and ic.column_id = col.column_id
                where
                    ic.object_id = ind.object_id
                    and ic.index_id = ind.index_id
                order by
                    ic.index_column_id
                for xml path ('')
            ),1,2,''
        )
FROM
    sys.indexes ind
    INNER JOIN sys.tables t ON ind.object_id = t.object_id
    INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE
    ind.name IS NOT NULL
ORDER BY
    t.name, ind.name;

这条 SQL 语句查询了系统元数据表,包含了以下信息:

- 表名

- 索引名

本文配套模板、静态源码可前往艾立兹素材库alisucai.com下载

发表评论 共有0条评论
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
公众号二维码

扫码关注公众号
获取全套技术教程