用ALTERTABLE語句來建立索引

2021-12-29 23:32:51 字數 3306 閱讀 2763

7.2.3 用alter table語句來建立索引

在已經存在的表上,可以通過alter table語句直接為表上的乙個或幾個字段建立索引。基本形式如下:

1. alter table 表名 add [ unique | fulltext | spatial ] index2. 索引名(屬性名 [ (長度) ] [ asc | desc]);其中的引數與上面的兩種方式的引數是一樣的。

1.建立普通索引

【示例7-13】 下面在example0表中的name欄位上建立名為index13_name的索引。sql**如下:

1. alter table example0 add index index13_name ( name(20) ) ;使用alter table語句建立索引之前,先執行show create table語句檢視example0表的結構。show create table語句執行結果如下:

1. mysql>show create table example0 \g2. *************************** 1. row **************************3. table: example04. create table: create table `example0` (5. `id` int(11) default null,6. `name` varchar(20) default null,7. `***` tinyint(1) default null,8. key `index7_id` (`id`)9. ) engine=innodb default charset=utf810. 1 row in set (0.00 sec)結果顯示,example0表上只有index7_id索引。下面執行alter table語句建立index13_name索引。alter table語句執行結果如下:

1. mysql>alter table example0 add index index13_name ( name(20) ) ;2. query ok, 0 rows affected (0.01 sec)3. records: 0 duplicates: 0 warnings: 0執行結果顯示建立成功,使用show createtable語句檢視example0表的結構。顯示如下:

1. mysql>show create table example0 \g2. *************************** 1. row ***************************3. table: example04. create table: create table `example0` (5. `id` int(11) default null,6. `name` varchar(20) default null,7. `***` tinyint(1) default null,8. key `index7_id` (`id`),9. key `index13_name` (`name`)10. ) engine=innodb default charset=utf811. 1 row in set (0.00 sec)結果可以看到,name欄位已經建立了乙個名為index13_name的索引。

2.建立唯一性索引

【示例7-14】下面在index14表中的course_id欄位上,建立名為index14_id的唯一性索引。sql**如下:

1. alter table index14 add unique index index14_id ( course_id ) ;其中,index14_id為索引的名詞;unique用來設定索引為唯一性索引;表index14中的course_id欄位可以有唯一性約束,也可以沒有唯一性約束。

3.建立全文索引

【示例7-15】 下面在index15表中的info欄位上建立名為index15_info的全文索引。sql**如下:

1. alter table index15 add fulltext index index15_info ( info ) ;其中,fulltext用來設定索引為全文索引;表index15的儲存引擎必須是myisam型別;info欄位必須為char、varchar和text等型別。

4.建立單列索引

【示例7-16】 下面在index16表中的address欄位上建立名為index16_addr的單列索引。address欄位的資料型別為varchar(20),索引的資料型別為char(4)。sql**如下:

1. alter table index16 add index index16_addr( address(4) ) ;這樣,查詢時可以只查詢address欄位的前4個字元,而不需要全部查詢。

5.建立多列索引

【示例7-17】 下面在index17表中的name和address欄位上建立名為index17_na的多列索引。sql**如下:

1. alter table index17 add index index17_na( name, address ) ;該索引建立好了以後,查詢條件中必須有name欄位才能使用索引。

6.建立空間索引

【示例7-18】下面在index18表中的line欄位上建立名為index18_line的多列索引。sql**如下:

1. alter table index18 add spatial index index18_line( line ) ;其中,spatial用來設定索引為空間索引;表index18的儲存引擎必須是myisam型別;line欄位必須是非空的,而且必須是空間資料型別。

7 2 3 用ALTER TABLE語句來建立索引

7.2.3 用alter table語句來建立索引 在已經存在的表上,可以通過alter table語句直接為表上的乙個或幾個字段建立索引。基本形式如下 alter table 表名 add unique fulltext spatial index 索引名 屬性名 長度 asc desc 其中的引...

用while語句來實現迴圈

while語句的一般形式為 while 表示式 語句 其中表示式是迴圈條件,語句為迴圈體。while語句的語義是 計算表示式的值,當值為真 非0 時,執行迴圈體語句。while語句迴圈的特點是先判斷條件表示式,後執行迴圈體語句。舉乙個簡單的例子 include int main printf d n...

MFC用CreateDirectory建立檔案目錄

createdirectory這個函式的作用是建立乙個新的目錄。如果底層檔案系統支援檔案和目錄上的安全描述,該功能可將指定的安全描述到新的目錄。函式原型 cpp view plain copy bool createdirectory lpctstr lppathname,lpsecurity at...