两张桌子。第一个表包含每个人,另一个表用于指定哪个人是家庭中的孩子:
create table persons(
personID bigint primary key auto_increment,
firstName varchar(30) not null,
lastName varchar(30) not null
);
create table kids(
personID bigint not null,
familyID bigint not null,
grade bigint,
constraint kidReference foreign key (personID) references persons (personID),
constraint familyReference foreign key (familyID) references families (familyID)
);
我想为每个孩子选择所有详细信息。使用以下查询,我得到相同的结果,相同的执行计划:
(1)逗号分隔连接:
select persons.personID, persons.firstName, persons.lastName, kids.familyID, kids.grade from persons, kids where persons.personID = kids.personID;
(2)自然连接:
select * from persons natural join kids;
(3)命名连接:
select * from persons join kids using (personID);
(4)条件加入:
select * from persons inner join kids on persons.personID = kids.personID;
仅仅是可读性的问题吗?在编写查询时,我应该只针对 -what-it-works-best- 吗?