Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between MySQL's create index and alter add index?

Tags:

indexing

mysql

I have a table "data" with column id(varchar), text(varchar), date(date). Creating index on mysql, I use heidiSQL.

When I right click on the column and select create new index (key), the code shows it's using alter table data add index 'index1' ('id,date(10)')

What is the difference between this and create index index1 on data ('id,date(10)')

like image 435
Suanmeiguo Avatar asked Jun 14 '13 16:06

Suanmeiguo


People also ask

What is the difference between CREATE INDEX and create unique index?

Index: It is a schema object which is used to provide improved performance in the retrieval of rows from a table. Unique Index: Unique indexes guarantee that no two rows of a table have duplicate values in the key column (or columns).

How many types of indexes are there in MySQL?

MySQL has three types of indexes: INDEX, UNIQUE (which requires each row to have a unique value), and PRIMARY KEY (which is just a particular UNIQUE index).

What does CREATE INDEX do in MySQL?

In MySQL, an index can be created on a table when the table is created with CREATE TABLE command. Otherwise, CREATE INDEX enables to add indexes to existing tables. A multiple-column index can be created using multiple columns. The indexes are formed by concatenating the values of the given columns.

How do you create an index in alter table?

ALTER command to add and drop INDEX ALTER TABLE tbl_name ADD INDEX index_name (column_list) − This adds an ordinary index in which any value may appear more than once. ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list) − This creates a special FULLTEXT index that is used for text-searching purposes.


1 Answers

The implementation is the same on the server-side.

The only difference is that with CREATE INDEX syntax, you must specify a name for the index.

Whereas with ALTER TABLE, you may specify a name for the index, but you don't have to.

If you don't specify a name, the server generates a default name, as the name of the first column in the index, with a number suffix if necessary.

like image 92
Bill Karwin Avatar answered Sep 24 '22 13:09

Bill Karwin