Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

419
Vistas
SQL: Create foreign table and key from a single column

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?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

If you are running mysql version 8.0 or above you can do below and use window function ROW_NUMBER.

Source

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;
over 4 years ago · Santiago Trujillo Denunciar

0

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.

  1. Add new column 'medium_old' and copy existing values from medium
  2. Drop the column 'medium' with the char type
  3. Add the column 'medium' with the new int type
  4. Update the new 'medium' values to the key from Table2 based on values in 'medium_old'
  5. Add the foreign key constraint to 'medium'
over 4 years ago · Santiago Trujillo Denunciar

0

There are two ways of doing it:

  1. You recreate table1 with the new values in the 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 ...
  2. Using multi-table update syntax, you can easily update a field in a table based on a field in another table. I would use this approach if you do not need to change the data type of the medium column. See this SO question on how to do this update.
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda