MySQL 修改和刪除索引

2021-10-03 21:00:17 字數 2372 閱讀 6015

mysql 中修改索引可以通過刪除原索引,再根據需要建立乙個同名的索引,從而實現修改索引的操作。

當不再需要索引時,可以使用 drop index 語句或 alter table 語句來對索引進行刪除。

1 使用 drop index 語句

語法格式:

drop index 《索引名》 on 《表名》
語法說明如下:

《索引名》:要刪除的索引名。

《表名》:指定該索引所在的表名。

2 使用 alter table 語句

根據 alter table 語句的語法可知,該語句也可以用於刪除索引。具體使用方法是將 alter table 語句的語法中部分指定為以下子句中的某一項。

drop primary key:表示刪除表中的主鍵。乙個表只有乙個主鍵,主鍵也是乙個索引。

drop index index_name:表示刪除名稱為 index_name 的索引。

drop foreign key fk_symbol:表示刪除外來鍵。

注意:如果刪除的列是索引的組成部分,那麼在刪除該列時,也會將該列從索引中刪除;如果組成索引的所有列都被刪除,那麼整個索引將被刪除。

刪除表 tb_stu_info 中的索引,輸入的 sql 語句和執行結果如下所示。

mysql> drop index height

-> on tb_stu_info;

query ok,

0 rows affected (

0.27 sec)

records:

0 duplicates:

0 warnings:

0mysql> 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

) engine=innodb default charset=gb2312

1 row in set (

0.00 sec)

刪除表 tb_stu_info2 中名稱為 id 的索引,輸入的 sql 語句和執行結果如下所示。

mysql> alter table tb_stu_info2

-> drop index height;

query ok,

0 rows affected (

0.13 sec)

records:

0 duplicates:

0 warnings:

0mysql> show create table tb_stu_info2\g**

****

****

****

****

****

****

*1. row ***

****

****

****

****

****

****

table: tb_stu_info2

create table: create table `tb_stu_info2` (

`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

) engine=innodb default charset=gb2312

1 row in set (

0.00 sec)

Mysql索引,建立,修改,刪除索引

建立普通索引 create table t12 id int,name varchar 10 index name show create table t12 建立唯一性索引 create table t13 id int,name varchar 20 unique index idinx id ...

Mysql 索引的建立刪除修改

此文 1 索引作用 在索引列上,除了上面提到的有序查詢之外,資料庫利用各種各樣的快速定位技術,能夠大大提高查詢效率。特別是當資料量非常大,查詢涉及多個表時,使用索引往往能使查詢速度加快成千上萬倍。例如,有3個未索引的表t1 t2 t3,分別只包含列c1 c2 c3,每個表分別含有1000行資料組成,...

MySQL 建立索引 修改索引 刪除索引的命令語句

檢視表中已經存在 index show index from table name 建立和刪除索引 索引的建立可以在 create table 語句中進行,也可以單獨用 create index 或 alter table 來給表增加索引。刪除索引可以利用alter table 或 drop ind...