數據定義語句(2)DROP DATABASE

  • 2019 年 11 月 13 日
  • 筆記

刪除資料庫。語法規則如下:

DROP {DATABASE|SCHEMA} [IF EXISTS] db_name

執行 DROP DATABASE 會刪除資料庫里的所有表然後再刪除資料庫,所以執行這條語句的時候一定要慎重。要執行該語句,你需要擁有資料庫的 DROP 許可權。DROP SCHEMADROP DATABASE 可以互相替換。

特別注意: 當在刪除資料庫時,與資料庫相關的授權資訊不會被自動刪除,這些資訊必須手動刪除。

當資料庫不存在時,使用 IF EXISTS 來避免產生錯誤。

DROP DATABASE 的返回值刪除的表的數量。

當刪除內置的資料庫時,只將資料庫清空恢復到初始狀態,不會真正執行刪除操作。

註:引用部分大意是刪除資料庫時會刪除哪些文件,這些與其他內容有關聯,留到後面一起翻譯。

If you use DROP DATABASE on a symbolically linked database, both the link and the original database are deleted. DROP DATABASE returns the number of tables that were removed. This corresponds to the number of .frm files removed. The DROP DATABASE statement removes from the given database directory those files and directories that MySQL itself may create during normal operation: • All files with the following extensions: • .BAK • .DAT • .HSH • .MRG • .MYD • .MYI • .TRG • .TRN • .cfg • .db • .frm • .ibd • .ndb • .par • The db.opt file, if it exists. If other files or directories remain in the database directory after MySQL removes those just listed, the database directory cannot be removed. In this case, you must remove any remaining files or directories manually and issue the DROP DATABASE statement again. Dropping a database does not remove any TEMPORARY tables that were created in that database. TEMPORARY tables are automatically removed when the session that created them ends. See Section 13.1.18.3, 「CREATE TEMPORARY TABLE Syntax」. You can also drop databases with mysqladmin. See Section 4.5.2, 「mysqladmin — Client for Administering a MySQL Server」.