数据表的索引操作

创建数据表的索引

create table 表名 (字段名 数据类型 [完整性约束条件],

                              字段名 数据类型 [完整性约束条件],

                              ……

                                             字段名 数据类型
                      [unique] | [fulltext] | [spatial] | index | key

                                 [别名] (字段名 1 [长度] [asc | desc]

        Uniqu:唯一索引
        fulltext:全文索引
        spatial:空间索引
         index | key:字段索引的标志
         别名:创建索引的名称,可以用来删除索引

        字段名:索引对应的字段名
        长度:索引的长度,一般针对全文索引
        asc:升序

        desc:降序

创建一个普通索引

create database student;

use student;

create table grade(

扫描二维码关注公众号,回复: 3954802 查看本文章

name varchar(12),

id int(4),

adress varchar(12),

index(id)

);

show create table grade\G;

explain select *from grade where id=1\g;    //查看索引是否被用

在已存在的表上创建索引

一、CERATE[UNIQUE | FULLTEXT | SPATIAL ] INDEX 索引名

ON 表名( 字段名(长度)) [ASC | DESC]

二、ALTER TABLE 表名ADD [UNIQUE | FULLTEXT | SPATIAL ] INDEX

索引名 ( 字段名[(长度)]) [ASC | DESC]

use student;

create table grade_1(

name varchar(12) not null,

id int(4) not null,

adress varchar(12)

);

第一种方法:

create index index_id on grade_1 (id) ;

show create table grade_1\G;

explain select *from grade_1 where id=1\g;

第二种方法:

alter table grade_1 add index name_index(name(12));

show create table grade_1 \g;

create database student;

删除索引:

            alter table 表名 drop index 索引名

            drop index 索引名 on 表名;

 

猜你喜欢

转载自blog.csdn.net/sinat_41803693/article/details/83536139
今日推荐