Column-constraint
Adds a constraint to a column's metadata. For details, see Constraints.
Syntax
[ { AUTO_INCREMENT | IDENTITY } [ (args) ] ]
[ CONSTRAINT constraint-name ] {
[ CHECK (expression) [ ENABLED | DISABLED ] ]
[ [ DEFAULT expression ] [ SET USING expression } | DEFAULT USING expression ]
[ NULL | NOT NULL ]
[ { PRIMARY KEY [ ENABLED | DISABLED ] REFERENCES table [( column )] } ]
[ UNIQUE [ ENABLED | DISABLED ] ]
}
Parameters
Note
You can specify enforcement of several constraints by qualifying them with the keywordsENABLED or DISABLED. See Enforcing Constraints below.
AUTO_INCREMENT | IDENTITY- Creates a table column whose values are automatically generated by and managed by the database. You cannot change or load values in this column. You can set this constraint on only one table column.
AUTO_INCREMENTandIDENTITYare synonyms. For details on this constraint and optional arguments, see IDENTITY sequences.These options are invalid for temporary tables.
CONSTRAINTconstraint-name- Assigns a name to the constraint, valid for the following constraints:
-
PRIMARY KEY -
REFERENCES(foreign key) -
CHECK -
UNIQUE
If you omit assigning a name to these constraints, Vertica assigns its own name. For details, see Naming constraints.
Vertica recommends that you name all constraints.
-
CHECK (expression)- Adds check condition
expression, which returns a Boolean value. DEFAULT- Specifies this column's default value:
DEFAULT default-exprVertica evaluates the
DEFAULTexpression and sets the column on load operations, if the operation omits a value for the column. For details about valid expressions, see Defining column values. SET USING- Specifies to set values in this column from the specified expression:
SET USING using-exprVertica evaluates the
SET USINGexpression and refreshes column values only when the functionREFRESH_COLUMNSis invoked. For details about valid expressions, see Defining column values. DEFAULT USING- Defines the column with
DEFAULTandSET USINGconstraints, specifying the same expression for both.DEFAULT USINGcolumns support the same expressions asSET USINGcolumns, and are subject to the same restrictions. NULL | NOT NULL- Specifies whether the column can contain null values:
-
NULL: Allows null values in the column. If you set this constraint on a primary key column, Vertica ignores it and sets it toNOT NULL. -
NOT NULL: Specifies that the column must be set to a value during insert and update operations. If the column has no default value and no value is provided,INSERTorUPDATEreturns an error.
If you omit this constraint, the default is
NULLfor all columns except primary key columns, which Vertica always sets toNOT NULL.External tables: If you specify
NOT NULLand the column contains null values, queries are liable to return errors or generate unexpected behavior. SpecifyNOT NULLfor an external table column only if you are sure that the column does not contain nulls. -
PRIMARY KEY- Identifies this column as the table's primary key.
REFERENCES- Identifies this column as a foreign key:
REFERENCES table [column]where
columnis the primary key intable. If you omitcolumn, Vertica references the primary key intable. UNIQUE- Requires column data to be unique with respect to all table rows.
Privileges
Table owner or user WITH GRANT OPTION is grantor.
-
REFERENCES privilege on table to create foreign key constraints that reference this table
-
USAGE privilege on schema that contains the table
Enforcing constraints
The following constraints can be qualified with the keyword ENABLED or DISABLED:
-
PRIMARY KEY -
UNIQUE -
CHECK
If you omit ENABLED or DISABLED, Vertica determines whether to enable the constraint automatically by checking the appropriate configuration parameter:
-
EnableNewPrimaryKeysByDefault -
EnableNewUniqueKeysByDefault -
EnableNewCheckConstraintsByDefault
For details, see Constraint enforcement.