Home >Database >Mysql Tutorial >What is the difference between unique index and primary key

What is the difference between unique index and primary key

青灯夜游
青灯夜游Original
2021-07-23 17:02:1128232browse

Difference: 1. The primary key is a constraint, and the unique index is an index; 2. After the primary key is created, it must contain a unique index, and the unique index is not necessarily the primary key; 3. The unique index column is allowed Null values ​​are not allowed for primary keys; 4. Primary keys can be referenced as foreign keys by other tables, but unique indexes cannot; 5. A table can only create one primary key, but multiple unique indexes can be created.

What is the difference between unique index and primary key

The operating environment of this tutorial: windows10 system, mysql8 version, Dell G3 computer.

PRIMARY KEY's full name is "primary key constraint", which is the most frequently used constraint in MySQL. Under normal circumstances, in order to facilitate the DBMS to find records in the table faster, a primary key will be set in the table.

Primary keys are divided into single-field primary keys and multi-field joint primary keys. This section will explain the creation, modification and deletion of these two primary key constraints respectively.

The following points should be noted when using primary keys:

  • Each table can only define one primary key.

  • The primary key value must uniquely identify each row in the table and cannot be NULL, that is, there cannot be two rows of data with the same primary key value in the table. This is the principle of uniqueness.

  • A field name can only appear once in the joint primary key field table.

  • The joint primary key cannot contain unnecessary redundant fields. When a field in the joint primary key is deleted, if the primary key composed of the remaining fields still satisfies the uniqueness principle, then the joint primary key is incorrect. This is the principle of minimization.

The difference between primary key and unique index

1. The primary key is a constraint, and the unique index is an index , the two are essentially different.

2. After the primary key is created, it must contain a unique index. The unique index is not necessarily the primary key.

3. The unique index column allows null values, but the primary key column does not allow null values.

4. The primary key can be referenced as a foreign key by other tables, but the unique index cannot.

5. A table can only create one primary key at most, but multiple unique indexes can be created.

6. Primary keys are more suitable for unique identifiers that are not easy to change, such as auto-increment columns, ID numbers, etc.

7. In RBO mode, the execution plan priority of the primary key is higher than that of the unique index. Both can improve the speed of queries.

(Recommended tutorial: mysql video tutorial)

The above is the detailed content of What is the difference between unique index and primary key. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn