CREATE DATABASECREATE DATABASE

创建具有指定名称的数据库。Creates a database with the specified name. 如果已存在同名数据库,会引发异常。If database with the same name already exists, an exception is thrown.

语法Syntax

CREATE { DATABASE | SCHEMA } [ IF NOT EXISTS ] database_name
    [ COMMENT database_comment ]
    [ LOCATION database_directory ]
    [ WITH DBPROPERTIES ( property_name = property_value [ , ... ] ) ]

参数Parameters

  • database_namedatabase_name

    要创建的数据库的名称。The name of the database to be created.

  • IF NOT EXISTSIF NOT EXISTS

    创建具有给定名称的数据库(如果不存在)。Creates a database with the given name if it does not exist. 如果已存在同名数据库,则不会执行任何操作。If a database with the same name already exists, nothing will happen.

  • database_directorydatabase_directory

    要在其中创建指定数据库的文件系统的路径。Path of the file system in which the specified database is to be created. 如果基础文件系统中不存在指定的路径,则使用该路径创建一个目录。If the specified path does not exist in the underlying file system, creates a directory with the path. 如果未指定位置,则在默认仓库目录中创建数据库,其路径由静态配置 spark.sql.warehouse.dir 进行配置。If the location is not specified, the database is created in the default warehouse directory, whose path is configured by the static configuration spark.sql.warehouse.dir.

  • database_commentdatabase_comment

    数据库的描述。The description for the database.

  • WITH DBPROPERTIES ( property_name=property_value [ , … ] )WITH DBPROPERTIES ( property_name=property_value [ , … ] )

    以键值对形式表示的数据库属性。The properties for the database in key-value pairs.

示例Examples

-- 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))|
+-------------------------+--------------------------+