是否存在在一次操作中截断数据库中所有表的查询(命令)?我想知道我是否可以用一个查询做到这一点。


当前回答

PHP单命令:

php -r '$d="PUT_YOUR_DB_NAME_HERE"; $q="show tables"; $dt="drop table"; exec("mysql -Nse \"$q\" $d", $o); foreach($o as $e) `mysql -e "$dt $e" $d`;'

执行的PHP脚本:

$d="PUT_YOUR_DB_NAME_HERE"; 
$q="show tables"; 
$dt="drop table"; 

exec("mysql -Nse \"$q\" $d", $o); 

foreach($o as $e)
  `mysql -e "$dt $e" $d`;

其他回答

这里,我知道这里

   SELECT Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';') 
    FROM INFORMATION_SCHEMA.TABLES where  table_schema in ('databasename1','databasename2');

如果不能删除或更新父行:外键约束失败

如果存在外键指向您试图删除/截断的表的表,就会发生这种情况。

在截断表之前,你需要做的是:

SET FOREIGN_KEY_CHECKS=0;

截断您的表并将其更改回

SET FOREIGN_KEY_CHECKS=1; 

使用这段PHP代码 $truncate = mysql_query("SELECT Concat(' truncate TABLE ',table_schema,'. "',TABLE_NAME, ';')作为table_query FROM INFORMATION_SCHEMA。table where table_schema in ('databasename')"); 而($ truncateRow =作用(截断)美元){ mysql_query()美元truncateRow [' tables_query ']); } ? > 查看详情 链接

如果使用SQL server 2005,有一个隐藏的存储过程,允许您对数据库中的所有表执行一个或一组命令。下面是如何用这个存储过程调用TRUNCATE TABLE:

EXEC [sp_MSforeachtable] @command1="TRUNCATE TABLE ?"

这里有一篇很好的文章进一步阐述。

然而,对于MySql,你可以使用mysqldump并指定——add-drop-tables和——no-data选项来删除和创建所有忽略数据的表。是这样的:

mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE]

Mysqldump使用指南

溶液(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
TB=$( mysql -Bse "show tables from DATABASE" );
for i in ${TB};
    do echo "Truncating table ${i}";
    mysql -e "set foreign_key_checks=0; set unique_checks=0;truncate table DATABASE.${i}; set foreign_key_checks=1; set unique_checks=1";
    sleep 1;
done

--

大卫,

感谢您花时间格式化代码,但这是它应该如何应用。

库尔特

在UNIX或Linux机器上:

确保您处于bash shell中。从命令行运行这些命令,如下所示。

注意:

我将凭据存储在~/.my.cnf文件中,因此不需要在命令行上提供凭据。

注意:

CPM是数据库名称

我只展示了来自每个命令的结果的一小部分示例。

找到你的外键约束:

klarsen@Chaos:~$ mysql -Bse "select concat(table_name, ' depends on ', referenced_table_name)
             from information_schema.referential_constraints
             where constraint_schema = 'cpm'
             order by referenced_table_name"

Approval_external_system依赖于approval_request 地址视客户而定 Customer_identification取决于客户 External_id取决于客户 凭证取决于客户 Email_address取决于客户 Approval_request取决于客户 Customer_status取决于客户 Customer_image取决于客户

列出表和行数:

klarsen@Chaos:~$ mysql -Bse "SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'cpm'" | cat -n

 1  address 297
 2  approval_external_system    0
 3  approval_request    0
 4  country 189
 5  credential  468
 6  customer    6776
 7  customer_identification 5631
 8  customer_image  2
 9  customer_status 13639

截断你的表:

klarsen@Chaos:~$ TB=$( mysql -Bse "show tables from cpm" ); for i in ${TB}; do echo "Truncating table ${i}"; mysql -e "set foreign_key_checks=0; set unique_checks=0;truncate table cpm.${i}; set foreign_key_checks=1; set unique_checks=1"; sleep 1; done

截断表地址 截断表approval_external_system 截断表approval_request 截断表国家 截断表凭据 截断表客户 截断表customer_identification 截断表customer_image 截断表customer_status

验证它是否有效:

klarsen@Chaos:~$ mysql -Bse "SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'cpm'" | cat -n

 1  address 0
 2  approval_external_system    0
 3  approval_request    0
 4  country 0
 5  credential  0
 6  customer    0
 7  customer_identification 0
 8  customer_image  0
 9  customer_status 0
10  email_address   0

在Windows机器上:

注意:

CPM是数据库名称

C:\>for /F "tokens=*" %a IN ('mysql -Bse "show tables" cpm') do mysql -e "set foreign_key_checks=0; set unique_checks=0; truncate table %a; foreign_key_checks=1; set unique_checks=1" cpm

我觉得最上面的答案太棒了。但是,当你有一个经过身份验证的MySQL数据库用户时,它就失败了。

这是建立在我链接的顶部答案之上的解决方案。此解决方案安全地处理身份验证,而无需:

为每个表输入密码 担心你的密码泄露到某个地方 对其他MySQL cnf文件(~/.my.cnf)的副作用要了解更多关于这些文件的详细信息,请查看答案底部的参考资料部分。

1. 创建本地。my.cnf文件

vi .temp.my.cnf
[client]
user=<admin_user_goes_here>
password=<admin_password_goes_here>

2. 截断或删除所有表

2.截断所有表(仅为空)

mysql --defaults-extra-file=.temp.my.cnf -Nse 'show tables' <db_name_goes_here> | while read table; do mysql --defaults-extra-file=.temp.my.cnf -e "truncate table $table" <db_name_goes_here>; done

2.B删除所有表(完全删除)

mysql --defaults-extra-file=.temp.my.cnf -Nse 'show tables' <db_name_goes_here> | while read table; do mysql --defaults-extra-file=.temp.my.cnf -e "drop table $table" <db_name_goes_here>; done

3.清理

删除用户密码文件

rm -rf .temp.my.cnf

资源:

https://rtcamp.com/tutorials/mysql/mycnf-preference/ https://dev.mysql.com/doc/refman/8.0/en/option-files.html https://dev.mysql.com/doc/refman/8.0/en/option-file-options.html#option_general_defaults-extra-file