MySQL刪除資料庫命令用法詳解

來源:互聯網
上載者:User

使用cmd模式下載刪除


命令:drop database <資料庫名>

例如:刪除名為 xhkdb的資料庫

 代碼如下 複製代碼

mysql> drop database xhkdb;

例子1:刪除一個已經確定存在的資料庫

  

 代碼如下 複製代碼
mysql> drop database drop_database;
   Query OK, 0 rows affected (0.00 sec)

例子2:刪除一個不確定存在的資料庫

 代碼如下 複製代碼

   mysql> drop database drop_database;
   ERROR 1008 (HY000): Can't drop database 'drop_database'; database doesn't exist
      //發生錯誤,不能刪除'drop_database'資料庫,該資料庫不存在。
   mysql> drop database if exists drop_database;
   Query OK, 0 rows affected, 1 warning (0.00 sec)//產生一個警告說明此資料庫不存在
   mysql> create database drop_database;
   Query OK, 1 row affected (0.00 sec)
   mysql> drop database if exists drop_database;//if exists 判斷資料庫是否存在,不存在也不產生錯誤
   Query OK, 0 rows affected (0.00 sec)


使用mysqladmin刪除資料庫:

需要特殊的許可權才能建立或刪除一個MySQL資料庫。因此,假設以root使用者的訪問,可以使用mysql mysqladmin二進位建立任何資料庫。

小心刪除任何資料庫,因為它會失去資料庫中可用的所有資料。

Here is an example to delete a database created in previous chapter:

[root@host]# mysqladmin -u root -p drop TUTORIALS
Enter password:******
下面是一個例子刪除在前面的章節中建立一個資料庫:

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指令碼刪除資料庫:

PHP使用mysql_query函數來建立或刪除一個MySQL資料庫。這個函數有兩個參數,成功返回TRUE或失敗則返回FALSE。

例子:
bool mysql_query( sql, connection );

參數  描述 

參數 描述
sql Required - SQL query to create or delete a MySQL database
connection Optional - if not specified then last opened connection by mysql_connect will be used.

 

例子:
試試下面的例子中,刪除一個資料庫:

 代碼如下 複製代碼
<html>
<head>
<title>Deleting MySQL Database - by www.yiibai.com</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 successfullyn";
mysql_close($conn);
?>
</body>
</html>

警告: 在刪除一個資料庫,使用PHP指令碼,它不會提示任何確認。所以刪除一個MySQL資料庫一定要小心

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.