SQL Select是否需要引用复合PK / FK表?

时间:2017-05-15 07:53:45

标签: sql select foreign-keys

我有3个表及其主键:

PLANT      DEPT      HOLDER
------     ------    ------
pId        dId       hId

以下是他们的关系:

1.) A department exist in many plants.

我为此设计了具有复合键PK / FK的表

DEPTinPLANT
-----------
{p_Id,d_Id}

和另一种关系,

2.) A holder can be assigned to many departments of a plant.

来自DEPTinPLANT和HOLDER表的复合PK / FK

HOLDERinDEPT
------------
{{p_Id,d_Id},h_ID}

现在用Select语句从HOLDERinDEPT中提取数据,我有

select *
from HOLDERinDEPT 
join PLANT  on p_Id = pid
join DEPT   on d_Id = dId
join HOLDER on h_id = hId

这是正确的还是需要首先引用DEPTinPLANT表然后再引用相应的基表?

1 个答案:

答案 0 :(得分:1)

select *
from HOLDERinDEPT hd
join PLANT p on p_Id = pid
join DEPT d  on d_Id = dId
join HOLDER h on h_id = hId

这是对的。 无需参考DEPTinPLANT