mysql点击清空所有表,如何清空mysql中所有表的所有行(在SQL中)

I'm writing some db utility scripts, and one of the tasks I need to do is rebuild the data only, but leave the schema intact. What is the easiest way to automate this from the command-line using bash and the mysql tools (no php, etc)?

Update:

I'd like the solution to handle all tables in one command, and if possible, not need to be updated if tables are added or removed.

解决方案TRUNCATE tableName;

This will empty the contents of the table.

Edit in response to the Q edit:

It seems from my quick test that you will have to do at least 2 queries as it seems that "show tables" cannot be used as a sub query, I don't know how to do this in bash so here is a PHP example, hopefully it will help.

mysql_connect('localhost', 'user', 'password');

$dbName = "database";

mysql_select_db($dbName); /*added semi-colon*/

$result_t = mysql_query("SHOW TABLES");

while($row = mysql_fetch_assoc($result_t))

{

mysql_query("TRUNCATE " . $row['Tables_in_' . $dbName]);

}

?>

At a minimum this needs some error handling.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值