db2user-defined-data-types

User Defined Datatype with Constraint in DB2


I want to create a user defined data type, let's call it ‘MyPosInt’ which can take only positive integers within 1 to 100 as its value in DB2.

Now I know, I can create a user defined datatype using 'CREATE DATATYPE' query, but how do I impose this constraint.


Solution

  • Check out this:

    CREATE TYPE MyPosInt AS INTEGER WITH WEAK TYPE RULES
           CHECK(VALUE > 0 AND VALUE <= 100)
    

    You will find more details in the documentation