I have 100 tables in my database, I want to keep one only.
I tried something like below query:
DROP ALL TABLES EXCEPT my_table
But that doesn't seem to exist. any idea?
I have 100 tables in my database, I want to keep one only.
I tried something like below query:
DROP ALL TABLES EXCEPT my_table
But that doesn't seem to exist. any idea?
You can build a DROP TABLE statement with multiple listed tables, and run the query using MySQL prepared statements -
SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables FROM information_schema.tables
WHERE table_schema = 'Database1' AND table_name <> 'my_table';
SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt1 FROM @tables;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
You can't drop multiple tables in MySQL.
The easiest in your case would be to export the table you want to keep (using a tool like mysqldump
), then drop and recreate the database.
I wonder why no one suggested to go to the directory :
C:\Program Files (x86)\Parallels\Plesk\Databases\MySQL\data\user_db\...
and delete them easily there? this is what I did and it seems working
If you have lots of tables, watch out for truncation by the GROUP_CONCAT function. It has a setting for maximum length of the returned string: group_concat_max_len
See: GROUP_CONCAT reference manual entry
This answer guided me to the solution: MYSQL: How to define or get LONG string variables
You can build all drop statements output them info file and use file after that
SELECT CONCAT( 'DROP TABLE ', table_name , ';' ) AS statement FROM information_schema.tables WHERE table_name not like 'table_to_keep' and table_schema like 'your_schema' into outfile 'path_to_file;
source path_to_file;
You can use mysqldump
, try it !
mysqldump -u[USERNAME] -p[PASSWORD]--add-drop-table --no-data [DATABASE] |
grep ^DROP |
grep -v 'my_table' |
mysql -u[USERNAME] -p[PASSWORD] [DATABASE]