Constraints

  • I'm new at trying to apply constraints to our database so maybe this is a basic question but...

    I have two fields in a table called temp_code char(1) and exp_date date/time.  Is there a way that I can put a constraint on temp_code that if it is = 'Y' then exp_date cannot be null?  The only valid entries for temp_code are 'Y' and 'N'

  • Yes, using a table level constraint:

    -- With this table:

    create table test2 (

    YesNo char(1) check (YesNo in ( 'Y', 'N')),

    otherStuff char(10) null,

    constraint nullSometimes check ( ( YesNo = 'Y' and not(otherStuff is null) ) or YesNo = 'N')

    )

    --These are allowed:

    insert test2 select 'Y', 'Something'

    insert test2 select 'N', null

    insert test2 select 'N', 'Something'

    --These are not:

    insert test2 select 'F', 'Something'

    insert test2 select 'Y', null

  • Thanks for the help Merrill.

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

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