Column-level constraints
Constraints are rules that are enforced on data columns in a table. Whenever there is any change in data within a table, all the constraints are verified to make sure none is violated. If violated, the changes are rejected with the appropriate error message. Here is a list of column-level constraints:
CHECK <condition>
: A given condition is checked whenever a value is being inserted into the table. The condition is a Boolean expression that should evaluate toTRUE
orNULL
. If it returnsFALSE
for any value, the entire statement is rejected. It is possible to have multiple checks for the same column.
Here's an example to illustrate this:
CREATE TABLE user ( id INT NOT NULL, name STRING NOT NULL, age INT NOT NULL CHECK (age > 18) CHECK (age < 65), PRIMARY KEY (id) );
DEFAULT
: TheDEFAULT
value constraint is exercised whenever theINSERT
statement doesn't explicitly insert a specific value orNULL
for the column...