mysql 如何加索引 mysql如何新增索引

2021-10-17 16:11:47 字數 2901 閱讀 1386

mysql新增索引的方法:可以通過【create table】語句來新增,如【constraint primary key | index 】,表示建立一般索引。

在mysql中可以在建立表(create table)的同時建立索引;也可以在建立表後建立索引,使用create index語句或alter table 語句。

1、使用create index語句

可以使用專門用於建立索引的 create index 語句在乙個已有的表上建立索引,但該語句不能建立主鍵。create index on ( [ asc | desc])

語法說明如下:

● :指定索引名。乙個表可以建立多個索引,但每個索引在該表中的名稱是唯一的。

● :指定要建立索引的表名。

● :指定要建立索引的列名。通常可以考慮將查詢語句中在 join 子句和 where 子句裡經常出現的列作為索引列。

● :可選項。指定使用列前的 length 個字元來建立索引。使用列的一部分建立索引有利於減小索引檔案的大小,節省索引列所佔的空間。在某些情況下,只能對列的字首進行索引。索引列的長度有乙個最大上限 255 個位元組(myisam 和 innodb 表的最大上限為 1000 個位元組),如果索引列的長度超過了這個上限,就只能用列的字首進行索引。另外,blob 或 text 型別的列也必須使用字首索引。

● asc|desc:可選項。asc指定索引按照公升序來排列,desc指定索引按照降序來排列,預設為asc。

二、使用 create table 語句

索引可以在建立表(create table)的同時建立,語法格式:

1、建立主鍵索引constraint primary key [索引型別] (,…)

在使用 create table 語句定義列選項的時候,可以通過直接在某個列定義後面新增 primary key 的方式建立主鍵。而當主鍵是由多個列組成的多列索引時,則不能使用這種方法,只能用在語句的最後加上乙個 primary kry(,…) 子句的方式來實現。

2、建立一般索引key | index (,…)

3、建立唯一性索引unique [ index | key] (,…)

4、建立外來鍵索引foreign key

示例1:建立乙個表 tb_stu_info,在該錶的 height 字段建立一般索引。mysql> create table tb_stu_info

-> id int not null,

-> name char(45) default null,

-> dept_id int default null,

-> age int default null,

-> height int default null,

-> index(height)

示例2:建立乙個表 tb_stu_info2,在該錶的 id 欄位上使用 unique 關鍵字建立唯一索引。mysql> create table tb_stu_info2

-> id int not null,

-> name char(45) default null,

-> dept_id int default null,

-> age int default null,

-> height int default null,

-> unique index(id)

三、使用 alter table 語句

在使用 alter table 語句修改表的同時,可以向已有的表新增索引。具體的做法是在 alter table 語句中新增以下語法成分的某一項或幾項。

1、建立主鍵索引add primary key (,…)

2、建立一般索引add index (,…)

3、建立唯一性索引add unique [ index | key] (,…)

4、建立外來鍵索引add foreign key (,…)

示例1:建立乙個表 tb_stu_info3後,在該錶的 id 欄位上使用 unique 關鍵字建立唯一索引。mysql> create table tb_stu_info3

-> id int not null,

-> name char(45) default null,

-> dept_id int default null,

-> age int default null,

-> height int default null,

query ok,0 rows affected (0.40 sec)

mysql>alter table tb_stu_info3 add unique (id) ;

四、顯示索引資訊

以使用 show index 命令來列出表中的相關的索引資訊。可以通過新增 \g 來格式化輸出資訊。

示例:mysql> show create table tb_stu_info\g

*************************** 1. row ***************************

table: tb_stu_info

create table: create table `tb_stu_info` (

`id` int(11) not null,

`name` char(45) default null,

`dept_id` int(11) default null,

`age` int(11) default null,

`height` int(11) default null,

key `height` (`height`)

) engine=innodb default charset=gb2312

1 row in set (0.01 sec)

mysql某字典加部分索引 MySQL索引

1.什麼是索引 索引是對資料庫表中一列或多列的值進行排序的一種結構,使用索引可快速訪問資料庫表中的特定資訊.sql索引在資料庫優化中佔非常大的比例,乙個好的索引設計,可以讓你的效率提高幾十甚至幾百倍.2.深入淺出理解索引 其實,我們的漢語字典的正文本身就是乙個聚集索引。比如,我們要查 安 字,就會很...

mysql加索引的優點 MYSQL 索引的優點

索引最常說的作用就是可以讓伺服器快速的定位到表的指定位置 但是這並不是索引唯一的作用,到目前位置可以看到,根據索引建立的資料結構的不同,索引也有一些其他的作用 最常見的b tree索引,按照順序進行儲存資料,所以mysql可以用來order by和group by。因為資料是有序的,所以b tree...

MySQL表字段加索引

新增普通索引 此時key型別為mul alter table table name add index column column 例如 alter table poicity add index delete flag delete flag 新增主鍵索引 primary key alter ta...