Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between key and index in Mysql

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about the difference between key and index in Mysql. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Let's take a look at the code first:

ALTER TABLE reportblockdetail ADD KEY taskcode (taskcode) ALTER TABLE reportblockdetail DROP KEY taskcode

Well, this is really easy to get confused.

We may not notice this problem at all when we use MySQL, because in most cases they show the same effect, but we still can't equate them (at least in theory).

The difference between index and key mainly lies in their different starting points. Index is responsible for maintaining the speed of table lookup and operation, while key is responsible for maintaining the integrity of tables.

If there is this confusion, it is probably due to a strange phenomenon in MySQL:

Indexes in MySQL are constrained indexes (that is, creating indexes automatically also creates constraints)

And the creation of constraints in MySQL automatically comes with an index.

Isn't it strange that the two are different, but they are attached to each other when they are created? why do you do that? Because the reason is:

The constraint effect in MySQL is achieved through the index, and the MySQL database determines whether the current column is unique or not by the unique index.

Finally, the summary is as follows:

Constraints include primary key constraints, unique constraints, foreign key constraints, non-null constraints, check constraints (check constraints cannot be used at all in MySQL, but can be established normally), etc.

Indexes include general index, primary key index, unique index, joint index, full-text index, and so on.

Both of them can be realized by building the table, and then the table can be established and deleted through the alter statement. The specific statement is directly above Baidu. I have been tested under the MySQL5.5 and innoDB storage engine.

In theory, MySQL's key and index can not be equated, they are not the same thing, but in practical use, they are basically no difference.

Content extension:

The difference between key and primary key

CREATE TABLE wh_logrecord (logrecord_id int (11) NOT NULL auto_increment, user_name varchar default NULL, operation_time datetime default NULL, logrecord_operation varchar (100) default NULL, PRIMARY KEY (logrecord_id), KEY wh_logrecord_user_name (user_name))

The difference between KEY and INDEX

Note: this part of me is still in the clouds.

KEY is usually synonymous with INDEX. If the keyword attribute PRIMARY KEY is given in the column definition, PRIMARY KEY can also be specified as KEY only. The purpose of this is to be compatible with other database systems. PRIMARY KEY is a unique KEY, at which point all key columns must be defined as NOT NULL. If these columns are not explicitly defined as NOT NULL,MySQL, they should be implicitly defined. There is only one PRIMARY KEY in a table.

The difference between Index and Key in MySQL

Key, namely key value, is a part of relational model theory, such as primary key (Primary Key), foreign key (Foreign Key), etc., which is used for data integrity check and uniqueness constraints. While Index is at the implementation level, for example, any column of the table can be indexed, so when the indexed column is in the Where condition of the SQL statement, it can get fast data location and rapid retrieval. As for Unique Index, it only belongs to one of the Index. Unique Index is established to indicate that the data in this column is not repeatable. Guess that MySQL can make further special optimization to the index of Unique Index type.

Therefore, when designing the table, the Key only needs to be at the model level, and when query optimization is needed, the relevant columns can be indexed.

In addition, in MySQL, for a Primary Key column, MySQL has automatically Unique Index it, so there is no need to index it repeatedly.

These are the differences between key and index in Mysql. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report