← Back to MySQL Course | Chapter 14: Views & Indexes | Lesson 4 of 5

DROP INDEX

Removing an Index

DROP INDEX permanently removes an index you no longer need, freeing up the storage space it was using and reducing the overhead MySQL pays on every insert or update to that table.

Example: Removing an Index

sql
CREATE TABLE users (id INT, email TEXT);
CREATE INDEX idx_email ON users (email);
DROP INDEX idx_email ON users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Dropping Indexes with ALTER TABLE

Indexes can also be dropped through ALTER TABLE using its DROP INDEX clause, which is handy when you're already running other structural changes on the table in the same statement.

Example: Dropping Indexes with ALTER TABLE

sql
CREATE TABLE users (id INT, email TEXT);
CREATE INDEX idx_email ON users (email);
ALTER TABLE users DROP INDEX idx_email;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Why We Drop Indexes

Every index has a cost: MySQL has to update it on every insert, update, or delete against the indexed column, so removing indexes that queries never actually use can noticeably speed up write-heavy tables.

Example: Why We Drop Indexes

sql
CREATE TABLE logs (id INT, note TEXT);
CREATE INDEX idx_note ON logs (note);
-- Every INSERT now also updates idx_note -- drop it if queries never filter on note
DROP INDEX idx_note ON logs;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Removing Primary Keys

The index automatically created behind a primary key can't be dropped with a plain DROP INDEX — you remove it specifically with ALTER TABLE ... DROP PRIMARY KEY instead.

Example: Removing Primary Keys

sql
CREATE TABLE users (id INT PRIMARY KEY, email TEXT);
ALTER TABLE users DROP PRIMARY KEY;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Viewing Indexes Before Dropping

Before dropping an index, it's worth listing the indexes currently defined on a table so you can confirm the exact name you're targeting rather than guessing and risking removing the wrong one.

Example: Viewing Indexes Before Dropping

sql
CREATE TABLE users (id INT, email TEXT);
CREATE INDEX idx_email ON users (email);
SHOW INDEX FROM users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.