SQL基础语法—create index语句

1 create index语句介绍

create index语句用来在表中创建索引。索引的作用提高数据库查询的效率,这个作用对用户来说是透明的,其作用只是对MySQL引擎来说的。

Syntax:
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
    [index_type]
    ON tbl_name (index_col_name,...)
    [index_option]
    [algorithm_option | lock_option] ...

index_col_name:
    col_name [(length)] [ASC | DESC]

index_option:
    KEY_BLOCK_SIZE [=] value
  | index_type
  | WITH PARSER parser_name
  | COMMENT 'string'
  | {VISIBLE | INVISIBLE}

index_type:
    USING {BTREE | HASH}

algorithm_option:
    ALGORITHM [=] {DEFAULT|INPLACE|COPY}

lock_option:
    LOCK [=] {DEFAULT|NONE|SHARED|EXCLUSIVE}

index_col_name可以包含一个字段,也可以包含多个字段(逗号隔开),如果包含多个字段,则表名此索引是复合索引;

unique index代表索引中的值不能有重复;

fulltex index只能创建在innodbmyisam存储引擎的char varchar text字段上;

index可以创建在包含NULL值的字段上;

key_block_size=value是在myisam存储引擎的表上指定索引键的block大小;

index_type代表创建索引的类型;

comment 'string'代表可以为索引添加最长1024的注释:

CREATE TABLE t1 (id INT);
CREATE INDEX id_index ON t1 (id) COMMENT 'MERGE_THRESHOLD=40';

2 create view语句应用

mysql> create index idx_st_sname on students(sname); ##创建普通索引
mysql> create index idx_st_union on students(sname,sex); ##创建复合索引
mysql> create unique index idx_st_sid on students(sid); ##创建唯一索引
mysql> insert into students values(1,‘eee’,0); ##插入重复数据失败
ERROR 1062 (23000): Duplicate entry '1' for key 'idx_st_sid'

猜你喜欢

转载自www.cnblogs.com/dabric/p/12389514.html