When you use the single-column constraint format to define a check constraint, the check constraint cannot depend on values in other columns of the table. The following example creates the my_accounts table that has two columns with check constraints, each in the single-column constraint format:
CREATE TABLE my_accounts ( chk_id SERIAL PRIMARY KEY, acct1 MONEY CHECK (acct1 BETWEEN 0 AND 99999), acct2 MONEY CHECK (acct2 BETWEEN 0 AND 99999))
Both acct1 and acct2 are columns of MONEY data type whose values must be between 0 and 99999. If, however, you want to test that acct1 has a larger balance than acct2, you cannot use the single-column constraint format. To create a constraint that checks values in more than one column, you must use the Multiple-Column Constraint Format.
Use the constraint definition portion of CREATE TABLE for these purposes:
Constraint Definition: |--+------------------------+-----------------------------------> '-CONSTRAINT--constraint-' >--+-----------------------------------------+------------------| | (1) .-ENABLED----------------------. | '--------+------------------------------+-' +-DISABLED---------------------+ | .-WITHOUT ERROR-. | '-FILTERING--+-WITH ERROR----+-'
Element | Description | Restrictions | Syntax |
---|---|---|---|
constraint | Name of constraint | Must be unique for the table among index and constraint names | Identifier, p. Identifier |