Question

We are going to change something that we have an enum to an id of a linked table instead.

How would we go about that?

Our current enum name: strat
Our new linked name: stratid

What I was thinking was something along the lines of:

UPDATE table_name  
SET stratid = (SELECT id FROM link_table WHERE stratname = table_name.strat);

I have not created the link table yet, right now it is all theory.

Will the above work?
Is there anything I should change in order to transfer from an enum to the linked table?

Was it helpful?

Solution

No gotchas for making the update other than that you need want to make triple sure that your link_table.ids are populated strictly in the order that table_name.strat options are defined.

For example, if strat is enum('FOO', 'BAR') then in linked_table the record with id == 1 should be the "FOO" record.

After that, you 'd perhaps want to make the stratid column NON NULL; this is not strictly equivalent to your previous arrangement, but it would probably be closer to what you want.

OTHER TIPS

Yes, create the link table first,
set the stratname as unique,
use an auto increment ID

A lazy solution for insert link_table:

insert into link_table
select distinct strat from table_name order by strat;

However, I not sure is all the predefined enum is being used.

Also, without knowing size of the enum,
I can't suggest you do a manual insert.

If you look at enum ...

enum('...', '...', ...)  <-- is just a comma separated value

So, here is the query to get the CSV :-

select column_type from information_schema.columns 
where schema_name="table_name" and column_name = "strat";

You can combine with an programming language to do the link_table insertion.

Lastly, you UPDATE query is not very optimize, you can switch to use INNER JOIN.
But I assume is one-time job, so be it!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top