Re: replacing mysql enum - Mailing list pgsql-sql

From Ian Barwick
Subject Re: replacing mysql enum
Date
Msg-id 1d581afe04121104255557ea@mail.gmail.com
Whole thread Raw
In response to replacing mysql enum  (Kenneth Gonsalves <lawgon@thenilgiris.com>)
Responses Re: replacing mysql enum
Re: replacing mysql enum
List pgsql-sql
On Sat, 11 Dec 2004 10:30:55 +0530, Kenneth Gonsalves
<lawgon@thenilgiris.com> wrote:
> hi,
> 
> from mysql:
> 
> field enum('a','b','c') default null,
> 
> i translated this as:
> 
> field varchar(2) check (field in (null,'a','b','c')),
> 
> is it necessary to put the 'null' in the check condition? if not will pg
> permit you to enter a null value in the field?

No, and yes:
create table consttest (field varchar(2) check (field in ('a','b','c')));
insert into consttest values (null);

Note this does not emulate the MySQL ENUM datatype precisely,
because it's possible to reference the ENUM fields by index value
too.

(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).

Ian Barwick


pgsql-sql by date:

Previous
From: Joe Conway
Date:
Subject: Re: Create Calendar
Next
From: tomas@nocrew.org (Tomas Skäre)
Date:
Subject: Re: [GENERAL] Query is not using index when it should