The following tables are available:

  1. TEST1

    • id PK
    • name
  2. TEST2

    • id_TEST1 FK REFERENCES TEST1 (id)

How to get data from another table using a foreign key? Below?

SELECT * FROM `ТЕСТ1` where `id` = `id_ТЕСТ1` 

Or for keys there is another way to get data from the tables?

  • And why do you need a "different way" than this one is bad for? - D-side
  • one
    @ user200192 is not the only way, so I ask what exactly interests you. - D-side
  • @ Buttons can, please, show how data is extracted in a different way by a key or at least a reference for review - user200192
  • @ user200192 You will answer first, what this does not suit you? - D-side
  • one
    @ user200192 well, why, I understand your condition and I know that it is important to ask yourself the question "why" :) The above query is incorrect and will not be executed. Hence the question of what you want to do. - D-side

1 answer 1

The sample can go on any columns, including the key. The relationship of the fields in the query, we write based on the logic of relationships that we know. Yes, in most cases, we form our knowledge about communication in the form of foreign keys, so that the database understands this and can check if they are trying to write contradictory data in the tables. Those. for example, to delete a record from the parent table for which there are records in the child table or that in the child table do not try to insert records with non-existing key values ​​in the parent.

The request can be written and executed regardless of the existence of a foreign key. You think meant such request:

 select * from ТЕСТ1 join ТЕСТ2 on id = id_ТЕСТ1