[英]How to drop all tables in database without dropping the database itself?
我想從數據庫中刪除所有表,但不刪除數據庫本身。 是否可以 ? 我只是在尋找比刪除數據庫並重新創建它更短的方法。 謝謝 !
最短的是重新創建數據庫。 但如果你不想...
這是用於 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();
沒有簡單的方法可以做到這一點。 要么你需要提前知道表格是什么:
//編輯您可以使用查詢SHOW TABLE STATUS
獲取此信息
$tables = array('users','otherdata');
foreach($tables as $table){
db.execute("DROP TABLE "+$table);
}
或者您可以刪除數據庫並將其重新創建為空(這真的沒有那么多努力!):
db.execute('DROP DATABASE SITEDATA');
db.execute('CREATE DATABASE SITEDATA');
我需要刪除所有表,除了無意中的轉儲中的一對表。
一個 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);
?>
您必須分別刪除數據庫中的每個表,因此刪除數據庫並重新創建它實際上是最短的路線(也是最快的路線)。
當我必須在 Oracle 中執行此操作時,我會編寫一個 select 語句,該語句將為我生成 drop table 語句。 有以下作用:
選擇“刪除表” || 表名 || ';' 來自用戶表;
然后我可以將 select 語句的輸出通過管道傳輸到一個文件。 運行此程序后,我將有一個文件可以為我刪除所有表。 它看起來像:
刪除表TABLE1;
刪除表TABLE2;
刪除表TABLE3;
等等...
不是 mysql 專家,但我想它會有類似的功能來選擇模式的所有表,以及從 SQL 語句直接輸出到文件。
使用SHOW TABLE STATUS
獲取數據庫中的所有表,然后遍歷結果並一一刪除它們。
評論中有一些解決方案: http : //dev.mysql.com/doc/refman/5.1/en/drop-table.html
執行此操作的程序方法如下:
$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();
建立的連接mysqli_connect();
.
刪除所有表的單行查詢,如下所示:
$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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.