Is there a nice easy way to drop all tables from a MySQL database, ignoring any foreign key constraints that may be in there?
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
This solution is based on @SkyLeach answer but with the support of dropping tables with foreign keys.
You can do:
Then run the generated queries. They will drop every single table on the current database.
Here is some help on
drop table
command.A one liner to drop all tables from a given database:
Running this will drop all tables from database DATABASE_NAME.
And a nice thing about this is that the database name is only written explicitly once.
Here is an automated way to do this via a bash script:
I found the generated set of drop statements useful, and recommend these tweaks:
Note: This does not execute the DROP statements, it just gives you a list of them. You will need to cut and paste the output into your SQL engine to execute them.
Therefore, in order for the drop statements to work if you need:
This will disable referential integrity checks - so when you are done performing the drops you need, you will want to reset key checking with
NB: to use output of SELECT easier, mysql -B option can help.
I use the following with a MSSQL server:
Replace YOUR_DATABASE with the name of your database or remove the entire IF statement (I like the added safety).