Home > Net >  Does it matter whether you first write down the primary key or foreign key, when you use join?
Does it matter whether you first write down the primary key or foreign key, when you use join?

Time:12-26

When I use join, by the ON part I write down the connection, foreign key = primary key.

But when I reverse the fk and pk I still get the same result, does this mean that it doesn't matter which one goes first?

Example:

select movie.title,
director.firstname,
director.lastname
from film join director
on movie.director  = director.directorcode

(This is the one with the right order FK=PK)

outcome

select movie.title,
director.firstname,
director.lastname
from film join director 
on director.directorcode = movie.director 

outcome

CodePudding user response:

This is happening because in both cases the FK and PK columns refer to the same values.

In a SQL join operation, the result of the join will be the same regardless of the order of the FK and PK in the join condition, as long as the FK and PK columns refer to the same values.

The purpose of the join condition is to match up rows from the two tables based on the values in the FK and PK columns.

CodePudding user response:

It does not matters what you write first in equals operation.

  •  Tags:  
  • sql
  • Related