刪除現有MySQL表這是很容易的,但必須非常小心,當刪除任何現有的表,刪除表後丟失的數據將不能恢復。
這是用來刪除MySQL表的通用SQL語法:
DROP TABLE table_name ;
這需要只是在 MySQL>提示符下執行DROP TABLE SQL命令。
下面是一個例子,它將刪除表:tutorials_tbl
root@host# mysql -u root -p Enter password: mysql> use test; Database changed mysql> DROP TABLE tutorials_tbl Query OK, 0 rows affected (0.8 sec) mysql>
在任何數據庫中刪除現有的表,需要使用PHP函數:mysql_query()。通過其第二個參數的正確SQL命令來刪除表。
<html>
<head>
<title>刪除MySQL數據庫中的表</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
$sql = "DROP TABLE tutorials_tbl";
mysql_select_db( 'test' );
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete table: ' . mysql_error());
}
echo "Table deleted successfully\n";
mysql_close($conn);
?>
</body>
</html>