用alter table添加索引与create index区别

发布时间 2023-05-02 10:14:35作者: 郭慕荣

1、alter table一次可以添加多个索引,create index一次只能创建一个。创建多个索引时,alter table只对表扫描一次,效率较高。
2、alter table可以不指定索引名,此时将使用索引列的第一列的列名;create index必须指定索引名。
因此,alter table添加索引更灵活,所以在创建索引的时候提倡使用alter table这种形式。

ALTER TABLE table_name ADD INDEX index_name (column_list)
ALTER TABLE table_name ADD UNIQUE (column_list)
ALTER TABLE table_name ADD PRIMARY KEY (column_list)

总结:创建索引建议使用alter table这种形式。