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'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM