0

How to make query in SQL to delete all record exist in every table.

There possible to write every name table using command delete. But it will take long time to write every single name table.

I am already try DELETE FROM * in MySQL but it got error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*' at line 1

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Edy Cu
  • 3,262
  • 5
  • 20
  • 19
  • So, is this in PHP??? I supplied an answer in C# (sort of pseudo)... – IAbstract Oct 20 '10 at 04:57
  • 1
    Duplicate http://stackoverflow.com/questions/935284/is-there-a-way-to-truncate-most-tables-in-a-mysql-schema http://stackoverflow.com/questions/454174/how-to-empty-all-rows-from-all-tables-in-mysql-in-sql – Michael MacDonald Oct 20 '10 at 05:12
  • @dboarman: no, actually i want to to insert record after i am delete all record exist in db. thx – Edy Cu Oct 20 '10 at 08:00

6 Answers6

1

You will just have to do a TRUNCATE query for each table. You could also use delete, but truncate is generally more appropriate for what you are doing.

TRUNCATE TABLE table1;
TRUNCATE TABLE table2;
TRUNCATE TABLE table2;

There is no * or all table selector.

Nate Totten
  • 8,904
  • 1
  • 35
  • 41
1

Please use this

<?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]);
}
?>

All the best

0

I believe MySql has an sql statement as follows:

Show tables

Create your command object with Show tables as the command text and execute.

Using that statement you could execute the query and fill MySqlDataReader. Iterate the reader and put table names in a formatted string as follows.

// create connection string
...
while (myDataReader.Read())
{
    // execute command
    string command = "TRUNCATE TABLE {0}";

    MySqlCommand myCommand = new MySqlCommand();
    myCommand.CommandText = string.Format(command, reader[0]);

    using (MySqlConnection myConnection = new MySqlConnection(myConnString))
    {
        myCommand.Connection = myConnection;
        myCommand.Connection.Open();

        myCommand.ExecuteNonQuery();
    }
}

This ought to be close enough to help you get on the right track.

IAbstract
  • 19,551
  • 15
  • 98
  • 146
0

You can use the INFORMATION_SCHEMA to iterate over all the tables and dynamically execute the DELETE or TRUNCATE statement.

Russell McClure
  • 4,821
  • 1
  • 22
  • 22
0

DELETE statement can be used.

Following will delete all the rows but indexes(e.g. auto increments) will not be reseted.

DELETE * FROM TABLEX

Following will delete all the rows also indexes will be reset

TRUNCATE TABLEX

qasimzee
  • 640
  • 1
  • 12
  • 30
0

If you want to automate this, you have to write some code external to MySQL. Use show tables to get the table names and then call TRUNCATE on each of them.

Justin
  • 8,853
  • 4
  • 42
  • 42