CREATE DATABASE
說明
使用指定的資料庫名稱建立資料庫。如果已經存在同名的資料庫,系統會擲回例外狀況。
語法
CREATE { DATABASE | SCHEMA } [ IF NOT EXISTS ] database_name
[ COMMENT database_comment ]
[ LOCATION database_directory ]
[ WITH DBPROPERTIES ( property_name = property_value [ , ... ] ) ]
參數
-
database_name
指定要建立的資料庫名稱。
-
IF NOT EXISTS
如果資料庫不存在,則使用指定的資料庫名稱建立資料庫。如果已經存在同名的資料庫,則不執行任何動作。
-
database_directory
要建立指定資料庫的檔案系統路徑。如果指定的路徑不存在於底層檔案系統中,此命令會建立一個具有該路徑的目錄。如果未指定位置,資料庫會建立在預設的倉庫目錄中,其路徑由靜態設定 spark.sql.warehouse.dir 設定。
-
database_comment
指定資料庫的說明。
-
WITH DBPROPERTIES ( property_name=property_value [ , … ] )
以鍵值對指定資料庫的屬性。
範例
-- Create database `customer_db`. This throws exception if database with name customer_db
-- already exists.
CREATE DATABASE customer_db;
-- Create database `customer_db` only if database with same name doesn't exist.
CREATE DATABASE IF NOT EXISTS customer_db;
-- Create database `customer_db` only if database with same name doesn't exist with
-- `Comments`,`Specific Location` and `Database properties`.
CREATE DATABASE IF NOT EXISTS customer_db COMMENT 'This is customer database' LOCATION '/user'
WITH DBPROPERTIES (ID=001, Name='John');
-- Verify that properties are set.
DESCRIBE DATABASE EXTENDED customer_db;
+-------------------------+--------------------------+
|database_description_item|database_description_value|
+-------------------------+--------------------------+
| Database Name| customer_db|
| Description| This is customer database|
| Location| hdfs://hacluster/user|
| Properties| ((ID,001), (Name,John))|
+-------------------------+--------------------------+