CREATE SCHEMA

创建具有指定名称的架构。 如果已存在同名架构,则会引发异常。

语法

CREATE SCHEMA [ IF NOT EXISTS ] schema_name
    [ COMMENT schema_comment ]
    [ LOCATION schema_directory ]
    [ WITH DBPROPERTIES ( property_name = property_value [ , ... ] ) ]

参数

  • schema_name

    要创建的架构的名称。

  • IF NOT EXISTS

    创建具有给定名称的架构(如果不存在)。 如果已存在同名架构,则不会执行任何操作。

  • schema_directory

    要在其中创建指定架构的文件系统的路径。 如果基础文件系统中不存在指定的路径,则使用该路径创建一个目录。 如果未指定位置,则在默认仓库目录中创建架构,其路径由静态配置 spark.sql.warehouse.dir 进行配置。

警告

为避免意外数据丢失,请不要将架构(数据库)注册到具有现有数据的位置,或者在由架构管理的位置创建新的外部表。 删除架构会递归删除托管位置中的所有数据文件。

  • schema_comment

    架构说明。

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

    以键值对形式表示的架构属性。

示例

-- Create schema `customer_sc`. This throws exception if schema with name customer_sc
-- already exists.
> CREATE SCHEMA customer_sc;

-- Create schema `customer_sc` only if schema with same name doesn't exist.
> CREATE SCHEMA IF NOT EXISTS customer_sc;

-- Create schema `customer_sc` only if schema with same name doesn't exist with
-- `Comments`,`Specific Location` and `Database properties`.
> CREATE SCHEMA IF NOT EXISTS customer_sc COMMENT 'This is customer schema' LOCATION '/user'
    WITH DBPROPERTIES (ID=001, Name='John');

-- Verify that properties are set.
> DESCRIBE SCHEMA EXTENDED customer_sc;
 database_description_item database_description_value
 ------------------------- --------------------------
             Database Name                customer_sc
               Description  This is customer schema
                  Location      hdfs://hacluster/user
                Properties    ((ID,001), (Name,John))