Re: replacing mysql enum - Mailing list pgsql-sql

From Ian Barwick
Subject Re: replacing mysql enum
Date
Msg-id 1d581afe041211090628da57e@mail.gmail.com
Whole thread Raw
In response to Re: replacing mysql enum  (Stephan Szabo <sszabo@megazone.bigpanda.com>)
Responses Re: replacing mysql enum
List pgsql-sql
On Sat, 11 Dec 2004 07:47:51 -0800 (PST), Stephan Szabo
<sszabo@megazone.bigpanda.com> wrote:
> On Sat, 11 Dec 2004, Ian Barwick wrote:
> 
> > (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
> > test=> SELECT * from consttest ;
> >  field
> > -------
> >  xx
> > (1 row)
> >
> > Not sure what logic is driving this).
> 
> The way NULL is handled in IN (because it's effectively an equality
> comparison). Unless I miss-remember the behavior, foo in (NULL, ...) can
> never return false and constraints are satisified unless the search
> condition returns false for some row.  I think this means you need the
> more verbose (field is null or field in ('a','b','c'))

This works as expected, although for constraints the nullness
of the column is better off handled by applying NOT NULL if
necessary.

What I still don't quite understand is why IN in a CHECK context is
handled differently to say: select 1 where 'x' in (null,'a','b','c') ?
This could be a bit of a gotcha for anyone constructing a constraint
similar to the original poster's and not realising it has no effect.

Ian Barwick


pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: replacing mysql enum
Next
From: Frank Bax
Date:
Subject: Re: Cast NULL into Timestamp?