The difference between primary key, unique key, and unique index

Primary key:
1. You can define one or more columns as the primary key. NULL is not allowed, the primary key can be used as a foreign key, but the unique index cannot;
2. Defining a primary key will automatically create a primary key index, which is a special type of unique index.


Unique key (unique constraint):
1. Unique constraint is used to limit the uniqueness of data on columns that are not constrained by the primary key. It is used as an optional means of accessing a row. The
specified columns are not allowed to have the same value. , allowing null (NULL)
2. Unique constraints can be used to ensure that when a record is added to the base table, one or more column values ​​are unique.

Unique index: You can define one or more columns as a unique index


Index : It is for the convenience of querying data and quickly locating


foreign key: When adding a sub-table record, whether there is a corresponding parent table record. Cannot insert



1. The main key and unique key will automatically create a unique index;
2. The main key can be used as a foreign key, but the unique index cannot;
3. The main key cannot be empty, and the unique index can be;
4. When creating unique constraints and primary keys Clustered and non-clustered indexes can be created when constrained, but by default primary key constraints generate clustered indexes, while unique constraints generate non-clustered indexes
5. Constraints and indexes, the former is used to check the correctness of data, the latter is used to To achieve the optimization of data query, the purpose is different.


Unique constraints are different from unique indexes:
1. Creating a unique constraint will create a Constraint in Oracle, and also create a unique index corresponding to the constraint.
2. Creating a unique index will only create a unique index, not a Constraint.
3. That is to say, the unique constraint is achieved by creating a unique index.

There is also a certain difference between the two when deleting:
1. When deleting a unique constraint, you can only delete the constraint without deleting the corresponding index, so the corresponding column must still be unique.
2. If the unique index is deleted, non-unique values ​​can be inserted.


1. Both the primary key constraint and the unique key constraint will implicitly create a unique index with the same name. When the primary key constraint or the unique key constraint fails, the implicitly created unique index will be deleted;
2. The primary key constraint requires the column value to be non-null and unique Key constraints and unique indexes do not require column values ​​to be non-null;
3. Repeated index creation is not allowed for the same field sequence;


1. The index key value does not store null values, so when the index is empty, the value is not taken from the index
2. In fact, it is only the index Non-NULL values, unique indexes are the same, skip NULL values ​​and do not index
3. If two fields constitute a unique constraint, and one of the fields is empty, then the value of the other field cannot be repeated. In the case of all NULLs, it is still the same as the single-field unique constraint, and will not cause duplication.
4. The unique key can have multiple rows with NULL values.
5. In the innodb engine of mysql oracle, it is allowed in the field of the unique index Multiple null values ​​appear.

Clustered index:
Because the clustered index specifies the physical storage order of data in the table, a table can only contain one clustered index.
But the index can contain multiple columns (composite index), just like a phone book is organized by first and last name.

Non-clustered index
Data is stored in one place, the index is stored in another place, and the index has a pointer to the storage location of the data.
Items in the index are stored in the order of the index key values, while information in the table is stored in another order (this can be dictated by the clustered index).


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326269861&siteId=291194637