472,333 Members | 1,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,333 software developers and data experts.

What is the good equivalent for ENUM ?

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
8 8417
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
>>>>> "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
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
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
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
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: gonzalo briceno | last post by:
I have been using phplib for a while and I really like the framework except for form creation. Maybe it is me but I in my opinion there isn't a...
2
by: Job Lot | last post by:
I have an enumeration as follows Public Enum Delimiters Tab Semicolon Comma Space End Enum How can I return character equivalent of the...
5
by: Neil Zanella | last post by:
Hello, I have seen code that does the following: enum Letter { X, Y, Z, numLetters }; Is this considered good or bad code? On one hand, the...
7
by: Morgan Cheng | last post by:
Hi, In my program module, there are some Constants should be defined to be integer key value of std::map. In the module, methods of a few classes...
2
by: Mullin Yu | last post by:
At Vb6, we can do the following, and then call typing SendUsingMethod, it will pop up SendUsingPickup and SendUsingPort option Public Enum...
9
by: Nadav | last post by:
Hi, I am tring to pass messages between threads, using the good old C++ I would call the GetMessage/PostThreadMessage APIs, Now, I am using C# and...
3
by: Arronax | last post by:
Just a basic question after I didn't see any examples on the web other than the "None" enum. Is this just a way of sending "hints" to...
0
by: rchenna | last post by:
Hi All, We have some performance issues with 'CONNECT BY PRIOR' SQL statement and trying to find an alternate SQL which gives the same output. ...
14
by: Just_a_fan | last post by:
In VB6, I could easily take the value from a combo box and make a command with it, this: baudrate = cboBaud(listindex). With the new dotted stuff...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.