I have to import data from an old schema to a new one, where a column ‘career_name’ (in table ‘users’) that used to be a VARCHAR now should be an INTEGER which is a foreign key to another table careers. This way I intend to tipify the data that was stored in the VARCHAR column in order to keep integrity in my database.
Actually, I do the following:
- Create both new tables in the new schema
- Use
SELECT DISTINCT 'career_name' FROM old_tablein order to obtain all possible values INSERTinto my new tablecareersthe rows obtained aboveINSERTdata into my new tableusersusing a CASE clause in order to obtain the ID from tablecareersof the corresponding formercareer_nameso the relation is created.
Now the problem is the following, the table careers is big, so writing one CASE clause for each row in the table in order to import users is nearly impossible.
Is there a way to avoid this? I wish I could use my table careers as an associative array, where the key was the former career_name and the value the row’s ID… Thanks!
Because the schema isnt known to me, I am assuming that new_users table has 2 columns which is name and career_id.