CREATE | ALTER TABLE
statementsupportedTables, views, streams, sequencesThis topic describes how to create constraints by specifying a CONSTRAINT clause in a CREATE TABLE, CREATE HYBRID TABLE, or ALTER TABLE statement: - An inline constraint is specified as part of the individual column definition. - An out-of-line constraint is specified as an independent clause: When creating a table, the clause is part of the column definitions for the table. - When altering a table, the clause is specified as an explicit ADD action for the table. For more information, see Constraints. If you are creating or altering hybrid tables, the syntax for defining constraints is the same; however, the rules and requirements are different.
Parameters
| Name | Required | Description |
|---|---|---|
{ ENFORCED | NOT ENFORCED } | Specifies whether the constraint is enforced in a transaction. For standard tables, NOT NULL and CHECK are the only types of constraints that are enforced by Snowflake, regardless of this property. For hybrid tables, you can’t set the NOT ENFORCED property on PRIMARY KEY, FOREIGN KEY, and UNIQUE constraints. Setting this property results in an “invalid constraint property” error. See also Referential Integrity Constraints. Default: NOT ENFORCED | |
{ DEFERRABLE | NOT DEFERRABLE } | Specifies whether, in subsequent transactions, the constraint check can be deferred until the end of the transaction. Default: NOT DEFERRABLE | |
INITIALLY { DEFERRED | IMMEDIATE } | For DEFERRABLE constraints, specifies whether the check for the constraints can be deferred, starting from the next transaction. Default: INITIALLY DEFERRED | |
{ ENABLE | DISABLE } | Specifies whether the constraint is enabled or disabled. These properties are provided for compatibility with Oracle. Default: DISABLE | |
{ VALIDATE | NOVALIDATE } | Specifies whether to validate existing data on the table when a constraint is created. Applies only when either { ENFORCED | NOT ENFORCED } or { ENABLE | DISABLE } is specified. Default for PRIMARY KEY and FOREIGN KEY constraints: NOVALIDATE Default for CHECK constraints: VALIDATE For hybrid tables, adding a UNIQUE or FOREIGN KEY constraint with ALTER TABLE always validates the rows that are already in the table, regardless of this property. See Add and drop constraints on an existing hybrid table. | |
{ RELY | NORELY } | Specifies whether a constraint in NOVALIDATE mode is taken into account during query rewrite. If you have ensured that the data in the table complies with the constraints, you can change this property to RELY to indicate that the query optimizer should expect such data integrity. For standard tables, it is your responsibility to enforce RELY constraints; otherwise, you might risk unintended behavior and unexpected results. If the RELY property is set for a constraint and a violation of referential integrity occurs, DML and CTAS statements might insert incorrect data. Setting the RELY property might improve query performance (for example, by eliminating unnecessary joins). For related PRIMARY KEY and FOREIGN KEY constraints, set this property on both constraints. For example:Copy code ALTER TABLE table_with_primary_key ALTER CONSTRAINT a_primary_key_constraint RELY; ALTER TABLE table_with_foreign_key ALTER CONSTRAINT a_foreign_key_constraint RELY; Default: NORELY | |
MATCH { FULL | PARTIAL | SIMPLE } | Specifies whether the FOREIGN KEY constraint is satisfied with regard to NULL values in one or more of the columns. Default: MATCH FULL | |
UPDATE { CASCADE | SET NULL | SET DEFAULT | RESTRICT | NO ACTION } | Specifies the action performed when the primary or unique key for the foreign key is updated. Default: UPDATE NO ACTION | |
DELETE { CASCADE | SET NULL | SET DEFAULT | RESTRICT | NO ACTION } | Specifies the action performed when the primary or unique key for the foreign key is deleted. Default: DELETE NO ACTION | |
CHECK ( expr ) | An expression that defines the condition to enforce. The expression can contain any of the following items: Table columns defined in the table on which the CHECK constraint operates. Constant values. Scalar functions that don’t rely on the environment or execution context. An inline CHECK constraint can reference only the column it’s defined on. To enforce a condition that spans multiple columns, define the constraint out of line. The expression can’t contain any of the following items: User-defined functions (UDFs). Aggregate functions, window functions, table functions, or subqueries. System-defined functions that change database state, such as the SYSTEM$CANCEL_ALL_QUERIES function. Non-deterministic system-defined functions, such as the RANDOM function. System-defined functions that rely on the environment or execution context, such as the CURRENT_DATE function or the CURRENT_ROLE function. For hybrid tables, you can specify a CHECK constraint only in a CREATE HYBRID TABLE statement, not in an ALTER TABLE … ADD CONSTRAINT statement. You can’t add a CHECK constraint to a table that a pipe loads, even if the pipe is paused. For more information, see CHECK constraints, CHECK constraints for hybrid tables, and Change the CHECK constraints on a table that a pipe loads. |
Usage notes
- NOT NULL specifies that the column doesn’t allow NULL values: For standard Snowflake tables, NOT NULL and CHECK are the only types of constraints that are enforced. See Referential Integrity Constraints.
- It can be specified only as an inline constraint within the column definition.
- The default is to allow NULL values in columns.
- Multi-column constraints (composite unique or primary keys) can only be defined out-of-line.
- When defining foreign keys, either inline or out-of-line, column name(s) for the referenced table do not need to be specified if the signature (name and data type) of the foreign key column(s) and the referenced table’s primary key column(s) exactly match.
- NOT NULL and CHECK constraints can’t be set on virtual columns.
- If you create a foreign key, the columns in the REFERENCES clause must be listed in the same order as they were listed for the primary key. For example: Copy code CREATE TABLE parent ... CONSTRAINT primary_key_1 PRIMARY KEY (c_1, c_2) ... CREATE TABLE child ... CONSTRAINT foreign_key_1 FOREIGN KEY (...) REFERENCES parent (c_1, c_2) ... In both cases, the order of the columns is c_1, c_2. If the order of the columns in the foreign key had been different (for example, c_2, c_1), the attempt to create the foreign key would have failed.
Syntax for inline constraints
Where:
Syntax for out-of-line constraints
Where:
Comments on constraints
Similar to other database objects and constructs, Snowflake supports comments on constraints: - Out-of-line constraints support the COMMENT clause within the constraint definition. Copy code CREATE OR REPLACE TABLE uni (c1 INT, c2 int, CONSTRAINT uni1 UNIQUE(C1) COMMENT 'Unique column'); - A COMMENT clause within the column definition can be used to comment on the column itself or its constraint: Copy code CREATE OR REPLACE TABLE uni (c1 INT UNIQUE COMMENT 'Unique column', c2 int); Note the following limitations: - You can’t set comments on constraints by using the COMMENT command. - The DESCRIBE TABLE command shows comments defined on columns, but not comments defined on constraints. To see comments on constraints, select from the TABLE_CONSTRAINTS view or the REFERENTIAL_CONSTRAINTS view. - The COMMENT clause within column and constraint definitions does’t support the equals sign (=). Do not specify: Copy code COMMENT = 'My comment' Use the syntax shown in the previous examples: Copy code COMMENT 'My comment'
Access control requirements
For creating PRIMARY KEY, UNIQUE, or CHECK constraints: - When altering an existing table to add the constraint, you must use a role that has the OWNERSHIP privilege on the table. - When creating a new table, you must use a role that has the CREATE TABLE privilege on the schema where the table will be created. For creating FOREIGN KEY constraints: - You must use a role that has the OWNERSHIP privilege on the foreign key table. - You must use a role that has the REFERENCES privilege on the unique or primary key table. The REFERENCES privilege can be granted to and revoked from roles using the GRANT <privileges> … TO ROLE and REVOKE <privileges> … FROM ROLE commands:
Examples of constraints with standard tables
For examples of constraints with hybrid tables, see CREATE HYBRID TABLE. The example below shows how to create a simple NOT NULL constraint while creating a table, and another NOT NULL constraint while altering a table: Create a table and create a constraint at the same time: Alter the table to add a column with a constraint: The following example specifies that the intent of the column is to hold unique values, but makes clear that the constraint is not actually enforced. This example also demonstrates how to specify a name for the constraint (“uniq_col3” in this case.) The following creates a parent table with a PRIMARY KEY constraint and another table with a FOREIGN KEY constraint that points to the same columns as the first table’s PRIMARY KEY constraint. The following example specifies an inline CHECK constraint in a CREATE TABLE statement: This CHECK constraint fails for the following DML operations because the quantity is a negative value or zero: The following example specifies an out-of-line CHECK constraint on multiple columns: The CHECK constraint ensures that price doesn’t exceed the maximum price. The following example specifies an inline CHECK constraint in a CTAS statement: The CHECK constraint ensures that the new high_value_products table only contains items that are considered to be high-priced.