This method defines CHECK constraints at the table level, separate from individual column definitions.
Table-level CHECK constraints are declared after all column definitions and can reference multiple columns within the same table, making them useful for complex business rules that involve relationships between columns.
Like column-level constraints, you can either allow Oracle to generate the constraint name automatically or specify a custom name using the CONSTRAINT keyword.
CREATE TABLE table_check_2
(
id1 NUMBER(1,0)
, id2 NUMBER(1,0)
, CHECK (id1 > 0) -- the name of the CHECK constraint will be automatically generated by Oracle, e.g., "SYS_C007027"
, CONSTRAINT cc_table_check_2_id2 CHECK (id2 > 100) -- the CHECK constraint name is explicitly defined here as "CC_TABLE_CHECK_2_ID2"
);
Verify the creation of the constraint:
SELECT constraint_name, constraint_type, generated, search_condition
FROM user_constraints
WHERE table_name = 'TABLE_CHECK_2';
Query result:
+----------------------+-----------------+----------------+------------------+
| CONSTRAINT_NAME | CONSTRAINT_TYPE | GENERATED | SEARCH_CONDITION |
+----------------------+-----------------+----------------+------------------+
| SYS_C007027 | C | GENERATED NAME | ID1 > 0 |
+----------------------+-----------------+----------------+------------------+
| CC_TABLE_CHECK_2_ID2 | C | USER NAME | ID2 > 100 |
+----------------------+-----------------+----------------+------------------+