CREATE SCHEMA | Databricks on AWS (original) (raw)

Applies to: Databricks SQL Databricks Runtime

Creates a schema (database) with the specified name. If a schema with the same name already exists, an exception is thrown.

Syntax

CREATE SCHEMA [ IF NOT EXISTS ] schema_name
    [ COMMENT schema_comment |
      DEFAULT COLLATION default_collation_name |
      { LOCATION schema_directory | MANAGED LOCATION location_path } |
      RETAIN DROPPED FOR number { HOUR | HOURS | DAY | DAYS | WEEK | WEEKS } |
      WITH DBPROPERTIES ( { property_name = property_value } [ , ... ] ) ] [...]

Parameters

Examples

SQL

-- 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 `experimental` with a case insensitive unicode default collation
> CREATE SCHEMA experimental DEFAULT COLLATION UNICODE_CI;

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

-- Create schema with a different managed storage location than the metastore's. MANAGED LOCATION is supported only in Unity Catalog.
> CREATE SCHEMA customer_sc MANAGED LOCATION 's3://depts/finance';

-- Create a schema with a 14-day recovery period for dropped managed tables.
> CREATE SCHEMA customer_sc RETAIN DROPPED FOR 14 DAYS;

-- 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/samplepath
                Properties    ((ID,001), (Name,John))