CREATE HYBRID TABLE

statementsupportedTables, views, streams, sequences

docs.snowflake.com ↗

Object: HYBRID TABLE

Creates a new hybrid table in the current/specified schema or replaces an existing table. A table can have multiple columns, with each column definition consisting of a name, data type, and optionally whether the column: - Requires a NOT NULL value. - Has a default value or is an identity column. - Has any inline constraints. Note When you create a hybrid table, you must define a PRIMARY KEY constraint on one or more columns. You can also use the following CREATE TABLE variants to create hybrid tables: - CREATE HYBRID TABLE … AS SELECT (CTAS) (creates a populated table; also referred to as CTAS) - CREATE HYBRID TABLE … LIKE (creates an empty copy of an existing hybrid table) For the full CREATE TABLE syntax used for standard Snowflake tables, see CREATE TABLE. Tip Before creating and using hybrid tables, you should become familiar with some unsupported features and limitations. Generally available to accounts in AWS, Microsoft Azure, and Google Cloud commercial regions. For more information, see Clouds and regions.

Syntax

supportedpartialnot supportednot scored

CREATE [ OR REPLACE ] HYBRID TABLE [ IF NOT EXISTS ] <table_name>
  ( <col_name> <col_type>
    [
      {
        DEFAULT <expr>
        | { AUTOINCREMENT | IDENTITY }
          [
            {
              ( <start_num> , <step_num> )
              | START <num> INCREMENT <num>
            }
          ]
          [ { ORDER | NOORDER } ]
      }
    ]
    [ NOT NULL ]
    [ inlineConstraint ]
    [ COLLATE '<collation_specification>' ]
    [ COMMENT '<string_literal>' ]
    [ , <col_name> <col_type> [ ... ] ]
    [ , outoflineConstraint ]
    [ , outoflineIndex ]
    [ , ... ]
  )
  [ COMMENT = '<string_literal>' ]

Where

inlineConstraint ::=
  [ CONSTRAINT <constraint_name> ]
  { UNIQUE
    | PRIMARY KEY
    | { [ FOREIGN KEY ] REFERENCES <ref_table_name> [ ( <ref_col_name> ) ] }
    | CHECK ( <expr> )
  }
  [ <constraint_properties> ]

outoflineConstraint ::=
  [ CONSTRAINT <constraint_name> ]
  { UNIQUE [ ( <col_name> [ , <col_name> , ... ] ) ]
    | PRIMARY KEY [ ( <col_name> [ , <col_name> , ... ] ) ]
    | [ FOREIGN KEY ] [ ( <col_name> [ , <col_name> , ... ] ) ]
      REFERENCES <ref_table_name> [ ( <ref_col_name> [ , <ref_col_name> , ... ] ) ]
    | CHECK ( <expr> )
  }
  [ <constraint_properties> ]
  [ COMMENT '<string_literal>' ]

outoflineIndex ::=
  INDEX <index_name> ( <col_name> [ , <col_name> , ... ] )
    [ INCLUDE ( <col_name> [ , <col_name> , ... ] ) ]

Parameters

NameRequiredDescription
namerequiredSpecifies the identifier (i.e. name) for the table; must be unique for the schema in which the table 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 (for example, "My object"). Identifiers enclosed in double quotes are also case-sensitive. For more information, see Identifier requirements.
col_namerequiredSpecifies the column identifier (i.e. name). All the requirements for table identifiers also apply to column identifiers. For more details, see Identifier requirements and Reserved & limited keywords. Note In addition to the standard reserved keywords, the following keywords cannot be used as column identifiers because they are reserved for ANSI-standard context functions: CURRENT_DATE CURRENT_ROLE CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER For the list of reserved keywords, see Reserved & limited keywords.
col_typerequiredSpecifies the data type for the column. For details about the data types that can be specified for table columns, see SQL data types reference.
PRIMARY KEY ( col_name [ , col_name , ... ] )requiredSpecifies the required primary key constraint for the table, either within a column definition (inline) or separately (out-of-line). See also Constraints for hybrid tables. For complete syntax details, see CREATE | ALTER TABLE … CONSTRAINT. For general information about constraints, see Constraints.
DEFAULT ... or AUTOINCREMENT ...optionalSpecifies whether a default value is automatically inserted in the column if a value is not explicitly specified via an INSERT or CREATE HYBRID TABLE AS SELECT statement: Default: No value (the column has no default value) Note DEFAULT and AUTOINCREMENT are mutually exclusive; only one can be specified for a column. For performance-sensitive workloads, NOORDER is the recommended option for AUTOINCREMENT columns.
  • DEFAULT expr — Column default value is defined by the specified expression which can be any of the following: Constant value. Simple expression. Sequence reference (seq_name.NEXTVAL). A simple expression is an expression that returns a scalar value; however, the expression cannot contain references to: Subqueries. Aggregates. Window functions. External functions.
  • { AUTOINCREMENT | IDENTITY } [ { ( start_num , step_num ) | START num INCREMENT num } ] [ { ORDER | NOORDER } ] — When AUTOINCREMENT is used, the default value for the column starts with a specified number and each successive value is automatically generated. Values generated by an AUTOINCREMENT column are guaranteed to be unique. The difference between any pair of the generated values is guaranteed to be a multiple of the increment amount. The optional ORDER and NOORDER parameters specify whether or not the generated values provide ordering guarantees as specified in Sequence Semantics. NOORDER is the default option for AUTOINCREMENT columns on hybrid tables. NOORDER typically provides significantly better performance for point writes. These parameters can only be used for columns with numeric data types (NUMBER, INT, FLOAT, etc.) AUTOINCREMENT and IDENTITY are synonymous. If either is specified for a column, Snowflake utilizes a sequence to generate the values for the column. For more information about sequences, see Using Sequences. The default value for both start and step/increment is 1.
CONSTRAINT ...optionalDefines an inline or out-of-line constraint for the specified column(s) in the table. UNIQUE, FOREIGN KEY, and CHECK constraints are optional for hybrid table columns. See also Constraints for hybrid tables and CHECK constraints. For complete syntax details, see CREATE | ALTER TABLE … CONSTRAINT. For general information about constraints, see Constraints.
COLLATE 'collation_specification'optionalSpecifies the collation to use for column operations such as string comparisons. This parameter applies only to text columns that are not indexed. For more information, see Collations on hybrid table columns and Collation specifications.
INDEX index_name ( col_name [ , col_name , ... ]optionalSpecifies a secondary index on one or more columns in the table. (When you define constraints on hybrid table columns, indexes are automatically created on those columns.) Indexes cannot be defined on the following columns: Semi-structured columns (VARIANT, OBJECT, ARRAY) because of space constraints associated with the underlying storage engines for the key of each record. Geospatial columns (GEOGRAPHY, GEOMETRY) or VECTOR columns. TIMESTAMP_TZ columns (or TIMESTAMP columns that resolve to TIMESTAMP_TZ). TIMESTAMP_NTZ columns are supported. Indexes can be defined when the table is created, or with the CREATE INDEX command. For more information about creating indexes for hybrid tables, see Index hybrid tables and CREATE INDEX.
INCLUDE ( col_name [ , col_name , ... ] )optionalSpecifies one or more included columns for a secondary index. Using included columns with a secondary index is particularly useful when queries frequently contain a set of columns in the SELECT list but not in the list of WHERE predicates. See INCLUDE columns. INCLUDE columns cannot be semi-structured columns (VARIANT, OBJECT, ARRAY) or geospatial columns (GEOGRAPHY, GEOMETRY).
COMMENT = 'string_literal'optionalSpecifies a comment at the column, constraint, or table level. For details, see Comments on constraints. Default: No value

Usage notes

Access control requirements

A role used to execute this operation must have the following privileges at a minimum: Privilege | Object | Notes CREATE HYBRID TABLE | Schema | None. SELECT | Table, external table, view | Required on queried tables and/or views only when cloning a table or executing CTAS statements. APPLY | Masking policy, row access policy, tag | Required only when applying a masking policy, row access policy, object tags, or any combination of these governance features when creating tables. Operating on an object in a schema requires at least one privilege on the parent database and at least one privilege on the parent schema.

Constraints for hybrid tables

The following rules apply to constraints that are defined on hybrid tables. - A hybrid table must be created with a PRIMARY KEY constraint. Multi-column (or composite) primary keys are supported. To define a multi-column primary key, use the syntax shown in the following example, where the constraint is defined “out of line” and refers to multiple columns that were previously defined for the table: Copy code CREATE OR REPLACE HYBRID TABLE ht2pk ( col1 INTEGER NOT NULL, col2 INTEGER NOT NULL, col3 VARCHAR, CONSTRAINT pkey_1 PRIMARY KEY (col1, col2) ); - PRIMARY KEY, UNIQUE, FOREIGN KEY, and CHECK constraints are all enforced on hybrid tables. Setting the NOT ENFORCED property on a PRIMARY KEY, UNIQUE, or FOREIGN KEY constraint returns an error. A CHECK constraint accepts NOT ENFORCED without returning an error, but Snowflake then creates no constraint at all rather than an unenforced one, so don’t use NOT ENFORCED to define an advisory CHECK constraint. - PRIMARY KEY, UNIQUE, and FOREIGN KEY constraints build their own underlying indexes. The creation of indexes results in additional data being stored. Secondary (or covering) indexes can also be defined explicitly when the table is created, using the outoflineIndex syntax. - Constraints are enforced at the row level, not at the statement or transaction level (that is, deferred constraints). - PRIMARY KEY and CHECK constraints can be defined only when the table is created. You can add and drop UNIQUE and FOREIGN KEY constraints on an existing hybrid table by using ALTER TABLE. For more information, see Add and drop constraints on an existing hybrid table. - CHECK constraints are enforced on every write. You can rename or drop a CHECK constraint on an existing hybrid table, but you can’t add one. For more information, see CHECK constraints. - You cannot alter a column to be UNIQUE. To add a UNIQUE constraint to an existing hybrid table, use the out-of-line ALTER TABLE … ADD CONSTRAINT syntax. The following rules apply specifically to FOREIGN KEY constraints: - A foreign key in a hybrid table that references a primary key cannot be NULL. If you attempt to load a NULL value into a column that has a FOREIGN KEY constraint, the load operation fails with a constraint error. See Create two hybrid tables with a primary-key/foreign-key relationship. - FOREIGN KEY constraints are supported only among hybrid tables that belong to the same database. - The referenced table from a FOREIGN KEY constraint cannot be truncated as long as the FOREIGN KEY relationship exists. - FOREIGN KEY constraints do not support partial matching. - FOREIGN KEY constraints do not support deferrable behavior. - FOREIGN KEY constraints only support RESTRICT and NO ACTION properties for DELETE and UPDATE operations. A CHECK constraint enforces a SQL expression as a condition on the values that can be inserted into or updated in one or more columns. Hybrid tables enforce CHECK constraints on every write, the same way standard tables do. For general information about the constraint and its expression rules, see CHECK constraints. The following rules are specific to hybrid tables: - You can define a CHECK constraint only when you create the table. Every form of CREATE HYBRID TABLE supports the constraint, including CREATE HYBRID TABLE … AS SELECT (CTAS) and CREATE HYBRID TABLE … LIKE. Adding a CHECK constraint to an existing hybrid table with ALTER TABLE … ADD CONSTRAINT isn’t supported. - You can rename or drop an existing CHECK constraint with ALTER TABLE … RENAME CONSTRAINT or ALTER TABLE … DROP CONSTRAINT. Because you can’t add a CHECK constraint to an existing hybrid table, dropping one is permanent. To restore the constraint, you must re-create the table. - ALTER TABLE … ALTER CONSTRAINT isn’t supported for a CHECK constraint on a hybrid table, so you can’t change the constraint to VALIDATE or NOVALIDATE after you create the table. - When you load a hybrid table with COPY INTO <table>, a row that violates a CHECK constraint causes the load to fail, because COPY INTO supports only ON_ERROR = ABORT_STATEMENT for hybrid tables. Hybrid tables don’t support error logging. To load the valid rows and keep the rejected rows, use a standard table as an intermediate step: Create a standard table that has the same columns and CHECK constraints, with error logging turned on. - Load the files into the standard table with ON_ERROR = CONTINUE. The rejected rows are recorded in the standard table’s error table. - Insert the rows from the standard table into the hybrid table with INSERT INTO … SELECT. Standard tables don’t enforce PRIMARY KEY, UNIQUE, or FOREIGN KEY constraints, so this statement can still fail on rows that violate them. For more information, see COPY limitations for hybrid tables. As with standard tables, an inline CHECK constraint can reference only the column it’s defined on. Define the constraint out of line to enforce a condition that spans multiple columns. The following example defines an inline CHECK constraint on a single column and a named out-of-line CHECK constraint that spans two columns: An INSERT, UPDATE, or MERGE statement that violates either constraint fails, and the row isn’t written: Because sale_price is greater than list_price, the statement violates the out-of-line constraint and returns an error that identifies the constraint by name: If you don’t name a constraint, Snowflake generates a name for it, and the error message reports that generated name instead. Naming your CHECK constraints makes these errors easier to interpret. You can add and drop UNIQUE and FOREIGN KEY constraints on a hybrid table that’s already in use without taking the table offline. Snowflake builds the index that backs the constraint in the background, and the table stays available for SELECT and DML statements while the build runs. This behavior matches the online index builds that CREATE INDEX performs. The following example adds a foreign key to a player table that was created without one. For a version of these tables that declares the foreign key when the table is created, see Create two hybrid tables with a primary-key/foreign-key relationship. The ALTER TABLE statement returns as soon as Snowflake accepts the change. Because the build runs in the background, a successful statement doesn’t mean the constraint is fully built. Use SHOW INDEXES to track the build: The status column reports BUILD IN PROGRESS while the index is being built and ACTIVE when the constraint is fully in place. Snowflake builds one index at a time for a given hybrid table. If you submit a second ADD CONSTRAINT statement while a build is still running, the second statement fails with an error that tells you to wait for the current build to finish or to cancel that build. Adding a constraint validates the rows that are already in the table. If any of them violate the new constraint, the ALTER TABLE statement still succeeds because validation happens during the background build. SHOW INDEXES reports BUILD VALIDATION FAILURE in the status column, and the status_info column explains that the existing data violates the constraint. A constraint left in this state still enforces every new write. Statements that would violate it fail, valid statements succeed, and TRUNCATE TABLE on a referenced table fails. Only the rows that were already in the table when you added the constraint remain unvalidated. SHOW INDEXES is the only command that reports this state. The constraint-oriented metadata doesn’t: SHOW IMPORTED KEYS, SHOW PRIMARY KEYS, the TABLE_CONSTRAINTS view view, and GET_DDL all list the constraint exactly as they would if it had validated. Check SHOW INDEXES before you rely on a constraint that you added to a table that already contained rows. To recover, drop the constraint, correct the offending rows, and add the constraint again:

Collations on hybrid table columns

Collations are not supported on PRIMARY KEY columns and other indexed columns in hybrid tables. However, if you do not intend to index a column, and the column has a character data type, you can specify a COLLATE clause for that column. For example: In some cases, you might need to disable collation for hybrid table columns by using the DEFAULT_DDL_COLLATION = '' syntax, which applies to all columns in the table. You might need to do this when a default collation is set at the account level or for all columns in all tables in a schema or database. For example: Table ht2 is defined without a collation setting on the indexed column c2, despite the fact that the DEFAULT_DDL_COLLATION parameter is set to 'de' at the schema level. For general information about collations, see Collation control.

CREATE HYBRID TABLE … AS SELECT (CTAS)

Creates a new hybrid table that contains the results of a query: Note When you use a CTAS statement to create a hybrid table, you must define the table schema explicitly. You must specify the following table properties in the syntax before the definition of the query: - Column definitions - A PRIMARY KEY constraint - Other constraints, as needed (UNIQUE, NOT NULL, CHECK) - Secondary indexes (and any INCLUDE columns) The schema of the new hybrid table can’t be inferred from a SELECT statement. You can’t define a FOREIGN KEY constraint in a CTAS statement, in either the inline or the out-of-line form. The statement fails and no table is created. If you use CREATE OR REPLACE, an existing table with the same name is left unchanged: To load a table that needs a foreign key, create the table with the constraint, then load it with COPY INTO <table> or INSERT INTO … SELECT. If the table also has a CHECK constraint, use INSERT INTO … SELECT, because COPY INTO can’t load a hybrid table that has a CHECK constraint. Alternatively, create and load the table with CTAS, then add the constraint with ALTER TABLE. The backing index builds in the background, so use SHOW INDEXES to confirm that it reaches ACTIVE. If the status is BUILD VALIDATION FAILURE, some existing rows violate the constraint. For how to recover, see Add and drop constraints on an existing hybrid table. The number of column names specified must match the number of SELECT list items in the query. To create the table with rows in a specific order, use an ORDER BY clause at the end of the query. For information about loading hybrid tables, see Loading data.

CREATE HYBRID TABLE … LIKE

Creates a new hybrid table with the same column definitions as an existing hybrid table, but without copying data from the existing table. Column names, types, defaults, constraints, and indexes are copied to the new table: Note CREATE HYBRID TABLE … LIKE only supports another hybrid table as the source table type. CREATE HYBRID TABLE … LIKE for a table with an auto-increment sequence accessed through a data share is not supported.

See also

Referenced by