I've got a table with multiple duplicated entries in a column and I want to put these entries in a new table and connect these tables with a foreign key in the initial table.
Old Table:
table 1
| id | name | medium |
| 0 | xy | a |
| 1 | xz | b |
| 2 | yz | a |
new Table:
table 1 table2
| id | name | medium | | id | name |
| 0 | xy | 0 | | 0 | a |
| 1 | xz | 1 | | 1 | b |
| 2 | yz | 0 |
With CREATE ... SELECT I have a good tool to create a new table from the results of a query but I don't know how to change the entries from table1.medium to a foreign key based on the comparison to table2.medium. Is there any chance to do that?
If you are running mysql version 8.0 or above you can do below and use window function ROW_NUMBER.
CREATE TABLE TABLE2
AS
SELECT ID,NAME(
SELECT ID, MEDIUM AS NAME,ROW_NUMBER()OVER(PARTITION BY MEDIUM ORDER BY ID) AS ROWW FROM TABLE1)
WHERE ROWW=1;
-- Adds a new column into your TABLE1 for MEDIUM's int values
ALTER TABLE TABLE1 ADD COLUMN MEDIUMINT INT;
-- Update MEDIUMINT according to your TABLE2 values.
UPDATE TABLE1 S1
JOIN TABLE2 S2 ON TABLE1.MEDIUM=S2.NAME
SET S1.MEDIUMINT=S2.ID;
-- Drops MEDIUM column from TABLE1
ALTER TABLE TABLE1 DROP COLUMN MEDIUM;
-- Rename MEDIUMINT column to MEDIUM
ALTER TABLE TABLE1 RENAME COLUMN MEDIUMINT TO MEDIUM;
It sounds like you can create Table2 but are having trouble changing Table1, updating the column type from char to int and creating a foreign key.
Easiest would be to rename Table1 to Table3 then create Table1 as you want, insert the data, then drop Table3.
If you need to modify the existing table, it will be a multi-step process.
There are two ways of doing it:
medium column. Then you drop the old table1 and rename the new table. I would use this approach if medium column not only needs new values, but also needs to be converted to a new data type - this seems to be the case in the question. You already figured this approach out with create table ... as ...medium column. See this SO question on how to do this update.