Let's look at the code first: ALTER TABLE reportblockdetail ADD KEY taskcode (taskcode) ALTER TABLE reportblockdetail DROP KEY taskcode Well, this is indeed where it gets confusing. We may not pay attention to this problem when using MySQL, because in most cases they show similar effects, but they still cannot be equated (at least in theory) The difference between an index and a key lies in their different starting points. The index is responsible for maintaining the search and operation speed of the table, while the key is responsible for maintaining the integrity of the table. If you have this confusion, it is probably due to a strange phenomenon in MySQL:
It's strange, right? They are two different things, but when they are created, they are attached to each other. Why do they do this? Because the reasons are:
Finally, let’s summarize:
In theory, MySQL key and index cannot be equated, they are not the same thing, but in actual use, there is basically no difference between them. Content extension: Difference between key and primary key CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_name varchar(100) default NULL, operation_time datetime default NULL, logrecord_operation varchar(100) default NULL, PRIMARY KEY (logrecord_id), KEY wh_logrecord_user_name (user_name) ) Difference between KEY and INDEX Note: I am still confused about this part. The difference between Index and Key in MySQL Key is the key value, which is part of the relational model theory. For example, there are primary keys and foreign keys, which are used for data integrity checks and uniqueness constraints. Index is at the implementation level. For example, you can create an index for any column in a table. Then, when the indexed column is in the Where condition in the SQL statement, you can quickly locate the data and retrieve it quickly. As for Unique Index, it is just a type of Index. The establishment of a Unique Index indicates that the data in this column cannot be repeated. I guess MySQL can make further special optimizations for Unique Index type indexes. Therefore, when designing a table, the Key only needs to be at the model level, and when query optimization is required, indexes can be created for the relevant columns. In addition, in MySQL, for a Primary Key column, MySQL has automatically created a Unique Index for it, so there is no need to create an index on it again. The above is the detailed content of the difference between key and index in MySQL. For more information about the difference between key and index in MySQL, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to use the realip module in Nginx basic learning
>>: How to install elasticsearch and kibana in docker
Table of contents background Understanding compos...
Source: https://medium.com/better-programming, au...
There are many ways to generate a global ID. Here...
One sentence to introduce HOC What is a higher-or...
Table of contents 1. From father to son 2. From s...
Table of contents Preface 1. Set the prototype on...
Preface These principles are summarized from actu...
CSS Layout - position Property The position attri...
Directly post code and examples #Write comments w...
Three MySQL instance processes are started on one...
1. Create a user: Order: CREATE USER 'usernam...
The specific code of JavaScript date effects is f...
The company's service uses docker, and the di...
If you have just come into contact with Linux, th...
Table of contents Question: Case (1) fork before ...