是否存在在一次操作中截断数据库中所有表的查询(命令)?我想知道我是否可以用一个查询做到这一点。
当前回答
溶液(1)
mysql> select group_concat('truncate',' ',table_name,';') from information_schema.tables where table_schema="db_name" into outfile '/tmp/a.txt';
mysql> /tmp/a.txt;
溶液2)
- Export only structure of a db
- drop the database
- import the .sql of structure
——编辑----
earlier in solution 1, i had mentioned concat() instead of group_concat() which would have not returned the desired result
其他回答
不。没有单独的命令可以一次截断所有mysql表。您必须创建一个小脚本来逐个截断表。
裁判:http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
溶液(1)
mysql> select group_concat('truncate',' ',table_name,';') from information_schema.tables where table_schema="db_name" into outfile '/tmp/a.txt';
mysql> /tmp/a.txt;
溶液2)
- Export only structure of a db
- drop the database
- import the .sql of structure
——编辑----
earlier in solution 1, i had mentioned concat() instead of group_concat() which would have not returned the desired result
我知道这不是一个确切的命令,但预期的结果可以从phpMyAdmin通过以下步骤实现:
选择(所有)要删除的表(勾选全部) 从“With selected:”列表中选择“Drop”/“Truncate” 在确认页面(“Do you really want to:”)复制查询(所有带有红色背景的内容) 在顶部,点击SQL并写:“SET FOREIGN_KEY_CHECKS=0;”然后粘贴之前复制的查询 点击“去”
其思想是快速从数据库中获取所有表(只需5秒和2次单击),但首先禁用外键检查。不需要CLI,也不需要删除数据库并重新添加。
下面的MySQL查询本身将生成一个查询,该查询将截断给定数据库中的所有表。它绕过外键:
SELECT CONCAT(
'SET FOREIGN_KEY_CHECKS=0; ',
GROUP_CONCAT(dropTableSql SEPARATOR '; '), '; ',
'SET FOREIGN_KEY_CHECKS=1;'
) as dropAllTablesSql
FROM ( SELECT Concat('TRUNCATE TABLE ', table_schema, '.', TABLE_NAME) AS dropTableSql
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'DATABASE_NAME' ) as queries
我不确定,但我认为有一个命令可以使用,你可以复制数据库的模式到新的数据库,一旦你这样做了,你可以删除旧的数据库,这之后,你可以再次复制数据库模式到旧的名称。