同上一節(jié)的情況完全一樣,創(chuàng)建或刪除 MySQL 數(shù)據(jù)庫需要特殊的權(quán)限。假如有了 root 用戶權(quán)限,那就可以用 mysqladmin 二進(jìn)制命令來隨意創(chuàng)建數(shù)據(jù)庫了。
刪除數(shù)據(jù)庫要非常謹(jǐn)慎,因?yàn)檫@樣做會(huì)丟失數(shù)據(jù)庫中所保存的全部數(shù)據(jù)。
在下面這個(gè)范例中,刪除了上一節(jié)所創(chuàng)建的數(shù)據(jù)庫。
[root@host]# mysqladmin -u root -p drop TUTORIALS
Enter password:******
這時(shí),系統(tǒng)會(huì)顯示一條警示消息,詢問是否確定要?jiǎng)h除數(shù)據(jù)庫。
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'TUTORIALS' database [y/N] y
Database "TUTORIALS" dropped
PHP 使用 mysql_query 函數(shù)來創(chuàng)建或刪除 MySQL 數(shù)據(jù)庫。該函數(shù)包含兩個(gè)參數(shù),如果成功執(zhí)行操作,返回 TRUE,否則返回 FALSE。
bool mysql_query( sql, connection );
下面這個(gè)范例展示了如何刪除一個(gè)數(shù)據(jù)庫。
<html>
<head>
<title>Deleting MySQL Database</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
$sql = 'DROP DATABASE TUTORIALS';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete database: ' . mysql_error());
}
echo "Database TUTORIALS deleted successfully\n";
mysql_close($conn);
?>
</body>
</html>
警告:在利用 PHP 腳本刪除數(shù)據(jù)庫時(shí),它不會(huì)提供任何確認(rèn)提示,因此一定要小心。