Database | Detailed index

I. Overview of the index

  • Engine is a data structure for storing data records to find a fast, fast access to specific information in the database table. (Similar to the directory book of the role)
  • In fact, the index is also a table that holds the primary key and index fields, and point to record the entity table.

Second, the advantages of index

MySQL to improve the retrieval speed .

Third, the shortcomings of the index

  • ① reduced update rate table
    such as table INSERT, UPDATE, and DELETE. Because when you update table, MySQL is not only to save the data, but also save about index file.
  • ② index files take up disk space

Fourth, the index type

  • Ordinary index (a conventional index) ---- separate index
    one data table may have a plurality of conventional indexes. If you do not explicitly specify the type of index, the index is what we call a regular index finger.

  • Composite index (composite index)
    Composite Index comply with " the most left-prefix " principle, that the use of the composite index of the first field in the query, the index will be used. Therefore, in the composite index order of the index column is essential.

  • The only index
    providing unique constraint. A table can have multiple unique index.

  • Primary key index
    is also called the primary key uniqueness constraint provided. A table can have only one primary key . Are marked as automatic growth of the field must be the primary key, but not necessarily the primary key is automatically grow. Usually the primary key fields defined on a meaningless (eg: No), the data type is preferably the primary key value.

  • Full-text index
    to improve query efficiency of full-text search.

Fifth, create an index

CREATE TABLE table_name[col_name data type]
[unique|fulltext][index|key][index_name](col_name[length])[asc|desc]

  • unique | fulltext is optional, represent a unique index, full-text indexing

  • index and key are synonymous, both have the same effect, to create the specified index

  • col_name create fields column index is desired, the column must be selected from a plurality of columns of the data defined in the table

  • index_name specifies the name of the index, is optional, if not specified, the default value for the index col_name

  • optional length parameter indicating the length of the index, only the string type field to specify the length of the index

  • asc or desc specifies ascending or descending index value storage

Sixth, the index is created

1, the general index

a, direct method

CREATE INDEX 索引名 ON 表名(username(length));

NOTE: If CHAR, VARCHAR type, length can be less than the actual length of the field; if BLOB and TEXT types, must be specified length.
Add indexing b, modified table structure

ALTER TABLE 表名 ADD INDEX 索引名(col_name);

c, create a table while creating the index
Here Insert Picture Description

2, a composite index

The establishment of a composite index created on multiple fields.

a, direct method

create index 索引名 on 表名(字段1,字段2,...);

b, using a modified embodiment of the table structure

ALTER TABLE 表名 add index 索引名(字段1,字段2,...);

3, a unique index

a, direct method

# 单个
CREATE UNIQUE INDEX 索引名 ON table_name(col_name);

# 多个
CREATE UNIQUE INDEX 索引名 on table_name(col_name1,col_name2...);

# 注意单个、多个指的是字段

b, modify table configuration method

# 单个
ALTER TABLE table_name ADD UNIQUE index index_name(col_name);

# 多个
ALTER TABLE table_name ADD UNIQUE index index_name(col_name1,col_name2...);

# 注意单个、多个指的是字段

Creating index c, create a table at the same time

CREATE TABLE news (
    id int(11) NOT NULL AUTO_INCREMENT ,
    title varchar(255)  NOT NULL ,
    content varchar(255)  NULL ,
    PRIMARY KEY (`id`),
    UNIQUE index_name_unique(title)
)

4, the primary key index

Create an index a, create a table at the same time


CREATE TABLE news (
    id int(11) NOT NULL AUTO_INCREMENT ,
    title varchar(255)  NOT NULL ,
    content varchar(255)  NULL ,
    PRIMARY KEY (`id`)
)

b, and then create a table to create an index

ALTER TABLE 表名 ADD primary key(col_name);

C, using the ALTER increase the master key
Note: the master key can only act on a column, adding the primary key index, the need to ensure that the default primary key is not empty (NOT NULL)
Example:

ALTER TABLE testalter_tbl MODIFY i INT NOT NULL;
ALTER TABLE testalter_tbl ADD PRIMARY KEY (i);

5, full-text indexing

a, direct method

CREATE FULLTEXT INDEX 索引名 ON 表名(col_name);

b, create a table at the same time creating an index

CREATE TABLE table (
	id int(11) NOT NULL AUTO_INCREMENT ,
	title char(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
	content  text CHARACTER SET utf8 COLLATE utf8_general_ci NULL ,
	PRIMARY KEY (`id`),
	FULLTEXT (content));

Structure c, changes to the table

ALTER TABLE 表名 ADD FULLTEXT 索引名(col_name);

Note: The default MySQL does not support Chinese full-text search!

6, add an index field name in brackets can be more

Seven, delete, and query indexes

1, remove the index
DROP INDEX 索引名 ON 表名;

or

ALTER TABLE 表名 DROP INDEX 索引名;
2, query index
show indexes from 表名;

or

show keys from 表名;

Guess you like

Origin blog.csdn.net/Geekst/article/details/90550450