Home  >  Article  >  Database  >  Do mysql primary keys still need to be indexed?

Do mysql primary keys still need to be indexed?

青灯夜游
青灯夜游Original
2020-10-09 14:36:4910001browse

Mysql primary key does not need to be indexed, the primary key has the function of indexing; when creating or setting the primary key, mysql will automatically add a unique index corresponding to the primary key, and no additional additions are required. The database management system will automatically generate a unique index for the primary key, so the primary key is a special index.

Do mysql primary keys still need to be indexed?

When you create or set a primary key, mysql will automatically add a unique index corresponding to the primary key, and no additional additions are required.

If the primary key is not used in the query conditions, you can set your query field as an index. If you want to set an index on the primary key, the primary key has the function of an index.

As shown below in a table created in mysql, you can see that only the primary key PRIMARY KEY is set in the CREATE TABLE statement, and there is no statement to create an index, but the unique primary key index is displayed in the index information. This is Created by mysql itself.

Do mysql primary keys still need to be indexed?

The difference and connection between database primary keys and indexes

1. The primary key must be a unique index, but a unique index is not Must be the primary key.

The primary key is an attribute or attribute group that can uniquely identify a row in the table. A table can only have one primary key, but it can have multiple candidate indexes. Because the primary key can uniquely identify a row of records, it can ensure that no errors will occur when updating and deleting data. Primary keys often form referential integrity constraints with foreign keys to prevent data inconsistencies. The database management system automatically generates a unique index for the primary key, so the primary key is also a special index.

2. The unique index identifies that the index value is unique. A table can have multiple unique indexes, but there can only be one primary key.

3. The primary key column cannot be empty, but the unique index column can be empty.

4. A table can only have one primary key, but can have multiple indexes.

For example, the primary key is equivalent to the page number of a book, and the index is equivalent to the table of contents of the book.

The above is the detailed content of Do mysql primary keys still need to be indexed?. 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