views:

404

answers:

2

I'm sure this is either totally impossible or really easy:

If I'm creating a table and I want one of the columns to have limited options, it seems that I use either the ENUM or SET value type. But I have to define the possible values at that moment. What if I have another table which has two columns, a primary key column and a data column, and I want the ENUM for my new table to be set to the primary key of the already existing column?

I'm sure I can just write in the values long-hand, but ideally what I need is for new values to be entered into the list table and for the table with the enum column to just accept that the value choices will include anything new added to that list table.

Is this possible without needing to manipulate the structure of the new table each time something is added to the list?

A: 

i think this link help : http://dev.mysql.com/doc/refman/5.0/en/enum.html

have a discussion of it in the user comments start :

"In MySQL 5.0, you can convert an enum's values into a dynamically-defined table of values, which then provides effectively a language-neutral method to handle this kind of conversion (rather than relying on PHP, Tcl, C, C++, Java, etc. specific code). "

he do it with stored PROCEDURE

Haim Evgi
A: 

The easiest way is to use a regular column without contraints. If you're interested in all the current values, use DISTINCT to query them:

select distinct YourColumn from YourTable

That way, you don't have any maintenance and can store whatever you like in the table.

The foreign key table you mention is also a good option. The foreign key will limit the original column. Before you do the actual insert, you run a query to expand the "enum" table:

insert into EnumTable (name)
select 'NewEnumValue'
where not exists (select * from EnumTable where name = 'NewEnumValue')

Not sure what exactly you're trying to achieve btw; limit the column, but automatically expand the choices when someone breaks the limit?

Andomar
Well, I can think of a lot of reasons, so maybe I'm not asking the right question. Here's an example:User uploads some data, in this case it would be stock market quotes. In order to keep things tidy, we have the quotes organized by Market (NYSE, NASDAQ). And the uploaded quotes get pushed to a table with one of the columns as "Market", which, for the time being, is set as: enum("NYSE","NASDAQ"). Plus we have a table called "Markets" which have two values, same as the enum column. I want to be able to add "LSE" to the Market table and have it automatically reflect on the enum column.
Anthony
I'd see the Market table as a view then? "select distinct market from quotes" If you'd like to keep the separate table, the "insert itno ... where not" approach should work.
Andomar