Re: Multi column check constraint

From: Philip Lijnzaad <lijnzaad_at_ebi.ac.uk>
Date: 2000/05/19
Message-ID: <u7aehm7gwz.fsf_at_o2-3.ebi.ac.uk>#1/1


Jeff> Can a check constraint be written such that if item_link_no is null then Jeff> voucher_id must be not null and vice versa?

It depends on the database; not all of them support table constraints (i.e. constraints that involve more than one column). If the do, following is what it would look like. This is Oracle syntax, but I don' tthink it's far off the standard:

alter table foobar add constraint exactly_one_null check (

(item_link_no is not null and voucher_id is not null) or
(item_link_no is null and voucher_id is not null) or
(item_link_no is not null and voucher_id is null));

A more elegant way is to translate the columns to 0/1 integers; using oracle's decode (and others db's have similar functions):

 check( decode(item_link_no,null,0,1)+decode(voucher,null,0,1) > 0);

                                                                      Philip

-- 
/dev/brain:  character special (53/0)
-----------------------------------------------------------------------------
Philip Lijnzaad, lijnzaad_at_ebi.ac.uk \ European Bioinformatics Institute,rm A2-24
+44 (0)1223 49 4639                 / Wellcome Trust Genome Campus, Hinxton
+44 (0)1223 49 4468 (fax)           \ Cambridgeshire CB10 1SD,  GREAT BRITAIN
PGP fingerprint: E1 03 BF 80 94 61 B6 FC  50 3D 1F 64 40 75 FB 53
Received on Fri May 19 2000 - 00:00:00 CEST

Original text of this message