Loading values of ENUM out another tabel
Loading values of ENUM out another tabel
am 23.12.2006 14:20:14 von stef
I was wondering if it is possible to load the values of a ENUM out of
antoher table.
Example:
Table 1
id Colors
1 A
2 B
3 C
Table 2
id car color
Would it be possible to set 'color' to an ENUM type and load the different
values of color out of table 1?
Thanks for any ideas.
Re: Loading values of ENUM out another tabel
am 23.12.2006 14:32:01 von Shion
Stef wrote:
> I was wondering if it is possible to load the values of a ENUM out of
> antoher table.
>
> Example:
>
> Table 1
> id Colors
> 1 A
> 2 B
> 3 C
>
> Table 2
> id car color
>
> Would it be possible to set 'color' to an ENUM type and load the different
> values of color out of table 1?
Was it this you had in mind?
http://dev.mysql.com/doc/refman/5.0/en/join.html
Works pretty in the same manner/syntax on most real SQL databases.
--
//Aho
Re: Loading values of ENUM out another tabel
am 23.12.2006 14:57:33 von stef
"J.O. Aho" schreef in bericht
news:4v4pehF19fs26U1@mid.individual.net...
> Stef wrote:
>> I was wondering if it is possible to load the values of a ENUM out of
>> antoher table.
>>
>> Example:
>>
>> Table 1
>> id Colors
>> 1 A
>> 2 B
>> 3 C
>>
>> Table 2
>> id car color
>>
>> Would it be possible to set 'color' to an ENUM type and load the
>> different values of color out of table 1?
>
> Was it this you had in mind?
> http://dev.mysql.com/doc/refman/5.0/en/join.html
>
> Works pretty in the same manner/syntax on most real SQL databases.
>
>
> --
>
> //Aho
Well ... I'm not really sure. I've written a CMS where ENUMs are shown as a
pull down list. For the moment these lists have a fixed number of valies
defined in the type field.
Sticking to the example the color column of table 2 would be defined as
enum('A','B','C'). What want to do is making the values inside the enum
depende of the values of the colors column in table 1. In other words ... I
do not know how many values there will be in advance.
Re: Loading values of ENUM out another tabel
am 25.12.2006 17:56:15 von stef
Does anyone have more ideas on this?
"Stef" schreef in bericht
news:458d2d87$0$21489$ba620e4c@news.skynet.be...
>I was wondering if it is possible to load the values of a ENUM out of
>antoher table.
>
> Example:
>
> Table 1
> id Colors
> 1 A
> 2 B
> 3 C
>
> Table 2
> id car color
>
> Would it be possible to set 'color' to an ENUM type and load the different
> values of color out of table 1?
>
> Thanks for any ideas.
>
Re: Loading values of ENUM out another tabel
am 25.12.2006 23:07:27 von Norman Peelman
"Stef" wrote in message
news:45900325$0$21505$ba620e4c@news.skynet.be...
> Does anyone have more ideas on this?
>
> "Stef" schreef in bericht
> news:458d2d87$0$21489$ba620e4c@news.skynet.be...
> >I was wondering if it is possible to load the values of a ENUM out of
> >antoher table.
> >
> > Example:
> >
> > Table 1
> > id Colors
> > 1 A
> > 2 B
> > 3 C
> >
> > Table 2
> > id car color
> >
> > Would it be possible to set 'color' to an ENUM type and load the
different
> > values of color out of table 1?
> >
> > Thanks for any ideas.
> >
>
>
I think what you need to do is 'read' in the ENUM definition for the
current 'color' and the ALTER it with the new info, something like:
1) read the enum column definition into an array using "SHOW COLUMNS FROM
LIKE ''"
2) add the new values to the array, creating your query, you need to
recreate the entire ENUM column
3) "ALTER TABLE MODIFY enum_column ENUM('?','?'.etc)" where the '?'
are your old and new enum values.
Definately make a copy of your table to test on... or just create a simple
test table to play with until you get it to work.
Link: http://dev.mysql.com/doc/refman/4.1/en/enum.html
....read all the user comments and you'll see what you need to do.
Especially the post by Willem-Jan van Dinter which I tried out while writing
this responce.
Once you have the current ENUM values in an array ($options in this case)
you can create a new query as such:
$options[] = 'newvalue1'; // tack new values onto end of array to preserve
current table data
$options[] = 'newvalue2';
//etc.
$query = "ALTER TABLE MODIFY color ENUM(";
$count = 0;
foreach($options AS $val)
{
if ($count == 0)
{
$query .= "'$val'"; // take notice of quotes - must add single quotes back
in
}
else
{
$query .= ",'$val'"; // precede 2nd through last entries with a comma
}
$count++;
}
$query .= ')';
----
Norm
--
FREE Avatar hosting at www.easyavatar.com
Re: Loading values of ENUM out another tabel
am 26.12.2006 01:14:23 von Norman Peelman
Nevermind... not using brain.
Norm
--
FREE Avatar hosting at www.easyavatar.com
"Stef" wrote in message
news:45900325$0$21505$ba620e4c@news.skynet.be...
> Does anyone have more ideas on this?
>
> "Stef" schreef in bericht
> news:458d2d87$0$21489$ba620e4c@news.skynet.be...
> >I was wondering if it is possible to load the values of a ENUM out of
> >antoher table.
> >
> > Example:
> >
> > Table 1
> > id Colors
> > 1 A
> > 2 B
> > 3 C
> >
> > Table 2
> > id car color
> >
> > Would it be possible to set 'color' to an ENUM type and load the
different
> > values of color out of table 1?
> >
> > Thanks for any ideas.
> >
>
>