CREATE SCHEMA | Snowflake Documentation (original) (raw)

Creates a new schema in the current database.

This command supports the following variants:

See also:

ALTER SCHEMA , DESCRIBE SCHEMA , DROP SCHEMA , SHOW SCHEMAS , UNDROP SCHEMA

CREATE OR ALTER

Syntax

CREATE [ OR REPLACE ] [ TRANSIENT ] SCHEMA [ IF NOT EXISTS ] [ CLONE [ { AT | BEFORE } ( { TIMESTAMP => | OFFSET => | STATEMENT => } ) ] [ IGNORE TABLES WITH INSUFFICIENT DATA RETENTION ] [ IGNORE HYBRID TABLES ] ] [ WITH MANAGED ACCESS ] [ DATA_RETENTION_TIME_IN_DAYS = ] [ MAX_DATA_EXTENSION_TIME_IN_DAYS = ] [ EXTERNAL_VOLUME = ] [ CATALOG = ] [ REPLACE_INVALID_CHARACTERS = { TRUE | FALSE } ] [ DEFAULT_DDL_COLLATION = '' ] [ STORAGE_SERIALIZATION_POLICY = { COMPATIBLE | OPTIMIZED } ] [ CLASSIFICATION_PROFILE = '' ] [ COMMENT = '' ] [ CATALOG_SYNC = '' ] [ [ WITH ] TAG ( = '' [ , = '' , ... ] ) ]

Variant syntax

CREATE OR ALTER SCHEMA

Creates a new schema if it doesn’t already exist, or transforms an existing schema into the schema defined in the statement. A CREATE OR ALTER SCHEMA statement follows the syntax rules of a CREATE SCHEMA statement and has the same limitations as anALTER SCHEMA statement.

For more information, see CREATE OR ALTER SCHEMA usage notes.

CREATE OR ALTER [ TRANSIENT ] SCHEMA [ WITH MANAGED ACCESS ] [ DATA_RETENTION_TIME_IN_DAYS = ] [ MAX_DATA_EXTENSION_TIME_IN_DAYS = ] [ EXTERNAL_VOLUME = ] [ CATALOG = ] [ REPLACE_INVALID_CHARACTERS = { TRUE | FALSE } ] [ DEFAULT_DDL_COLLATION = '' ] [ LOG_LEVEL = '' ] [ TRACE_LEVEL = '' ] [ STORAGE_SERIALIZATION_POLICY = { COMPATIBLE | OPTIMIZED } ] [ COMMENT = '' ]

CREATE SCHEMA … CLONE

Creates a new schema with the same parameter values:

CREATE [ OR REPLACE ] SCHEMA [ IF NOT EXISTS ] CLONE [ ... ]

For more details, see CREATE … CLONE.

Required parameters

_name_

Specifies the identifier for the schema; must be unique for the database in which the schema is created.

In addition, the identifier must start with an alphabetic character and cannot contain spaces or special characters unless the entire identifier string is enclosed in double quotes (e.g. "My object"). Identifiers enclosed in double quotes are also case-sensitive.

For more details, see Identifier requirements.

Optional parameters

TRANSIENT

Specifies a schema as transient. Transient schemas do not have a Fail-safe period so they do not incur additional storage costs once they leave Time Travel; however, this means they are also not protected by Fail-safe in the event of a data loss. For more information, see Understanding and viewing Fail-safe.

In addition, by definition, all tables created in a transient schema are transient. For more information about transient tables, seeCREATE TABLE.

Default: No value (i.e. schema is permanent)

CLONE _sourceschema_

Specifies to create a clone of the specified source schema. For more details about cloning a schema, see CREATE … CLONE.

AT | BEFORE ( TIMESTAMP => _timestamp_ | OFFSET => _timedifference_ | STATEMENT => _id_ )

When cloning a schema, the AT | BEFORE clause specifies to use Time Travel to clone the schema at or before a specific point in the past.

IGNORE TABLES WITH INSUFFICIENT DATA RETENTION

Ignore tables that no longer have historical data available in Time Travel to clone. If the time in the past specified in the AT | BEFORE clause is beyond the data retention period for any child table in a database or schema, skip the cloning operation for the child table. For more information, seeChild Objects and Data Retention Time.

IGNORE HYBRID TABLES

Ignore hybrid tables, which will not be cloned. Use this option to clone a schema that contains hybrid tables. The cloned schema includes other objects but skips hybrid tables.

If you don’t use this option and your schema contains one or more hybrid tables, the command ignores hybrid tables silently. However, the error handling for schemas that contain hybrid tables will change in an upcoming release; therefore, you may want to add this parameter to your commands preemptively.

WITH MANAGED ACCESS

Specifies a managed schema. Managed access schemas centralize privilege management with the schema owner.

In regular schemas, the owner of an object (i.e. the role that has the OWNERSHIP privilege on the object) can grant further privileges on their objects to other roles. In managed schemas, the schema owner manages all privilege grants, includingfuture grants, on objects in the schema. Object owners retain the OWNERSHIP privileges on the objects; however, only the schema owner can manage privilege grants on the objects.

DATA_RETENTION_TIME_IN_DAYS = _integer_

Specifies the number of days for which Time Travel actions (CLONE and UNDROP) can be performed on the schema, as well as specifying the default Time Travel retention time for all tables created in the schema. For more details, see Understanding & using Time Travel.

For a detailed description of this object-level parameter, as well as more information about object parameters, seeParameters. For more information about table-level retention time, seeCREATE TABLE and Understanding & using Time Travel.

Values:

Default:

Note

A value of 0 effectively disables Time Travel for the schema.

MAX_DATA_EXTENSION_TIME_IN_DAYS = _integer_

Object parameter that specifies the maximum number of days for which Snowflake can extend the data retention period for tables in the schema to prevent streams on the tables from becoming stale.

For a detailed description of this parameter, see MAX_DATA_EXTENSION_TIME_IN_DAYS.

EXTERNAL_VOLUME = _externalvolumename_

Object parameter that specifies the default external volume to use for Apache Iceberg™ tables.

For more information about this parameter, see EXTERNAL_VOLUME.

CATALOG = _catalogintegrationname_

Object parameter that specifies the default catalog integration to use for Apache Iceberg™ tables.

For more information about this parameter, see CATALOG.

REPLACE_INVALID_CHARACTERS = { TRUE | FALSE }

Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character (�) in query results for anIceberg table. You can only set this parameter for tables that use an external Iceberg catalog.

Default: FALSE

DEFAULT_DDL_COLLATION = '_collationspecification_'

Specifies a default collation specification for all tables added to the schema. The default can be overridden at the individual table level.

For more details about the parameter, see DEFAULT_DDL_COLLATION.

LOG_LEVEL = '_loglevel_'

Specifies the severity level of messages that should be ingested and made available in the active event table. Messages at the specified level (and at more severe levels) are ingested.

For more information about levels, see LOG_LEVEL. For information about setting log level, seeSetting levels for logging, metrics, and tracing.

TRACE_LEVEL = '_tracelevel_'

Controls how trace events are ingested into the event table.

For information about levels, see TRACE_LEVEL. For information about setting trace level, seeSetting levels for logging, metrics, and tracing.

STORAGE_SERIALIZATION_POLICY = { COMPATIBLE | OPTIMIZED }

Specifies the storage serialization policy for Apache Iceberg™ tables that use Snowflake as the catalog.

Default: OPTIMIZED

CLASSIFICATION_PROFILE = '_classificationprofile_'

Associates the schema with a classification profile so that sensitive data in the schema isautomatically classified.

COMMENT = '_stringliteral_'

Specifies a comment for the schema.

Default: No value

CATALOG_SYNC = '_snowflakeopencatalogintegrationname_'

Specifies the name of a catalog integration configured for Snowflake Open Catalog. If specified, Snowflake syncs Snowflake-managed Apache Iceberg™ tables in the schema with an external catalog in your Snowflake Open Catalog account. For more information about syncing Snowflake-managed Iceberg tables with Open Catalog, see Sync a Snowflake-managed table with Snowflake Open Catalog.

For more information about this parameter, see CATALOG_SYNC.

Default: No value

TAG ( _tagname_ = '_tagvalue_' [ , _tagname_ = '_tagvalue_' , ... ] )

Specifies the tag name and the tag string value.

The tag value is always a string, and the maximum number of characters for the tag value is 256.

For information about specifying tags in a statement, see Tag quotas for objects and columns.

Access control requirements

A role used to execute this operation must have the followingprivileges at a minimum:

Privilege Object Notes
CREATE SCHEMA Database Can create both regular and managed access schemas.
CREATE SCHEMA … CLONE … WITH MANAGED ACCESS Options The required privileges depends on whether the source schema is managed or unmanaged: Managed: OWNERSHIP on the source schema. Unmanaged: MANAGE GRANTS ON ACCOUNT and USAGE on the source schema.
USAGE External volume, catalog integration Required if setting the EXTERNAL_VOLUME or CATALOG object parameters, respectively.
MODIFY LOG LEVEL Account Required to set the LOG_LEVEL for a schema.
MODIFY TRACE LEVEL Account Required to set the TRACE_LEVEL for a schema.
OWNERSHIP Schema Required only when executing a CREATE OR ALTER SCHEMA statement for an existing schema. OWNERSHIP is a special privilege on an object that is automatically granted to the role that created the object, but can also be transferred using the GRANT OWNERSHIP command to a different role by the owning role (or any role with the MANAGE GRANTS privilege).

For instructions on creating a custom role with a specified set of privileges, see Creating custom roles.

For general information about roles and privilege grants for performing SQL actions onsecurable objects, see Overview of Access Control.

General usage notes

CREATE OR ALTER SCHEMA usage notes

Examples

Create a permanent schema:

CREATE SCHEMA myschema;

SHOW SCHEMAS;

+-------------------------------+--------------------+------------+------------+---------------+--------------+-----------------------------------------------------------+---------+----------------+ | created_on | name | is_default | is_current | database_name | owner | comment | options | retention_time | |-------------------------------+--------------------+------------+------------+---------------+--------------+-----------------------------------------------------------+---------+----------------| | 2018-12-10 09:34:02.127 -0800 | INFORMATION_SCHEMA | N | N | MYDB | | Views describing the contents of schemas in this database | | 1 | | 2018-12-10 09:33:56.793 -0800 | MYSCHEMA | N | Y | MYDB | PUBLIC | | | 1 | | 2018-11-26 06:08:24.263 -0800 | PUBLIC | N | N | MYDB | PUBLIC | | | 1 | +-------------------------------+--------------------+------------+------------+---------------+--------------+-----------------------------------------------------------+---------+----------------+

Create a transient schema:

CREATE TRANSIENT SCHEMA tschema;

SHOW SCHEMAS;

+-------------------------------+--------------------+------------+------------+---------------+--------------+-----------------------------------------------------------+-----------+----------------+ | created_on | name | is_default | is_current | database_name | owner | comment | options | retention_time | |-------------------------------+--------------------+------------+------------+---------------+--------------+-----------------------------------------------------------+-----------+----------------| | 2018-12-10 09:34:02.127 -0800 | INFORMATION_SCHEMA | N | N | MYDB | | Views describing the contents of schemas in this database | | 1 | | 2018-12-10 09:33:56.793 -0800 | MYSCHEMA | N | Y | MYDB | PUBLIC | | | 1 | | 2018-11-26 06:08:24.263 -0800 | PUBLIC | N | N | MYDB | PUBLIC | | | 1 | | 2018-12-10 09:35:32.326 -0800 | TSCHEMA | N | Y | MYDB | PUBLIC | | TRANSIENT | 1 | +-------------------------------+--------------------+------------+------------+---------------+--------------+-----------------------------------------------------------+-----------+----------------+

Create a managed access schema:

CREATE SCHEMA mschema WITH MANAGED ACCESS;

SHOW SCHEMAS;

+-------------------------------+--------------------+------------+------------+---------------+--------------+-----------------------------------------------------------+----------------+----------------+ | created_on | name | is_default | is_current | database_name | owner | comment | options | retention_time | |-------------------------------+--------------------+------------+------------+---------------+--------------+-----------------------------------------------------------+----------------+----------------| | 2018-12-10 09:34:02.127 -0800 | INFORMATION_SCHEMA | N | N | MYDB | | Views describing the contents of schemas in this database | | 1 | | 2018-12-10 09:36:47.738 -0800 | MSCHEMA | N | Y | MYDB | ROLE1 | | MANAGED ACCESS | 1 | | 2018-12-10 09:33:56.793 -0800 | MYSCHEMA | N | Y | MYDB | PUBLIC | | | 1 | | 2018-11-26 06:08:24.263 -0800 | PUBLIC | N | N | MYDB | PUBLIC | | | 1 | | 2018-12-10 09:35:32.326 -0800 | TSCHEMA | N | Y | MYDB | PUBLIC | | TRANSIENT | 1 | +-------------------------------+--------------------+------------+------------+---------------+--------------+-----------------------------------------------------------+----------------+----------------+

CREATE OR ALTER SCHEMA examples

Create a simple schema

Create a schema named s1:

CREATE OR ALTER SCHEMA s1;

Create or alter schema s1 and set properties and parameters:

CREATE OR ALTER SCHEMA s1 WITH MANAGED ACCESS DATA_RETENTION_TIME_IN_DAYS = 5 DEFAULT_DDL_COLLATION = 'de';

Unset a parameter previously set on schema

The absence of a previously set parameter in the modified schema definition results in unsetting it. In the following example, turn off managed access for the schema s1 created in the previous example:

CREATE OR ALTER SCHEMA s1 DATA_RETENTION_TIME_IN_DAYS = 5 DEFAULT_DDL_COLLATION = 'de';