Why is a primary-foreign key relation required when we can join without it?

前端 未结 5 854
攒了一身酷
攒了一身酷 2020-12-02 08:31

If we can get data from two tables without having primary and foreign key relation, then why we need this rule? Can you please explain me clearly, with suitable example? It\

5条回答
  •  长情又很酷
    2020-12-02 08:58

    A primary key is not required. A foreign key is not required either. You can construct a query joining two tables on any column you wish as long as the datatypes either match or are converted to match. No relationship needs to explicitly exist.

    To do this you use an outer join:

    select tablea.code, tablea.name, tableb.location from tablea left outer join 
    tableb on tablea.code = tableb.code
    

    join with out relation

    SQL join

提交回复
热议问题