Sql 简明教程

SQL - Unique Indexes

SQL Unique Indexes

SQL Unique Index 确保表索引列中的任何两行没有相同的值(不允许重复值)。

The SQL Unique Index ensures that no two rows in the indexed columns of a table have the same values (no duplicate values allowed).

可以使用 SQL 中的 CREATE UNIQUE INDEX 语句在表的某一列或多列上创建一个唯一索引。

A unique index can be created on one or more columns of a table using the CREATE UNIQUE INDEX statement in SQL.

在表上创建唯一索引之前,需要注意以下几点 -

Following are the points to be noted before creating a Unique Index on a table −

  1. If the unique index is only created on a single column, the rows in that column will be unique.

  2. If a single column contains NULL in multiple rows, we cannot create a unique index on that column.

  3. If the unique index is created on multiple columns, the combination of rows in these columns will be unique.

  4. We cannot create a unique index on multiple columns if the combination of columns contains NULL in more than one row.

Syntax

以下是 SQL 中创建 UNIQUE INDEX 的语法 -

Following is the syntax for creating a UNIQUE INDEX in SQL −

CREATE UNIQUE INDEX index_name
ON table_name (column1, column2, ..., columnN);

在此,

Here,

  1. index_name is the name of the index that you want to create.

  2. table_name is the name of the table on which you want to create the index.

  3. (column1, column2, …​., columnN) are the names of one or more columns on which the unique index is being created.

Example

首先,让我们使用以下查询创建一个名为 CUSTOMERS 的表:

First of all, let us create a table named CUSTOMERS using the following query −

CREATE TABLE CUSTOMERS (
   ID INT NOT NULL,
   NAME VARCHAR(15) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS VARCHAR(25),
   SALARY DECIMAL(10, 4),
   PRIMARY KEY(ID)
);

使用以下查询,向上面创建的表中插入一些值:

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', '26', 'Mumbai', 6500),
(5, 'Hardik','27', 'Bhopal', 8500),
(6, 'Komal', '22', 'Hyderabad', 9000),
(7, 'Muffy', '24', 'Indore', 5500);

一旦创建了表,让我们使用以下查询为 CUSTOMERS 表中名为 SALARY 的列创建一个唯一索引:

Once the table is created, let us create a unique index for the column named SALARY in the CUSTOMERS table using the following query −

CREATE UNIQUE INDEX UNIQUE_ID ON CUSTOMERS (SALARY);

但是,当我们执行上述查询时,会获得如下输出:

But, when we execute the above query, the output is obtained as follows −

ERROR 1062 (23000): Duplicate entry '2000.00' for key 'customers.UNIQUE_ID'

由于无法在 SALARY 列上创建唯一索引(因为有重复值),因此让我们使用以下查询在相同表的 NAME 列上创建 Unique Index

Since a unique index could not be created on SALARY column (due to duplicate values), let us create Unique Index on the NAME column of the same table, using the following query −

CREATE UNIQUE INDEX UNIQUE_ID ON CUSTOMERS (NAME);

Output

当我们执行上述查询时,输出将获得如下:

When we execute the above query, the output is obtained as follows −

Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

Verification

让我们使用以下查询验证是否为 NAME 列创建了唯一索引:

Let’s verify whether the unique index for the column NAME is created or not using the following query −

SHOW INDEX FROM CUSTOMERS;

在下面查看输出时,可以在索引列表中找到与 ID(主键)一起出现的 NAME 列。

As you observe the output below, you can find the column NAME along with the ID (PRIMARY KEY) in the list of indexes.

Table

Non_unique

Key_name

Seq_in_index

Column_name

customers

0

PRIMARY

1

ID

customers

0

UNIQUE_ID

1

NAME

Updating with Duplicate Values

如果我们尝试用重复值更新具有唯一索引的列,数据库引擎将生成错误。

If we try to update the columns that have unique index with duplicate values, the database engine generates an error.

Example

假设有之前创建的 CUSTOMERS 表,并使用以下查询在名为 ADDRESS 的列上创建唯一索引:

Assume the previously created CUSTOMERS table and create a unique index on the column named ADDRESS using the following query −

CREATE UNIQUE INDEX ADD_UNIQUE_INDEX ON CUSTOMERS(ADDRESS);

现在,让我们使用以下查询将名为 ADDRESS 的列中的值更新为重复值(已存在的数据):

Now, let us update the value in the column named ADDRESS with a duplicate (already existing data) value using the following query −

UPDATE CUSTOMERS SET ADDRESS = 'Mumbai' WHERE ADDRESS = 'Delhi';

Output

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

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

ERROR 1062 (23000): Duplicate entry 'Mumbai' for key 'customers.ADD_UNIQUE_INDEX'

Creating a unique index on Multiple Fields

我们还可以使用 CREATE UNIQUE INDEX 语句在表的多个字段或列上创建唯一索引。为此,只需将列的名称(您需要在上面创建索引)传递给查询即可。

We can also create a unique index on multiple fields or columns of a table using the CREATE UNIQUE INDEX statement. To do so, you just need to pass the name of the columns (you need to create the index on) to the query.

Example

我们不必创建新表,不妨考虑一下之前创建的 CUSTOMERS 表。我们将使用以下查询对 NAME 和 AGE 列创建唯一索引 −

Instead of creating a new table, let us consider the previously created CUSTOMERS table. We will create a unique index on the columns NAME and AGE using the following query −

CREATE UNIQUE INDEX MUL_UNIQUE_INDEX ON CUSTOMERS(NAME, AGE);

Output

当我们执行上述查询时,输出将获得如下:

When we execute the above query, the output is obtained as follows −

Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0.

Verification

现在,让我们使用以下查询列出创建于 CUSTOMERS 表上的所有索引 −

Now, let us list all the indexes that are created on the CUSTOMERS table using the following query −

SHOW INDEX FROM CUSTOMERS;

如你观察到的那样,你可以在索引列表中找到列名 NAME 和 AGE,以及 ID(主键)。

As you observe you can find the column names NAME, and AGE along with the ID (PRIMARY KEY) in the list of indexes.

Table

Non_unique

Key_name

Seq_in_index

Column_name

customers

0

PRIMARY

1

ID

customers

0

MUL_UNIQUE_INDEX

1

NAME

customers

0

MUL_UNIQUE_INDEX

2

AGE