SQL 我们如何检查表是否有索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2735963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:07:38  来源:igfitidea点击:

How can we check that table have index or not?

sqlsql-serversql-server-2005sql-server-2008

提问by kumar

How can we check that table have index or not ? if have how to find that index for a particular column for a table?

我们如何检查表是否有索引?如果有如何为表的特定列找到该索引?

Regards, kumar

问候,库马尔

回答by Daniel Renshaw

In SQL Server Management Studio you can navigate down the tree to the table you're interested in and open the indexes node. Double clicking any index in that node will then open the properties dialog which will show which columns are included in the index.

在 SQL Server Management Studio 中,您可以向下导航到您感兴趣的表并打开索引节点。双击该节点中的任何索引将打开属性对话框,该对话框将显示索引中包含哪些列。

If you would like to use T-SQL, this might help:

如果您想使用 T-SQL,这可能会有所帮助:

SELECT
    sys.tables.name,
    sys.indexes.name,
    sys.columns.name
FROM sys.indexes
    INNER JOIN sys.tables ON sys.tables.object_id = sys.indexes.object_id
    INNER JOIN sys.index_columns ON sys.index_columns.index_id = sys.indexes.index_id
        AND sys.index_columns.object_id = sys.tables.object_id
    INNER JOIN sys.columns ON sys.columns.column_id = sys.index_columns.column_id
        AND sys.columns.object_id = sys.tables.object_id
WHERE sys.tables.name = 'TABLE NAME HERE'
ORDER BY
    sys.tables.name,
    sys.indexes.name,
    sys.columns.name

回答by Gary

ordering by column name is wrong, you need to order by the position in the index, so order by clause should be tabname, indname and sys.index_columns.index_column_id...

按列名排序是错误的,需要按在索引中的位置排序,所以order by子句应该是tabname、indname和sys.index_columns.index_column_id...

回答by Amit Rai Sharma

Try

尝试

select object_name(object_id),* from sys.indexes 
where object_name(object_id) = 'your table name'