How to make a foreign key?

In general, there are two tables in the database. MySQL

First:
id | name | lastname | surname | id_key |

Second:
id_key | car | city|

id_keywithout A.I. the code consists of six digits.

I need that when the user enters the code (id_key), in his table, that other table would be entered as well.
What is needed for this? Create a foreign key from id_key, how?


Answer 1

INSERT INTO `table2` (`id_key`) SELECT `id_key` FROM `table1` ORDER BY `id` DESC LIMIT 0,1

So? This is subject to id AUTO_INCREMENT.


Answer 2

If you create a foreign key, nothing will be entered by itself, just a relationship will appear.
This may or may not be done.
Just then, if you, for example, make an admin panel for a site, you can display in <select></select>only those data from the database that can be entered there. In your case, all your id_keyfrom the first table will be displayed there through a loop.