By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

What is the good equivalent for ENUM ?

P: n/a
Hello,

I have to migrate a MySQL database to a PostgreSQL database without
procedures.

The problem is that this MySQL database uses ENUM, do you see what can I
do to migrate ENUM into PostgreSQL ?

Thanks in advance :-)

-------------------------------------
Bruno BAGUETTE - pg******@baguette.net
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 11 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 3 Sep 2003 at 14:30, Bruno BAGUETTE wrote:
Hello,

I have to migrate a MySQL database to a PostgreSQL database without
procedures.

The problem is that this MySQL database uses ENUM, do you see what can I
do to migrate ENUM into PostgreSQL ?


varchar with check constraints. Add constraits to allow only certain values of
varchar string.

Check http://archives.postgresql.org/pgsql...3/msg00273.php

Bye
Shridhar

--
All new: Parts not interchangeable with previous model.
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 11 '05 #2

P: n/a
>>>>> "SD" == Shridhar Daithankar <sh*****************@persistent.co.in> writes:

SD> On 3 Sep 2003 at 14:30, Bruno BAGUETTE wrote:
The problem is that this MySQL database uses ENUM, do you see what can I
do to migrate ENUM into PostgreSQL ?


SD> varchar with check constraints. Add constraits to allow only
SD> certain values of varchar string.

I used to do this. It turns out to be horribly inflexible when you
need to alter the enum values since the constraints cannot easily be
changed.

What I do is create a short table for the enum like this:

CREATE TABLE status_levels (
status varchar(10) PRIMARY KEY
) WITHOUT OIDS;
INSERT INTO status_levels (status) VALUES ('active');
INSERT INTO status_levels (status) VALUES ('overdue');
INSERT INTO status_levels (status) VALUES ('suspended');
INSERT INTO status_levels (status) VALUES ('terminated');

then reference it via foreign key from the "enum" field:

CREATE TABLE whatever (
...
*status varchar(10) NOT NULL DEFAULT 'active' REFERENCES status_levels(status),
...
);


--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Vivek Khera, Ph.D. Khera Communications, Inc.
Internet: kh***@kciLink.com Rockville, MD +1-240-453-8497
AIM: vivekkhera Y!: vivek_khera http://www.khera.org/~vivek/

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 11 '05 #3

P: n/a
that's how I'd do it, since in reality, that's all an ENUM is, is a mini
foreign table internalized onto a column in another table.

Vivek Khera wrote:
>>"SD" == Shridhar Daithankar <sh*****************@persistent.co.in> writes:
>>
>>
SD> On 3 Sep 2003 at 14:30, Bruno BAGUETTE wrote:

The problem is that this MySQL database uses ENUM, do you see what can I
do to migrate ENUM into PostgreSQL ?


SD> varchar with check constraints. Add constraits to allow only
SD> certain values of varchar string.

I used to do this. It turns out to be horribly inflexible when you
need to alter the enum values since the constraints cannot easily be
changed.

What I do is create a short table for the enum like this:

CREATE TABLE status_levels (
status varchar(10) PRIMARY KEY
) WITHOUT OIDS;
INSERT INTO status_levels (status) VALUES ('active');
INSERT INTO status_levels (status) VALUES ('overdue');
INSERT INTO status_levels (status) VALUES ('suspended');
INSERT INTO status_levels (status) VALUES ('terminated');

then reference it via foreign key from the "enum" field:

CREATE TABLE whatever (
...
?status varchar(10) NOT NULL DEFAULT 'active' REFERENCES status_levels(status),
...
);


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 11 '05 #4

P: n/a
On Wed, 2003-09-03 at 09:50, Vivek Khera wrote:
>> "SD" == Shridhar Daithankar <sh*****************@persistent.co.in> writes:
SD> On 3 Sep 2003 at 14:30, Bruno BAGUETTE wrote: The problem is that this MySQL database uses ENUM, do you see what can I
do to migrate ENUM into PostgreSQL ?

SD> varchar with check constraints. Add constraits to allow only
SD> certain values of varchar string.

I used to do this. It turns out to be horribly inflexible when you
need to alter the enum values since the constraints cannot easily be
changed.


It'll be better when domains have alterable constraints. Your
way is the traditional (and best, IMO) way, though.
What I do is create a short table for the enum like this:

CREATE TABLE status_levels (
status varchar(10) PRIMARY KEY
) WITHOUT OIDS;
INSERT INTO status_levels (status) VALUES ('active');
INSERT INTO status_levels (status) VALUES ('overdue');
INSERT INTO status_levels (status) VALUES ('suspended');
INSERT INTO status_levels (status) VALUES ('terminated');

then reference it via foreign key from the "enum" field:

CREATE TABLE whatever (
...
status varchar(10) NOT NULL DEFAULT 'active' REFERENCES status_levels(status),
...
);


--
-----------------------------------------------------------------
Ron Johnson, Jr. ro***********@cox.net
Jefferson, LA USA

"You ask us the same question every day, and we give you the
same answer every day. Someday, we hope that you will believe us..."
U.S. Secretary of Defense Donald Rumsfeld, to a reporter
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 11 '05 #5

P: n/a
Ron Johnson wrote:
On Wed, 2003-09-03 at 09:50, Vivek Khera wrote:
>>>> "SD" == Shridhar Daithankar <sh*****************@persistent.co.in> writes:


SD> On 3 Sep 2003 at 14:30, Bruno BAGUETTE wrote:
> The problem is that this MySQL database uses ENUM, do you see what can I
> do to migrate ENUM into PostgreSQL ?


SD> varchar with check constraints. Add constraits to allow only
SD> certain values of varchar string.

I used to do this. It turns out to be horribly inflexible when you
need to alter the enum values since the constraints cannot easily be
changed.


It'll be better when domains have alterable constraints. Your
way is the traditional (and best, IMO) way, though.


In 7.4 we have:

Add DOMAIN CHECK constraints (Rod)
Add ALTER DOMAIN .. SET / DROP NOT NULL, SET / DROP DEFAULT, ADD / DROP
CONSTRAINT (Rod)

--
Bruce Momjian | http://candle.pha.pa.us
pg***@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 11 '05 #6

P: n/a
On Wed, 2003-09-03 at 18:05, Bruce Momjian wrote:
Ron Johnson wrote:
On Wed, 2003-09-03 at 09:50, Vivek Khera wrote:
>>>>> "SD" == Shridhar Daithankar <sh*****************@persistent.co.in> writes:

SD> On 3 Sep 2003 at 14:30, Bruno BAGUETTE wrote:
>> The problem is that this MySQL database uses ENUM, do you see what can I
>> do to migrate ENUM into PostgreSQL ?

SD> varchar with check constraints. Add constraits to allow only
SD> certain values of varchar string.

I used to do this. It turns out to be horribly inflexible when you
need to alter the enum values since the constraints cannot easily be
changed.


It'll be better when domains have alterable constraints. Your
way is the traditional (and best, IMO) way, though.


In 7.4 we have:

Add DOMAIN CHECK constraints (Rod)
Add ALTER DOMAIN .. SET / DROP NOT NULL, SET / DROP DEFAULT, ADD / DROP
CONSTRAINT (Rod)


"Rod"??

--
-----------------------------------------------------------------
Ron Johnson, Jr. ro***********@cox.net
Jefferson, LA USA

Regarding war zones: "There's nothing sacrosanct about a hotel
with a bunch of journalists in it."
Marine Lt. Gen. Bernard E. Trainor (Retired)
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 11 '05 #7

P: n/a
On Wed, 3 Sep 2003, Ron Johnson wrote:
On Wed, 2003-09-03 at 09:50, Vivek Khera wrote:
>>>> "SD" == Shridhar Daithankar <sh*****************@persistent.co.in> writes:


SD> On 3 Sep 2003 at 14:30, Bruno BAGUETTE wrote:
> The problem is that this MySQL database uses ENUM, do you see what can I
> do to migrate ENUM into PostgreSQL ?


SD> varchar with check constraints. Add constraits to allow only
SD> certain values of varchar string.

I used to do this. It turns out to be horribly inflexible when you
need to alter the enum values since the constraints cannot easily be
changed.


It'll be better when domains have alterable constraints. Your
way is the traditional (and best, IMO) way, though.


Speaking of enumeration, I noticed that the old copy of the SQL3 spec I
have lists an enumerated type. Is that still in the SQL 3 spec, and if
so, are there any plans to implement it in pgsql?

Just wondering.
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 11 '05 #8

P: n/a


On Wed, 3 Sep 2003, Ron Johnson wrote:
It'll be better when domains have alterable constraints. Your
way is the traditional (and best, IMO) way, though.


This is similar to what I have tended to use, but I have always wondered
about the efficency, and have used an int4 serial column as the primary
key and used this to reference the status data.

This has made it a little frustrating sometimes to write queries though.
Is referencing the varchar column generally reasonable in postgresql, and
not likely to give big performance issues as the tables get larger?
(sorry, this is a little hand wavy and not very exact, but I am wondering
if i am getting carried away giving everything an id)

This is the type of thing I have used: (edited from a couple of posts ago)
CREATE TABLE status_levels (
status_levels_id serial primary key, status varchar(10) ) WITHOUT OIDS;
INSERT INTO status_levels (status) VALUES ('active');
INSERT INTO status_levels (status) VALUES ('overdue');
INSERT INTO status_levels (status) VALUES ('suspended');
INSERT INTO status_levels (status) VALUES ('terminated');

then reference it via foreign key from the "enum" field:

CREATE TABLE whatever (
...
status int4 NOT NULL DEFAULT 1 REFERENCES status_levels(status_levels_id), ...
);

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 11 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.