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
<br />It has been no more than two years sin...
There are two common ways to make div background ...
The three-dimensional column chart consists of th...
1. Search mysql in the browser to download and in...
Aggregating data from various sources allows the ...
Sometimes some docker containers exit after a per...
Time flies, and in just six days, 2013 will becom...
Make a blank space for Taobao: When you shrink th...
Look at the code first Copy code The code is as fo...
This article shares the specific code of js to re...
background I am learning nodejs recently, and I r...
1. Before configuring the IP address, first use i...
What is DNS The full name of DNS is Domain Name S...
Create an HTML page with an unordered list of at l...
This article records the specific method of insta...