linux mysql character set index, primary key, etc.

1. The character set mysql defaults to use the character set Latin1
* show variables like "%char%"; //View the default character encoding
* set character_set_database=utf8; //Modify the character encoding temporarily
* vim /etc/my.cnf character_set_server=utf8 / /Permanently modify the character encoding
* alter table table name character set=utf8; //Modify the character encoding of the table
* Now the utf8mb4 format is used by default, because utf8 has a bug

2. Index
* To improve the work efficiency of select, you only need to set it clearly, without subsequent operations
* 1) Single-column index: ordinary index, unique index, primary key, full-text index.
* 2) Multi-column/combined index.

3. Ordinary index
* index
* create table table name (field data type (), ... field data type () index index name (field [length])); //Add index when creating table
* Add index for a field, length is the index field prefix, when the index field value is too long, you can set
* show index from table name\G; //view index
* alter table table name add index index name (field); //add index to table
* create index index Name on table name (field); //Add an index to the table
* drop index Index name on table name; //Delete index
* There is no restriction on ordinary indexes and can be created freely

4. Unique index
* unique data is not allowed to repeat
* create table table name (field data type (), ... field data type () unique index index name (field [length]));

5. Primary key
* Special unique index primary key
* The primary key field cannot be repeated and cannot be empty (it can be used for ID)
* There can only be one primary key, so it is not necessary to name it like an index
* alter table table name add primary key (field); //Add primary key
* create table table name (field data type,...,primary key (field)); //Add primary key
* show index from table name; //View primary key
* alter table table name drop primary key; // Delete Primary key

6. Full text index
*fulltext
* is mainly used for the field data types as text and varchar, such as the introduction of novel websites.

7. Features of single-column index
* Speed ​​up query rate
* Reduce disk IO cost
* Speed ​​up the connection between tables

8. Multi-column/combined index suppose there are two fields ab, create index where a=1 and b=1 when mysql query, only one index will be used, so first query a=1 and then query b=1, you should use combination Index (a, b) 9. The left-most prefix principle combined index (a, b, c) a=1 or a=1 and b=1 or a=1 and b=1 and c=1a=1 b=1 c =110. Reasons for index failure
* The conditions include or
* Use like plus %, when fuzzy query
* Full table scan is faster than using index query
* Index is part of the expression
* The field type is a string, and the data is not added''

Guess you like

Origin blog.csdn.net/qq_39109226/article/details/110477624