General index & unique index primary key index & & & composite index full-text index

General index

This is the most basic index, it does not have any restrictions. It has created the following ways:

(1) directly create an index

CREATE INDEX index_name ON table(column(length))  

Adding an index (2) modify the table structure

ALTER TABLE table_name ADD INDEX index_name ON (column(length))

(3) create a table when creating an index at the same time

CREATE TABLE `table` (
    `id` int(11) NOT NULL AUTO_INCREMENT ,
    `title` char(255) CHARACTER NOT NULL ,
    `content` text CHARACTER NULL ,
    `time` int(10) NULL DEFAULT NULL ,
    PRIMARY KEY (`id`),
    INDEX index_name (title(length))
)

The only index

Similar to the previous general index, it is different: the value of the index columns must be unique, but allow free value. If it is a combination of the index, the column value must be unique. It has created the following ways:

(1) create unique index

CREATE UNIQUE INDEX indexName ON table(column(length))

(2) modify the table structure

ALTER TABLE table_name ADD UNIQUE indexName ON (column(length))

(3) when creating the table specified directly

CREATE TABLE `table` (
    `id` int(11) NOT NULL AUTO_INCREMENT ,
    `title` char(255) CHARACTER NOT NULL ,
    `content` text CHARACTER NULL ,
    `time` int(10) NULL DEFAULT NULL ,
    UNIQUE indexName (title(length))
);

Primary key index

It is a special unique index, a table can have only one primary key, does not allow nulls. Usually at the same time create a primary key index when construction of the table:

CREATE TABLE `table` (
    `id` int(11) NOT NULL AUTO_INCREMENT ,
    `title` char(255) NOT NULL ,
    PRIMARY KEY (`id`)
);

Composite index

It refers to the index created on multiple fields, only use the first field when creating an index in a query, the index will be used. The most left-prefix following the collection when using combination index

ALTER TABLE `table` ADD INDEX name_city_age (name,city,age); 

Full-text index

The main keywords used to find the text, rather than directly compared with the value of the index. fulltext index with other indexes very different, it is more like a search engine, rather than simply matching the parameters of where the statement. fulltext index with the match against operational use, rather than a general statement where plus like. It can create table, alter table, create index to use, but currently only char, varchar, text can be created on the column full-text index. It is worth mentioning that the large amount of data when the data will now be placed in a global index is not in the table, and then use the CREATE index create fulltext index than the first for a table and then writes the data to establish fulltext the much faster.

CREATE TABLE `table` (
    `id` int(11) NOT NULL AUTO_INCREMENT ,
    `title` char(255) CHARACTER NOT NULL ,
    `content` text CHARACTER NULL ,
    `time` int(10) NULL DEFAULT NULL ,
    PRIMARY KEY (`id`),
    FULLTEXT (content)
);

Reference blog https://www.cnblogs.com/nov5026/p/11210078.html

Published 44 original articles · won praise 13 · views 2987

Guess you like

Origin blog.csdn.net/Hanmin_hm/article/details/104974405