Alter Index

In this lesson we discuss how to modify an index.

ALTER INDEX

It is hard to predict what indexes to create without observing the access patterns for an application. We can add, remove, or modify indexes after the application is deployed. Note that modifying indexes doesn’t change the data in the table.

Example Syntax

ALTER TABLE table

ADD INDEX indexName (col1, col2, … coln);

Connect to the terminal below by clicking in the widget. Once connected, the command line prompt will show up. Enter or copy and paste the command ./DataJek/Lessons/18lesson.sh and wait for the MySQL prompt to start-up.

Press + to interact
-- The lesson queries are reproduced below for convenient copy/paste into the terminal.
-- Query 1
ALTER TABLE Actors ADD INDEX nameIndex (FirstName);
-- Query 2
ALTER TABLE Actors ADD INDEX nameIndexWithOnlyTenChars (FirstName(10));
-- Query 3
ALTER TABLE Actors DROP INDEX nameIndex;
-- Query 4
ALTER TABLE Actors DROP PRIMARY KEY;
-- Query 5
CREATE TABLE Movies (Name VARCHAR(100), Released DATE, PRIMARY KEY (Name));
DESC Movies;
ALTER TABLE Movies DROP PRIMARY KEY;
ALTER TABLE Movies ADD PRIMARY KEY (Released);
Terminal 1
Terminal
Loading...

MySQL allows us to add a new index to an existing table. Say we find out that a lot of users of our application are searching ...

Access this course and 1400+ top-rated courses and projects.