ADD Table Constraint

Description

This form adds a new constraint to a table using the same constraint syntax as CREATE TABLE, plus the option NOT VALID, which is currently only allowed for foreign key and CHECK constraints.

The NOT VALID clause is not supported in Snowflake.

The functionality of the Add table constraint is going to depend on the definition of the constraint, for more information on table constraints and their translation click here.

Click here to navigate to the PostgreSQL documentation page for this syntax.

Grammar Syntax

ADD table_constraint [ NOT VALID ]

table_constraint is:

[ CONSTRAINT constraint_name ]
{ CHECK ( expression ) [ NO INHERIT ] |
  UNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] ) index_parameters |
  PRIMARY KEY ( column_name [, ... ] ) index_parameters |
  EXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] |
  FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
    [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] }
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]

Sample Source Patterns

PostgreSQL

ALTER TABLE ONLY Table1
ADD CONSTRAINT const CHECK (char_length(code) = 5);

Snowflake

ALTER TABLE Table1
ADD CONSTRAINT const UNIQUE (zip)
-- ** MSC-WARNING - MSCEWI1002 - REMOVED NEXT STATEMENT, NOT APPLICABLE IN SNOWFLAKE. NOT VALID **
--                                  NOT VALID
                                           ;
-- ** MSC-WARNING - MSCEWI1035 - CHECK STATEMENT NOT SUPPORTED **
--ALTER TABLE ONLY Table1
--ADD CONSTRAINT const CHECK (char_length(code) = 5)
                                                  ;

Last updated