MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Oracle SQL | CHECK Constraints
  1. Creating CHECK Constraints with Column Definition
  2. Creating CHECK Constraints as Table-Level Constraints

  1. Creating CHECK Constraints with Column Definition
    This method defines CHECK constraints directly within the column definition as part of the CREATE TABLE statement. The constraint applies only to the specific column where it is defined and is evaluated whenever data is inserted or updated in that column. You can either let Oracle automatically generate a constraint name (which results in a system-generated name like SYS_C007025) or explicitly specify a meaningful constraint name using the CONSTRAINT keyword.
    CREATE TABLE table_check_1
    (
    id1 NUMBER(1,0) CHECK (id1 > 0) -- the name of the CHECK constraint will be automatically generated by Oracle, e.g., "SYS_C007025"
    , id2 NUMBER(1,0) CONSTRAINT cc_table_check_1_id2 CHECK (id2 > 100) -- the name of the CHECK constraint is explicitly defined as "CC_TABLE_CHECK_1_ID2"
    );
    
    Verify the creation of the constraint:
    SELECT constraint_name, constraint_type, generated, search_condition
    FROM user_constraints
    WHERE table_name = 'TABLE_CHECK_1';
    
    Query result:
    +----------------------+-----------------+----------------+------------------+
    | CONSTRAINT_NAME      | CONSTRAINT_TYPE | GENERATED      | SEARCH_CONDITION |
    +----------------------+-----------------+----------------+------------------+
    | SYS_C007025          | C               | GENERATED NAME | ID1 > 0          |
    +----------------------+-----------------+----------------+------------------+
    | CC_TABLE_CHECK_1_ID2 | C               | USER NAME      | ID2 > 100        |
    +----------------------+-----------------+----------------+------------------+
    
    The constraint type 'C' in the query results indicates a CHECK constraint, and the GENERATED column shows whether the constraint name was system-generated or user-defined.
  2. Creating CHECK Constraints as Table-Level Constraints
    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        |
    +----------------------+-----------------+----------------+------------------+
    
© 2025 mtitek