Re: ENUM like data type

From: Mike Rylander <mrylander(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: ENUM like data type
Date: 2005-06-28 11:22:36
Message-ID: b918cf3d050628042253044ba@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql

On 6/21/05, MRB <nomail(at)example(dot)com> wrote:
> Hi All,
>
> I have something in mind I'm not certain is do-able.
>
> I'm working with a lot of data from MySQL where the MySQL ENUM type is used.
>

MySQL's ENUM is basically a wrapper for CHECK. You can use a CHECK
constraint like this:

CREATE TABLE test(
testfield TEXT CHECK (testfield IN( 'Bits', 'of', 'data'))
);

> This is not a big problem per se but creating the proper lookup tables
> is becoming a bit tedious so I was hoping to make something better of it.
>
> Here is where I get uncertain as to if this is possible. My idea is to
> create a pseudo type that triggers the creation of it's lookup tables
> the same way the SERIAL type triggers creation of a sequence and returns
> an int with the right default value.

Although you can't create a generic type to handle this, you can
create a DOMAIN to wrap up your constraint for each "enum" type field
that you want:

CREATE DOMAIN fruit AS TEXT CHECK (VALUE IN ('apple','orange','banana'));
CREATE TABLE eat (
food fruit
);

http://www.postgresql.org/docs/8.0/interactive/sql-createdomain.html

Hope that helps.

--
Mike Rylander
mrylander(at)gmail(dot)com
GPLS -- PINES Development
Database Developer
http://open-ils.org

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Dave Cramer 2005-06-28 12:03:12 Re: Implementing SQL/PSM for PG 8.2
Previous Message Jan Wieck 2005-06-28 11:18:07 Re: Implementing SQL/PSM for PG 8.2

Browse pgsql-sql by date

  From Date Subject
Next Message Sergey Levchenko 2005-06-28 11:39:39 ERROR: "TZ"/"tz" not supported
Previous Message Sergey Levchenko 2005-06-28 10:20:43 ERROR: "TZ"/"tz" not supported