Php 如何在不删除数据库本身的情况下删除数据库中的所有表?
我想从数据库中删除所有表,但不想删除数据库本身。可能吗?我只是在寻找比删除数据库并再次创建它更短的方法。谢谢 没有简单的方法可以做到这一点。或者,您需要提前知道这些表是什么: //编辑您可以使用查询Php 如何在不删除数据库本身的情况下删除数据库中的所有表?,php,sql,mysql,drop-table,Php,Sql,Mysql,Drop Table,我想从数据库中删除所有表,但不想删除数据库本身。可能吗?我只是在寻找比删除数据库并再次创建它更短的方法。谢谢 没有简单的方法可以做到这一点。或者,您需要提前知道这些表是什么: //编辑您可以使用查询SHOW TABLE STATUS $tables = array('users','otherdata'); foreach($tables as $table){ db.execute("DROP TABLE "+$table); } 或者,您可以删除数据库并将其重新创建为空(实际上不需要太
SHOW TABLE STATUS
$tables = array('users','otherdata');
foreach($tables as $table){
db.execute("DROP TABLE "+$table);
}
或者,您可以删除数据库并将其重新创建为空(实际上不需要太多工作!):
您必须分别删除数据库中的每个表,因此删除数据库并重新创建它实际上是最短的路线(也是最快的路线)。当我必须在Oracle中执行此操作时,我会编写一条select语句,为我生成drop table语句。大意是: 选择“放置表格”| |表格| |名称|”来自用户表 然后,我可以通过管道将select语句的输出传输到一个文件。在我运行这个之后,我会有一个文件,可以为我删除所有的表。它看起来像: 下表1 下表2 下表3 等等
虽然不是mysql专家,但我可以想象它会有一个类似的功能,既可以为模式选择所有表,也可以将SQL语句的输出直接输出到文件。使用
显示表状态
获取数据库中的所有表,然后循环结果并逐个删除它们。最短的是重新创建数据库。但是如果你不想
这是针对MySQL/PHP的。没有测试过,但差不多
$mysqli = new mysqli("host", "my_user", "my_password", "database");
$mysqli->query('SET foreign_key_checks = 0');
if ($result = $mysqli->query("SHOW TABLES"))
{
while($row = $result->fetch_array(MYSQLI_NUM))
{
$mysqli->query('DROP TABLE IF EXISTS '.$row[0]);
}
}
$mysqli->query('SET foreign_key_checks = 1');
$mysqli->close();
注释中有一些解决方案:我需要删除所有表,但意外转储的表除外 一个PHP函数,用于删除除某些表(改编自)以外的所有表,以供可能需要的其他人使用:
<?php
$mysqli = new mysqli( "localhost", "user", 'password', "database");
function drop_all_tables($exceptions_array, $conn) {
$exceptions_string="('" ;
foreach ($exceptions_array as $table) {
$exceptions_string .=$table . "','";
}
$exceptions_string=rtrim($exceptions_string, ",'");
$exceptions_string .="')" ;
$sql="SELECT CONCAT('DROP TABLE ', TABLE_NAME, '; ')
FROM information_schema.tables
WHERE table_schema = DATABASE() AND table_name NOT IN $exceptions_string";
$result=$ conn->query($sql);
while($row = $result->fetch_array(MYSQLI_NUM)) {
$conn->query($row[0]);
}
}
//drop_all_tables(array("table1","table2","table3","table4"), $mysqli);
?>
删除所有表的单行查询,如下所示:
$dbConnection = mysqli_connect("hostname", "username", "password", "database_name");
$dbConnection->query('SET foreign_key_checks = 0');
$qry_drop = "DROP TABLE IF EXISTS buildings, business, computer, education, fashion, feelings, food, health, industry, music, nature, people, places, religion, science, sports, transportation, travel";
$dbConnection->query($qry_drop);
$mysqli->query('SET foreign_key_checks = 1');
$mysqli->close();
你可以执行这个。如果我遗漏了任何表格,请添加更多表格
drop table wp_commentmeta;
drop table wp_comments;
drop table wp_links;
drop table wp_options;
drop table wp_postmeta;
drop table wp_posts;
drop table wp_term_relationships;
drop table wp_term_taxonomy;
drop table wp_termmeta;
drop table wp_terms;
drop table wp_usermeta;
drop table wp_users;
执行此操作的程序方法如下所示:
$query_disable_checks = 'SET foreign_key_checks = 0';
$query_result = mysqli_query($connect, $query_disable_checks);
// Get the first table
$show_query = 'Show tables';
$query_result = mysqli_query($connect, $show_query);
$row = mysqli_fetch_array($query_result);
while ($row) {
$query = 'DROP TABLE IF EXISTS ' . $row[0];
$query_result = mysqli_query($connect, $query);
// Getting the next table
$show_query = 'Show tables';
$query_result = mysqli_query($connect, $show_query);
$row = mysqli_fetch_array($query_result);
}
这里的$connect
就是用mysqli_connect()建立的连接
如果数据库中有其他内容(存储过程、函数和与这些内容相关的用户权限),删除数据库可能不是OP想要的。正如fredley所说,如果您不需要任何东西,只需要删除空数据库并重新创建它。如果您无法进行删除/创建,第一种方法是唯一的方法,您不能在一个命令中删除所有表。但是您可能拥有此数据库的权限,并且没有创建新数据库的权限……您不必真正了解这些表,您可以从元数据中查询这些表(select table\u name from information\u schema.tables where table\u type='BASE table'和table\u schema='yourdbname'
)。您可能必须重复应用此脚本,因为存在外键约束可能会阻止删除表。绝对。可能需要几次传球。嗯,不一定。如果删除一个事务中的所有表,则可以在Oracle中延迟约束检查(SET CONSTRAINTS all DEFERRED;
)。在MySQL中,您可以临时禁用外键检查(如在我的解决方案中-设置外键检查=0;
),这至少可以减少所需的阶段数。
$query_disable_checks = 'SET foreign_key_checks = 0';
$query_result = mysqli_query($connect, $query_disable_checks);
// Get the first table
$show_query = 'Show tables';
$query_result = mysqli_query($connect, $show_query);
$row = mysqli_fetch_array($query_result);
while ($row) {
$query = 'DROP TABLE IF EXISTS ' . $row[0];
$query_result = mysqli_query($connect, $query);
// Getting the next table
$show_query = 'Show tables';
$query_result = mysqli_query($connect, $show_query);
$row = mysqli_fetch_array($query_result);
}