Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

MySQL : Drop all the tables in a schema


Drop all the tables in a schema in Mysql Database

It is often required to delete tables from a MySQL schema. Following is a simple SQL guide that can be used for deleting the all or the specified tables.

1. Turn off the Foreign Key checks

First we need to turn off the foreign key check within the tables. Otherwise, an error would be thrown due to the key constraints.

SET FOREIGN_KEY_CHECKS = 0;

2. Collect All table names

If the table names are not known, you can query the information_schema table to find them.

SELECT
    table_name
FROM
    information_schema.tables
WHERE
    table_schema = db_name;

3. Drop Each table

Tables can be dropped selectively,

DROP TABLE IF EXISTS ;

4. Enable foreign key check options

Make sure you enable the foreign key check back to make sure the integrity of your database.

SET FOREIGN_KEY_CHECKS = 1;

...


This post first appeared on Devdummy, please read the originial post: here

Share the post

MySQL : Drop all the tables in a schema

×

Subscribe to Devdummy

Get updates delivered right to your inbox!

Thank you for your subscription

×