Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

database

mysql

I had a database with nearly 500 tables and I want to delete all the records of all the tables. How to achieve this?

like image 951
Rajasekar Avatar asked Dec 21 '22 17:12

Rajasekar


2 Answers

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

like image 178
Zubin Avatar answered Jan 13 '23 13:01

Zubin


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]);
}
?>
like image 31
ayush Avatar answered Jan 13 '23 13:01

ayush