// dialect comparison

DROP INDEX in every SQL dialect

Dropping an index is nearly universal, with one twist: SQL Server and MySQL require the table name in the statement.

Filed under Index

The verdict

Three engines drop by name alone; SQL Server and MySQL want the table. Indexes that enforce constraints are protected in SQL Server, PostgreSQL, SQLite and Oracle: drop the constraint instead.

Where they differ

Dropping an index is quick everywhere because it only frees pages; the cost shows up later if a query needed it. The portability question is whether the statement needs the table name.

How each engine writes it

SQL Server

DROP INDEX idx_orders_customer ON orders;

Requires the table name. DROP INDEX IF EXISTS works since 2016. An index that enforces a PRIMARY KEY or UNIQUE constraint refuses to drop (Msg 3723); use ALTER TABLE ... DROP CONSTRAINT.

PostgreSQL

DROP INDEX idx_orders_customer;

Drops by name alone, taking an ACCESS EXCLUSIVE lock on the table. DROP INDEX CONCURRENTLY avoids blocking reads and writes but cannot run inside a transaction.

MySQL

DROP INDEX idx_orders_customer ON orders;

Requires the table name. Also spelled ALTER TABLE orders DROP INDEX idx_orders_customer.

SQLite

DROP INDEX idx_orders_customer;

Simple name-based drop. The sqlite_autoindex_* indexes behind UNIQUE and PRIMARY KEY constraints cannot be dropped; removing the constraint means rebuilding the table.

Oracle

DROP INDEX idx_orders_customer;

Name-based. An index that enforces a primary or unique key raises ORA-02429; drop or disable the constraint first.

Questions that come up

What happens to running queries when I drop an index?

The drop needs an exclusive lock on the table (a metadata lock in MySQL), so it waits for running statements on that table and blocks new ones while it waits. The drop itself is fast; avoid running it under a long report. PostgreSQL's CONCURRENTLY avoids the blocking.

Can I get the index back without rebuilding?

No. DROP removes the definition and data; the only path back is CREATE INDEX again.

How do I find what an index was on first?

Check catalog views: sys.indexes + sys.index_columns in SQL Server, pg_indexes in PostgreSQL, SHOW CREATE TABLE in MySQL, PRAGMA index_info in SQLite, user_ind_columns in Oracle.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.