1. 程式人生 > >資料表的索引操作

資料表的索引操作

建立資料表的索引

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(

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]

create database student;

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;