Skip site navigation (1) Skip section navigation (2)

Re: enum problem

From: Jason Earl <jdearl(at)yahoo(dot)com>
To: victor(at)work(dot)ro, pgsql-novice(at)postgresql(dot)org
Subject: Re: enum problem
Date: 2001-10-05 18:08:12
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
I took a look at the MySQL documentation and it would
appear that there isn't a datatype with quite the
exact properties of enum.

However, depending on what you need there are several
ways to create something very enum like.

    bar text 
        check (bar IN ('baz', 'qux', 'quz'))

The check construct will allow you to limit entries to
one of these values plus NULL (so the above example
would allow you to insert 'baz', 'qux', 'quz' or
NULL).  In fact, the major difference between this an
an ENUM type in MySQL is the fact that PostgreSQL
fails to insert records if they don't match the check
constraint.  The query raises an error and the
transaction is aborted.  In MySQL the insert will
happen, but it will insert an empty text string.

Depending on what you want, this may or may not be a
good thing.

Another handy trick is to use PostgreSQL's referential
integrity and foreign key constraints.  For example,
you could create too tables:

CREATE TABLE foo_lookup (
    value    text PRIMARY KEY

And fill it with your lookup values:

INSERT INTO foo_lookup (value) VALUES ('baz');
INSERT INTO foo_lookup (value) VALUES ('quz');
INSERT INTO foo_lookup (value) VALUES ('qux');

And then create your main table:

    bar REFERENCES foo_lookup

This would guarantee that was either 'baz',
'quz', 'qux' or NULL and it would also make it
possible to later add new values to your table (by
inserting them in foo_lookup).  You could even add an
empty string to foo_lookup and create a simple trigger
that would change to an empty string if a
value is inserted that isn't in foo_lookup.  That
would give you a field that worked just like MySQL's

I hope this is helpful,

--- victor <victor(at)work(dot)ro> wrote:
> Hi,
> is there any data type that suply enum type from
> mysql?
> ---------------------------(end of
> broadcast)---------------------------
> TIP 6: Have you searched our list archives?

Do You Yahoo!?
NEW from Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.

In response to

pgsql-novice by date

Next:From: P.V. SubramanianDate: 2001-10-05 18:52:25
Subject: How to find out that a record has been updated
Previous:From: Duncan Adams (DNS)Date: 2001-10-05 15:23:20
Subject: sequence last_value

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group