Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does dropping a MySQL table automatically drop that table's indices too? [duplicate]

Tags:

indexing

mysql

Does dropping a MySQL table automatically drop that table's indices too?

like image 488
Zeke Avatar asked May 18 '10 00:05

Zeke


People also ask

Does dropping table automatically drop index?

If a table is dropped, all associated indexes are dropped automatically. See Also: Oracle Database SQL Language Reference for syntax and restrictions on the use of the DROP INDEX statement.

Do I need to drop index before dropping table?

Yes, it does. However, if you have foreign key constraints such as RESTRICT that ensure referential integrity with other tables, you'll want to drop those keys prior to dropping or truncating a table.

Does dropping an index lock a table MySQL?

Indexes on variable-width columns of NDB tables are dropped online; that is, without any table copying. The table is not locked against access from other NDB Cluster API nodes, although it is locked against other operations on the same API node for the duration of the operation.

What does DROP TABLE do in MySQL?

The DROP TABLE statement allows a table to be removed from a MySQL database. This statement deletes the entire structure as well as the content of the table.


2 Answers

Yes. It drops the indexes. This can be verified:

CREATE TABLE table1 (foo INT);
CREATE INDEX ix_table1_foo ON table1 (foo);
CREATE INDEX ix_table1_foo ON table1 (foo); -- fails: index already exists.
DROP TABLE table1;
CREATE TABLE table1 (foo INT);
CREATE INDEX ix_table1_foo ON table1 (foo); -- succeeds: index does not exist.

You can also verify it by looking in the information schema:

CREATE TABLE table1 (foo INT);
CREATE INDEX ix_table1_foo ON table1 (foo);

SELECT COUNT(*)
FROM information_schema.STATISTICS
WHERE INDEX_NAME = 'ix_table1_foo';  -- returns 1

DROP TABLE table1;

SELECT COUNT(*)
FROM information_schema.STATISTICS
WHERE INDEX_NAME = 'ix_table1_foo';  -- returns 0
like image 163
Mark Byers Avatar answered Oct 10 '22 06:10

Mark Byers


Yes. Indices are part of their owner table and are freed along with the row data when dropped.

(Foreign-key indices on other tables that refer to it will prevent the table being dropped.)

like image 38
bobince Avatar answered Oct 10 '22 05:10

bobince