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 D912B3A6394 for ; Sat, 11 Dec 2004 17:57:02 +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 95447-10 for ; Sat, 11 Dec 2004 17:56:53 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 6D5A23A6300 for ; Sat, 11 Dec 2004 17:56:52 +0000 (GMT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id iBBHuoik017434; Sat, 11 Dec 2004 12:56:50 -0500 (EST) To: Ian Barwick Cc: Kenneth Gonsalves , pgsql-sql@postgresql.org Subject: Re: replacing mysql enum In-reply-to: <1d581afe04121104255557ea@mail.gmail.com> References: <04121110305520.21547@thenilgiris.com> <1d581afe04121104255557ea@mail.gmail.com> Comments: In-reply-to Ian Barwick message dated "Sat, 11 Dec 2004 13:25:37 +0100" Date: Sat, 11 Dec 2004 12:56:50 -0500 Message-ID: <17433.1102787810@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200412/66 X-Sequence-Number: 19686 Ian Barwick writes: > (Oddly enough, putting the NULL in the CHECK constraint seems > to make the constraint worthless: > test=> create table consttest (field varchar(2) check (field in > (null, 'a','b','c'))); > CREATE TABLE > test=> insert into consttest values ('xx'); > INSERT 408080 1 For a non-null field value, that IN clause reduces to null OR false OR false OR false which reduces to null because of the way 3-state boolean logic is defined in SQL (which makes sense if you interpret null as "unknown"). And a null result from CHECK is defined not to be a failure case by the SQL standard. This is really the same logic that allows the explicit-null-free CHECK condition to accept NULLs: null IN ('a','b','c') becomes null OR null OR null becomes null which doesn't fail. I believe this was intentional on the part of the SQL committee. Their thought was that if you intend to disallow NULLs, you should write an explicit NOT NULL constraint, separately from any CHECK you might write. Therefore, defining CHECK such that it tend to fall through silently on NULL inputs is a good thing. regards, tom lane