

The first two constraints should look familiar. Say you store a regular price and a discounted price, and you want to ensure that the discounted price is lower than the regular price:ĭiscounted_price numeric CHECK (discounted_price > 0), (If you don't specify a constraint name in this way, the system chooses a name for you.)Ī check constraint can also refer to several columns. So, to specify a named constraint, use the key word CONSTRAINT followed by an identifier followed by the constraint definition. Price numeric CONSTRAINT positive_price CHECK (price > 0) This clarifies error messages and allows you to refer to the constraint when you need to change it. You can also give the constraint a separate name. The check constraint expression should involve the column thus constrained, otherwise the constraint would not make too much sense. A check constraint consists of the key word CHECK followed by an expression in parentheses.

Default values and constraints can be listed in any order. For instance, to require positive product prices, you could use:Īs you see, the constraint definition comes after the data type, just like default value definitions.

It allows you to specify that the value in a certain column must satisfy a Boolean (truth-value) expression. A check constraint is the most generic constraint type.
