开发者

How to delete all rows in all table of a database in MySQL?

开发者 https://www.devze.com 2023-02-12 05:04 出处:网络
I had a database with ne开发者_运维知识库arly 500 tables and I want to delete all the records of all the tables. How to achieve this?The simplest way is to drop and recreate the database structure usi

I had a database with ne开发者_运维知识库arly 500 tables and I want to delete all the records of all the tables. How to achieve this?


The simplest way is to drop and recreate the database structure using these shell commands:

mysqldump -d dbname > structure.sql
mysqladmin drop dbname
mysqladmin create dbname
mysql dbname < structure.sql

Insert mysql credentials as required, eg -u root -psecret -h localhost


TRUNCATE tableName;

This will empty the contents of the table. check here

<?php
mysql_connect('localhost', 'user', 'password');
$dbName = "database";
mysql_select_db($dbName)
$result_t = mysql_query("SHOW TABLES");
while($row = mysql_fetch_assoc($result_t))
{
   mysql_query("TRUNCATE " . $row['Tables_in_' . $dbName]);
}
?>


check this other question on stackoverflow

also, it would be useful to know if you want to use just sql or also any scripting language.

0

精彩评论

暂无评论...
验证码 换一张
取 消