2010-10-20 99 views
0

如何在SQL中进行查询以删除每个表中存在的所有记录。删除所有表中存在的所有记录

有可能使用命令delete写每个名称表。但编写每一个名字表需要很长时间。

我已经尝试在MySQL DELETE FROM *但它得到了错误:

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

+0

那么,这是在PHP?我在C#中提供了一个答案(有点伪)... – IAbstract 2010-10-20 04:57:37

+1

Duplicate http://stackoverflow.com/questions/935284/is-there-a-way-to-truncate-most-tables-in-a-mysql-模式 http://stackoverflow.com/questions/454174/how-to-empty-all-rows-from-all-tables-in-mysql-in-sql – 2010-10-20 05:12:21

+0

@dboarman:不,实际上我想插入记录后我删除所有记录存在数据库。谢谢 – 2010-10-20 08:00:29

回答

1

你只需要做为每个表TRUNCATE查询。你也可以使用删除,但截断通常更适合你正在做的事情。

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

没有*或全部表选择器。

0

我相信MySQL有一个SQL语句如下:

Show tables 

作为命令文本与Show tables创建命令对象,并执行。

使用该语句,您可以执行查询并填充MySqlDataReader。按照以下方式迭代阅读器并将表格名称放入格式化的字符串中。

// 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(); 
    } 
} 

这应该足够接近以帮助您走上正确的轨道。

0

您可以使用INFORMATION_SCHEMA遍历所有表并动态执行DELETE或TRUNCATE语句。

0

DELETE语句可以使用。

以下将删除所有行,但索引(例如自动增量)将不会被重置。

DELETE * FROM TABLEX

下面将删除所有行也索引将被重置

TRUNCATE TABLEX

1

请使用

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

所有最优秀的

0

如果你想自动完成这个,你必须编写一些MySQL外部的代码。使用show tables来获取表名,然后在每个表上调用TRUNCATE