MYSQL数据表——创建索引

在建立数据表时创建索引索引

注意:mysql语言都不区分大小写

1、创建索引语法

  • 语法:
  •    create table 表名(字段名1 数据类型[约束条件]
                        字段名2 数据类型[约束条件]
                        字段名3 数据类型[约束条件]
     					....
     					[UNIQUE | FULLTEXT | SPATIAL] INDEX | KEY  
     					[索引名](字段名 [(长度)] [ASC | DESC])
     					 );
    

说明
UNIQUE: 可选。表示索引为唯一性索引。
FULLTEXT: 可选。表示索引为全文索引。
SPATIAL: 可选。表示索引为空间索引。
INDEX和KEY: 用于指定字段为索引,两者选择其中之一就可以 ,作用是一样的。
索引名: 可选。给创建的索引取一个新名称。
字段名: 指定索引对应的字段的名称,该字段必须是前面定义好的字段。
长度: 可选。指索引的长度,必须是字符串类型才可以使用。
ASC: 可选。表示升序排列。
DESC: 可选。表示降序排列

2、创建普通索引

  • 在t1表中创建ID字段建立索引
  • create table t1 (id int,
                     name varchar(20),
                     score float,
                     index(id)
                     );
    

3、创建唯一索引[unique[

  • 创建一个名为t2的表,在id字段建立唯一索引,按照升序排列。
  • create table t2 (id int,
                     name varchar(20),
                     score float,
                     unique index unique_id (id)
                     );
    

4.创建全文索引[FULLTEXT]

  • 创建一个名为t3的表,在name字段建立名为fulltext_name的全文索引。
  • create table t3 (id int,
                     name varchar(20),
                     score float,
                     fulltext index fulltext_name(name)
                     );
    

5.创建单列索引[single]

  • 创建一个名为t4的表,在name字段建立名为single_name的单列索引。
  • create table t4 (id int not null
                     name varchar(20) not null,
                     score float,
                     single index single_name(name(20))
                     );
    

6.创建多列索引[multi]

  • 创建一个名为t5的表,在name和id字段建立名为multi的单列索引。
  • create table t5 (id int not null
                     name varchar(20) not null,
                     score float,
                     multi index multi(id,name(20))
                     );
    

7.创建空间索引[spatial]

  • 创建一个名为t6的表,在空间类型为GEOMETRY的字段上创建空间索引。

  • create table t6 (id int,
                     space  GEOMETRY not null,
                     spatial index sp(space)
                     );
    

使用CREATE INDEX在已经存在的数据表中建立索引

创建book表
create table book (
bookid int not null,
bookname varchar(255) not null,
author varchar(255) not null,
info varchar(255) null,
comment varchar(255) null,
publicyear not null);

1.创建语法

  • create [UNIQUE | FULLTEXT | SPATIAL] index 索引名 
    on 表名(字段名 [(长度)] [asc | desc])
    );
    

2.创建普通索引

  • 在book表中的bookid字段建立名为Index_id的普通索引
  • create index index_id on book(bookid);
    

3.创建唯一索引

  • 在book表中的bookid字段建立名为uniqueidx的唯一索引。
  •  create unique index uniqueidx on book(bookid);
    

4.创建单列索引

  • 在book表中的bookid字段建立名为singleidx的唯一索引。

  • create single index singleidx on book(comment);
    

5.创建多列索引

-在book表中的author和info 字段建立名为multi的多列索引。

  • create multi index multi on book(author(20),info(20));
    

6.创建全文索引

  • 在book表中的info 字段建立名为fulltextidx的全文索引。
  • create fulltext index fulltextidx on book(info(20));
    

使用alter table 语句在已经存在的表上建立索引

  • alter table 表名 add [UNIQUE | FULLTEXT | SPATIAL]
    index 索引名 
    on 表名(字段名 [(长度)] [asc | desc])
    );
    

使用方法参照语法即可

猜你喜欢

转载自blog.csdn.net/Nan_Mu_Zi/article/details/87434324