Valid values should be permit in the column of a Table while insertion of records

  • Suppose there is a Gender column in a Table which have only 2 permissible values as 'M' and 'F' for Male and Females. No other values can be inserted in that column while inserting the records except 'M' and 'F'.

    There are some more columns which have set of permissible values

    How the constraint could be apply on that that type of columns?Is there any general way to restrict the values of columns in the database?

    Should I write a trigger or what should i do for such a scenario?

    Thanks in advance if anyone can help me.....

  • You can use a CHECK contraint to enforce that only certain values can be entered. This can be done when you create the table (in-line) or added to the table later one. For example:

    CREATE TABLE TEST

    (

    GENDER CHAR(1),

    CONSTRAINT chk_Gender CHECK(GENDER IN ('M', 'F')

    )

    or

    ALTER TABLE TEST

    ADD CHECK(GENDER IN ('M', 'F')

    Eli

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply