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

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

Second:
id_key | car | city|

id_key without AI code consists of six digits.

It is necessary for me that when a user id_key code ( id_key ), that, another table would appear in his table as well. What is needed for this? Create a foreign key from id_key , how?

  • You want to ask how to add the same key to two tables? - alex_90
  • no) id_key is a primary key, in one table, how to make it another foreign key for another table, just write its name? - Zow
  • And what have the elves? - skegg
  • No offense, but you need to learn to express your thoughts more clearly. I do not understand anything. - Zibba
  • How to make a foreign key? - Zow

2 answers 2

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

So? This provided id AUTO_INCREMENT .

    If you create a foreign key, nothing will be entered by itself, just a link will appear. This can not be done. Just then, if you, for example, do admin panel to the site, you can output only the data from the database that can be entered there in <select></select> . In your case, all your id_key from the first table will be displayed there through the loop.