-1

I have been using JOINS for a long period of time and don't know how foreign keys affect JOINs. I want to know how they are different? If one is absent, does it affect the other?

  • Does this answer your question? [Why are foreign keys more used in theory than in practice?](https://stackoverflow.com/questions/1876013/why-are-foreign-keys-more-used-in-theory-than-in-practice) – philipxy Apr 11 '22 at 11:05
  • You ask re multiple things. If you don't know what they are, why are you asking for "the difference"?--Find out what each is. If you think you know what they are, how is it that you are stuck giving "the difference"?--and what does that even mean? Either way give definitions, show & apply research & ask 1 clear specific non-duplicate question about how you are stuck on a specific point. (Rarely, we can give 2 definitions having a precise overlap of structure with reasonable "difference".) PS [Foreign keys are not needed to join tables!](https://stackoverflow.com/a/23842061/3404097) – philipxy Apr 11 '22 at 11:09

2 Answers2

1

A join calculates a table that consists of a rows combined from the rows of the joined tables. A foreign key enforces consistency between two tables. Both are entirely different concepts and have nothing to do with each other, even though you will often end up joining on columns that are related by a foreign key constraint.

That said, the existence of a foreign key may influence how the optimizer estimates the results of certain joins, since it provides additional information. But that will depend on the database software you are using.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • Hmm, "*Nothing to do with each other*" is a bit strong in my opinion. Typically FK columns are used in JOIN condition. So on a logical level, I think they do have something "in common" –  Apr 11 '22 at 07:13
  • @a_horse_with_no_name I wanted to emphasize that they are independent. It can make sense to join on a condition that does not correspond to a foreign key, and it can make sense to have a foreign key where you would never join. – Laurenz Albe Apr 11 '22 at 07:42
0

JOIN is a condition which determines does some row from one table matches some row in another table and does these rows pair must be processed as one combined row.

FOREIGN KEY is a rule for data consistency checking subsystem which checks the data state after any data change for final data state validity.

There is nothing in common between JOIN and FK.

Akina
  • 39,301
  • 5
  • 14
  • 25