1. 程式人生 > >刪除mysql資料庫中所有表

刪除mysql資料庫中所有表

刪除表的命令

drop table 表名;

如果有200張表,執行200次,想想就不想動手了。

下面提供一個使用information_schema庫的方案:

SELECT CONCAT('drop table ',table_name,';') FROM information_schema.`TABLES` WHERE table_schema='資料庫名';

例子

mysql> SELECT CONCAT('drop table ',table_name,';') FROM information_schema.`TABLES` WHERE table_schema=
'django_demo'; +----------------------------------------+ | CONCAT('drop table ',table_name,';') | +----------------------------------------+ | drop table auth_group; | | drop table auth_group_permissions; | | drop table auth_permission; | | drop table auth_user; |
| drop table auth_user_groups; | | drop table auth_user_user_permissions; | | drop table django_admin_log; | | drop table django_content_type; | | drop table django_migrations; | | drop table django_session; | | drop table tb_books; |
+----------------------------------------+ 11 rows in set (0.00 sec)

去掉|


a = '''
| drop table auth_group;                 |
| drop table auth_group_permissions;     |
| drop table auth_permission;            |
| drop table auth_user;                  |
| drop table auth_user_groups;           |
| drop table auth_user_user_permissions; |
| drop table django_admin_log;           |
| drop table django_content_type;        |
| drop table django_migrations;          |
| drop table django_session;             |
| drop table tb_books;   '''

print(a.replace('|', ''))

複製到資料庫執行兩次(因為可能表有關聯)