How to fill a foreign key column with id values ​​from another table? The first thing I did was create a table

 CREATE TABLE my_list (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(20), last_name VARCHAR(20)); 

Then it was necessary to add the int_id foreign key of the interests

 ALTER TABLE my_list ADD int_id INT, ADD CONSTRAINT FOREIGN KEY(int_id) REFERENCES interests(int_id); 

An int_id column is int_id with NULL values.

Question - Is it possible to fill the int_id column in the my_list table my_list all the values ​​from the int_id column of the interests table?

enter image description here

I tried to do something like this:

 UPDATE my_list SET int_id = (SELECT int_id FROM interests) 

But it does not work.

For example, I filled out the prof_id foreign key as follows:

 UPDATE my_list SET prof_id = CASE WHEN id = 1 THEN 1 WHEN id = 2 THEN 2 // и т.д END; 

But if in my table there will be 1000 records. It will be very tedious to fill out. I am new to SQL , so I still don’t know much. Thanks in advance for the tips.

  • 2
    You did not specify which database you have. And in different databases, the syntax may be different. And secondly, it is necessary to determine exactly in which record which id should be written. This you have indicated on the same. Update can most often be used with join ru.stackoverflow.com/q/597149/194569 - Mike
  • one
    Based on what condition do you need to update the int_id in the my_list table? For example, an entry with id=7 (Carla) which int_id should it be? - Novitskiy Denis

0