X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 505AC3A1D71 for ; Sun, 16 Jan 2005 18:46:28 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 60981-02 for ; Sun, 16 Jan 2005 18:46:25 +0000 (GMT) Received: from boutiquenumerique.com (boutiquenumerique.com [82.67.9.10]) by svr1.postgresql.org (Postfix) with ESMTP id 0B9AA3A1D66 for ; Sun, 16 Jan 2005 18:46:25 +0000 (GMT) Received: (qmail 29394 invoked from network); 16 Jan 2005 19:46:33 +0100 Received: from unknown (HELO musicbox) (boutiquenumerique-lists@192.168.0.2) by boutiquenumerique.com with SMTP; 16 Jan 2005 19:46:33 +0100 To: "j.random.programmer" , "Richard Huxton" Cc: pgsql-sql@postgresql.org Subject: Re: SQL design question: null vs. boolean values References: <20050116135936.28081.qmail@web14206.mail.yahoo.com> Message-ID: Date: Sun, 16 Jan 2005 19:47:33 +0100 From: PFC Organization: =?iso-8859-15?Q?La_Boutique_Num=E9rique?= Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In-Reply-To: <20050116135936.28081.qmail@web14206.mail.yahoo.com> User-Agent: Opera M2/7.54 (Linux, build 751) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.002 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200501/116 X-Sequence-Number: 19904 > create table xyz > ( > field_foo char(1) check (field_foo in 'y', 'n'), > foo_detail varchar(255), > check ( > case > when field_foo='y' and foo_detail is null > then false > else true > end > ) > ); A simpler check would be : CHECK( (field_foo = 'y' AND foo_detail IS NOT NULL) OR ( (field_foo = 'n' OR field_foo IS NULL) AND foo_detail IS NULL) ) Which means " field_foo can be y, n, or NULL, and foo_detail should be null except if field_foo is 'y' " Also, IMHO, the Y/N/unknown case should have three values, NULL meaning 'the user has not answered this question'. Because if you insert a blank row in the table and fill it afterwards, you'll know if it was 'answered unknown' or 'unanswered'.