Sql 简明教程

SQL - Show Indexes

The SQL Show Index Statement

SHOW INDEX 是用于检索有关表上定义的索引信息的基本 SQL 语句。但是,SHOW INDEX 语句只适用于 MySQL RDBMS,在 SQL Server 中不是有效语句。

The SHOW INDEX is the basic SQL statement to retrieve the information about the indexes that have been defined on a table. However, the SHOW INDEX statement only works on MySQL RDBMS and is not a valid statement in the SQL Server.

从对 MySQL 表上的 SHOW INDEX 语句查询获得的结果集包含索引信息。

The result-set obtained from querying the SHOW INDEX statement on a MySQL table contains the index information.

Syntax

以下是 MySQL 中 SHOW INDEX 语句的语法 −

Following is the syntax of the SHOW INDEX statement in MySQL −

SHOW INDEX FROM table_name;

Example

以下示例演示了 MySQL 中 SHOW INDEX 语句的工作原理。首先,使用下面的 CREATE 查询在 MySQL 数据库中创建一个名为 CUSTOMERS 的表:

Following example demonstrates the working of SHOW INDEX statement in MySQL. First, create a table with the name CUSTOMERS in the MySQL database using the CREATE query below −

CREATE TABLE CUSTOMERS (
   ID INT NOT NULL,
   NAME VARCHAR (20) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (25),
   SALARY DECIMAL (20, 2),
   PRIMARY KEY (ID)
 );

现在让我们使用以下查询向上面创建的表中插入一些值:

Let us now insert some values into the above created table using the following query −

INSERT INTO CUSTOMERS VALUES
(1, 'Ramesh', '32', 'Ahmedabad', 2000),
(2, 'Khilan', '25', 'Delhi', 1500),
(3, 'Kaushik', '23', 'Kota', 2000),
(4, 'Chaitali', '25', 'Mumbai', 6500),
(5, 'Hardik','27', 'Bhopal', 8500),
(6, 'Komal', '22', 'Hyderabad', 9000),
(7, 'Muffy', '24', 'Indore', 5500);

插入数据后,使用以下查询为 CUSTOMERS 表中的 NAME 列创建索引:

Once the data is inserted, create an index for the column NAME in the CUSTOMERS table using the following query −

CREATE INDEX INDEX_NAME ON CUSTOMERS(NAME);

现在,您可以使用以下查询列出在 CUSTOMERS 表上定义的所有索引:

Now, you can list all the indexes that are defined on the CUSTOMERS table using the following query −

SHOW INDEX FROM CUSTOMERS;

Output

执行以上查询后,输出显示如下 −

On executing the above query, the output is displayed as follows −

Table

Non_unique

Key_name

Seq_in_index

Column_name

customers

0

PRIMARY

1

ID

customers

1

index_name

1

NAME

Showing Indexes in SQL Server

在 SQL 服务器中,系统存储过程 sp_helpindex 用于检索有关在表上定义的索引的信息。它将结果作为包含有关每个索引的详细信息的表返回,包括名称、类型和列。

In SQL server, the system stored procedure sp_helpindex is used to retrieve the information about the indexes that have been defined on a table. It returns the result as a table that contains detailed information about each index, including the name, type, and columns.

Syntax

以下是列出 SQL Server 中表上定义的索引的基本语法:

Following is the basic syntax to list indexes defined on a table in SQL Server −

sp_helpindex [ @objname = ] 'name'

此处, [ @objname = ] 'name' 指定正在检索其索引信息的表的名称。索引信息包括:

Here, [ @objname = ] 'name' specifies the name of the table for which the index information is being retrieved. The index information includes −

  1. index_name is the names of the columns that are included in index.

  2. index_description is the brief description of the index such as the type of index (like clustered or non-clustered).

  3. index_keys is the keys that are included in the index.

Example

CREATE INDEX INDEX_NAME on CUSTOMERS(NAME);

现在,让我们使用系统存储过程 sp_helpindex 列出在 CUSTOMERS 表上创建的所有索引,如下所示:

Now, let us list all the indexes that are created on the CUSTOMERS table using the system stored procedure sp_helpindex as shown below −

EXEC sys.sp_helpindex @objname = N'CUSTOMERS';

Output

执行以上查询后,输出显示如下 −

On executing the above query, the output is displayed as follows −

index_name

index_description

index_keys

INDEX_NAME

nonclustered located on PRIMARY

NAME

PK_CUSTOMER_ 3214EC27755869D9

clustered, unique, primary key located on PRIMARY

ID