The difference between key, primary key, unique key and index in mysql [transfer]

First, the 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)
)

 
Analysis:
KEY wh_logrecord_user_name (user_name)
This table's user_namefields and wh_logrecord_user_nametable user_namefields establish foreign keys. The
parentheses are the corresponding tables for establishing foreign keys. The parentheses are the corresponding fields.
Similar to KEY user (userid).
Of course, the keys may not all be foreign keys.

 
Summary:
Key is an index constraint, and the constraint index on the fields in the table is created through primary foreign unique. Commonly used foreign key, foreign key association.

 
KEY forum (status, type, displayorder) # is a multi-column index (key)
KEY tid (tid) # is a single-column index (key).

 
When building a table:
Does KEY forum (status, type, displayorder) select * from table group by status, type, displayorder automatically use this index,
and is this index useful when select * from table group by status?

 
The purpose of the key: mainly used to speed up the query.

 

Second, the difference between KEY and INDEX

Comment: I am still in the fog in this part.
KEYUsually INDEXsynonymous. If the keyword attribute is given PRIMARY KEYin 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 this time, all key columns must be defined as NOT NULL. If these columns are not explicitly defined as NOT NULL, MySQL should implicitly define these columns. There is only one PRIMARY KEY for a table.

 
The difference between Index and Key in MySQL

 
Key is the key value, which is a part of the relational model theory, such as primary key (Foreign Key), foreign key (Foreign Key), etc., used for data integrity check and uniqueness constraints. Index is at the implementation level. For example, you can index any column of the table. When the indexed column is in the Where condition in the SQL statement, you can get fast data positioning and fast retrieval. As for the Unique Index, it is only one of the Index. The Unique Index is established to indicate that the data in this column is not repeatable. Guess that MySQL can do special optimization on the Unique Index type index.

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

 
In addition, in MySQL, for a column of Primary Key, MySQL has automatically established a Unique Index for it, there is no need to repeat the index on it.

 
An explanation found:

 

Note that “primary” is called PRIMARY KEY not INDEX.
KEY is something on the logical level, describes your table and database design (i.e. enforces referential integrity…)
INDEX is something on the physical level, helps improve access time for table operations.
Behind every PK there is (usually) unique index created (automatically).

 

Third, what is the difference between mysql UNIQUE KEYandPRIMARY KEY

1. One or more columns of Primary key must be NOT NULL. If the column is NULL, when PRIMARY KEY is added, the column is automatically changed to NOT NULL. UNIQUE KEY does not have this requirement for columns

 
2. A table can only have one PRIMARY KEY, but can have multiple UNIQUE KEY

 
3. The primary key and unique key constraints are implemented by referring to the index. If the inserted values ​​are NULL, according to the principle of the index, all NULL values ​​are not recorded on the index, so when inserting all NULL values, there can be duplicates. Others cannot insert duplicate values.

alter table t add constraint uk_t_1 unique (a,b);
insert into t (a ,b ) values (null,1);    # 不能重复
insert into t (a ,b ) values (null,null);#可以重复

 
Four, use UNIQUE KEY

CREATE TABLE `secure_vulnerability_warning` (
  `id` int(10) NOT NULL auto_increment,
  `date` date NOT NULL,
  `type` varchar(100) NOT NULL,
  `sub_type` varchar(100) NOT NULL,
  `domain_name` varchar(128) NOT NULL,
  `url` text NOT NULL,
  `parameters` text NOT NULL,
  `hash` varchar(100) NOT NULL,
  `deal` int(1) NOT NULL,
  `deal_date` date default NULL,
  `remark` text,
  `last_push_time` datetime default NULL,
  `push_times` int(11) default '1',
  `first_set_ok_time` datetime default NULL,
  `last_set_ok_time` datetime default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `date` (`date`,`hash`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8

The purpose of UNIQUE KEY: It is mainly used to prevent duplication when data is inserted.

1. When creating a table

CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
UNIQUE (Id_P)
)

If you need to name UNIQUE constraints and define UNIQUE constraints for multiple columns, use the following SQL syntax:

CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)
)

2. When the table has been created, to create a UNIQUE constraint in the "Id_P" column, use the following SQL:

ALTER TABLE Persons
ADD UNIQUE (Id_P)

To name UNIQUE constraints and define UNIQUE constraints for multiple columns, use the following SQL syntax:

ALTER TABLE Persons
ADD CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)

3. Revoke the UNIQUE constraint

To undo the UNIQUE constraint, use the following SQL:

ALTER TABLE Persons
DROP INDEX uc_PersonID

Article transfer from: https://www.iteye.com/blog/zccst-1697043

Guess you like

Origin www.cnblogs.com/KillBugMe/p/12681488.html