mysql命令行快速清空数据库的方法

有时我们只想把数据库里的所有表删除掉,而数据库保留,这时可以用mysql命令实现。

这里我们以wordpress数据库为例说明如何操作。

1. 以root身份登录数据库

mysql -uroot -p

2. 先用select语句将数据库wordpress里的所有表名及DROP语句串联后显示出来:

SELECT concat('DROP TABLE IF EXISTS wordpress.', table_name, ';') FROM information_schema.tables WHERE table_schema = 'wordpress';

显示如下:

+------------------------------------------------------------+

| concat('DROP TABLE IF EXISTS wordpress.', table_name, ';') |

+------------------------------------------------------------+

| DROP TABLE IF EXISTS wordpress.wp_commentmeta; |

| DROP TABLE IF EXISTS wordpress.wp_comments; |

| DROP TABLE IF EXISTS wordpress.wp_links; |

| DROP TABLE IF EXISTS wordpress.wp_options; |

| DROP TABLE IF EXISTS wordpress.wp_postmeta; |

| DROP TABLE IF EXISTS wordpress.wp_posts; |

| DROP TABLE IF EXISTS wordpress.wp_term_relationships; |

| DROP TABLE IF EXISTS wordpress.wp_term_taxonomy; |

| DROP TABLE IF EXISTS wordpress.wp_termmeta; |

| DROP TABLE IF EXISTS wordpress.wp_terms; |

| DROP TABLE IF EXISTS wordpress.wp_usermeta; |

| DROP TABLE IF EXISTS wordpress.wp_users; |

+------------------------------------------------------------+

12 rows in set (0.00 sec)

3. 将上面内容复制到记事本并整理如下:

DROP TABLE IF EXISTS wordpress.wp_commentmeta;

DROP TABLE IF EXISTS wordpress.wp_comments;

DROP TABLE IF EXISTS wordpress.wp_links;

DROP TABLE IF EXISTS wordpress.wp_options;

DROP TABLE IF EXISTS wordpress.wp_postmeta;

DROP TABLE IF EXISTS wordpress.wp_posts;

DROP TABLE IF EXISTS wordpress.wp_term_relationships;

DROP TABLE IF EXISTS wordpress.wp_term_taxonomy;

DROP TABLE IF EXISTS wordpress.wp_termmeta;

DROP TABLE IF EXISTS wordpress.wp_terms;

DROP TABLE IF EXISTS wordpress.wp_usermeta;

DROP TABLE IF EXISTS wordpress.wp_users;

4. 复制上面内容到mysql命令行即可。

Logo

华为云1024程序员节送福利,参与活动赢单人4000元礼包,更有热门技术干货免费学习

更多推荐