Theme V

Index index database like Xinhua Dictionary sequencer table, it is a structure of the database table values ​​one or more columns are sorted, whose role is to speed up the search data in the table. There are three ways to create an index, as follows:

First, create a table when creating an index

CREATE TABLE table name (Field Name Data Type [integrity constraints],

Field Name Data Type [integrity constraints],

 ...... 

Field Name Data Type [UNIQUE | FULLTEXT | SPATIAL] INDEX | KEY [alias] (1 Field [(length)]) [ASC | DESC])   

 );

 Related explained above syntax as follows:

• UNIQUE: optional parameter represents the unique constraint

• FULLTEXT: optional parameter that indicates the full text of the constraints

• SPATIAL: optional parameter represents the spatial constraints

• INDEX and KEY: used to represent the index fields, you can choose between a • Alias: The name of an optional parameter represents the creation of an index       

• a field name: field name corresponding to the specified index • Length: an optional parameter that indicates the length of the index

• ASC and DESC: an optional parameter, wherein, ASC represents ascending, descending order DESC MySQL represents an index into a variety,

details as follows:

1. The general index 

 

 

 After the above statement is executed, using the CREATE TABLE statement see structure SHOW table, execution results are as follows:

 

 To see whether the index is used, can be viewed EXPLAIN statement, the basic format: EXPLAIN SELECT * FROM t1 WHERE id = 1; 2 unique index.

3. The full-text index

 

 

 4. separate index

 

 5. Multi-column index (note that only the query used in these fields first field, multi-column index will be used)

 6. The spatial index (note that the value of the selected field can not be null, and the table storage engine is MyISAM)  

 

 二、使用CREATE INDEX 语句在已经存在表上创建索引CREATE INDEX的基本格式:CREATE[UNIQUE|FULLTEXT|SPATIAL]INDEX 索引名 ON 表名(字段名[(长度)][ASC|DESC]);/*UNIQUE、FULLTEXT、SPATIAL都是可选参数,分别用于唯一性、全文、空间索引。INDEX用于指明字段为索引。 创建一个book表

 

 创建好数据表book后,开始使用CREATE INDEX语句在已存在的数据表中创建索引,具体如下:1.创建普通索引基本格式:CREATE INDEX index_id ON book(id);执行结果如下:

 

 上述语句执行后,使用SHOW CREATE TABLE语句查看表的结构,执行结果如下:

 

 

2.创建唯一性索引基本格式:CREATE UNIQUE INDEX uniqueidx ON book(id);执行结果如下:

 

 

 3.创建单列索引其基本格式:CREATE INDEX singleidx ON book(name);执行结果如下:

 

4.创建多列索引其基本格式:CREATE INDEX mulitidx ON book(name(8),sex(3));执行结果如下:

 

5.创建全文索引首先删除表book,重建创建表book,在表中的sex字段上创建全文索引。执行结果如下:

 

 

 

 

全文索引的基本格式:CREATE FULLTEXT INDEX fulltextidx ON book(sex);执行结果如下:

 

6.创建空间索引首先创建数据表t7,然后在g字段上创建名称为spatidx的空间索引,其格式CREATE SPATIAL  INDEX  spatidx ON t7(g);

 

 

 三、使用ALTER TABLE 语句在已经表上创建索引在已经存在的表中创建索引,除了用CREATE INDEX语句,还可以用ALTER TABLE 语句,其语法格式为ALTER TABLE 表名 ADD[UNIQUE|FULLTAXT|SPATIAL]INDEX 索引名(字段名[(长度)][ASC|DESC])/*UNIQUE、FULLTEXT、SPATIAL都是可选参数,分被用于唯一索引、全文索引、空间索引,ADD表示向表中添加字段*/

 

 

 1.创建普通索引在表中的id字段上创建名称为index_id的普通索引,基本格式:ALTER TABLE book ADD INDEX index_id(id);执行结果如下:

 

 2.创建唯一性索引在表中的id字段上建立一个名称为uniqueidx的唯一性索引,基本格式:ALTER TABLE book ADD UNIQUE uniqueidx(id);执行结果如下:

 

 

3.创建单列索引在表中的name字段上建立一个名称singleidx的单列索引,基本格式:ALTER TABLE book ADD INDEX singleidx(name(20));执行结果如下:

 

 

 4.创建多列索引在表中的score和sex字段上建立一个名称为multidx的多列索引,基本格式:ALTER TABLE book ADD INDEX multidx(score(4),sex(4));执行结果如下:

 

 

 5.创建全文索引删除表book,重新创建表book,在表中的name字段上创建全文索引,格式ALTER TABLE book ADD FULLTEXT INDEX fulltextidx(name);执行结果如下:

 

 

6.创建空间索引创建表t8,在表中space字段上创建名称为spatidx的空间索引,基本格式ALTER TABLE t8ADD SPATIAL INDEX spatidx(space);执行结果如下: 

 

 

 删除索引删除索引的方式有两种,具体如下:1.使用ALTER TABLE 删除索引基本格式:ALTER TABLE 表名 DROP INDEX 字段名删除表book中名称为fulltextidx的全文索引,执行结果如下:

 

 2.使用DROP INDEX 删除索引基本格式:DROP INDEX 索引名 ON 表名;执行结果如下:

 

Guess you like

Origin www.cnblogs.com/13912475703q/p/12048871.html