问题描述
我正在为考试而学习,但是没有找到一个可靠的标准来确定是否要使用笛卡尔乘积 x
或自然加入 | X |
。
I am studying for exams and am failing to find a solid criteria by which I can determine if the Cartesian Product x
is to be used or if Natural Join |X|
is to be used.
我想出了一个粗略的指导:
I had come up with a rough guide that:
如果需要投影与要连接的表中的属性名称相同的属性,则必须使用 x
并说明要投影的表名: tableA.colname1 = tableB.colname1
"If you need to project an attribute that has the same name as an attribute in the table to be joined you must use x
and state the table names to be projected: tableA.colname1 = tableB.colname1
"
但是,这并没有遵循我的笔记中的某些解决方案,我的讲师似乎将 x
与上述约定或一起使用| x |
可以互换。
This however doesn't follow some of the solutions in my notes and my lecturer seems to use x
with the above convention or |x|
interchangeably.
有人能遵循一个规则来定义一个在另一个之上的使用吗?
Does anyone have a rule that can be followed to define use of one over the other?
例如,以下模式(仅与为简短起见所引用的问题相关的模式):
Take for example this schema (only schema related to the question quoted for brevity):
takes(ID, course_id, sec_id, semester, year, grade)
student(ID, name, dept_name, tot_cred)
Q)查找所有符合以下条件的学生的姓名选修了在2011年春季或2011年秋季开设的课程。
Q) Find the name of all students who took courses that were taught in either Spring 2011 or Autumn 2011.
我的回答尝试:
π name(σ semester="Spring" ^ year=2011(takes ⋈ student)) ∪ π name(σ semester="Autumn" ^ year=2011(takes ⋈ student))
实际答案:
π name(σ semester="Spring" ^ year=2011 ^ takes.ID=student.ID(takes x student)) ∪ π name(σ semester="Autumn" ^ year=2011 ^ takes.ID=student.ID(takes x student))
有人可以提供原因吗?
在我看来,自然加入将处理 takes.ID = student.ID
?
In my mind the Natural Join would take care of the takes.ID=student.ID
?
推荐答案
按照我的理解,自然连接是经过预测的,经过过滤的笛卡尔积:
A natural join, as I understand it, is a projected, filtered Cartesian product:
- 笛卡尔积,然后
- 选择它,以便相同名称的列具有相同的值,并且
- 对其进行投影,以便所有列都具有不同的名称。
在此假设下,您的答案与实际答案同构。
Under this assumption, your answer is isomorphic to the actual answer.
要查看此信息,您可能需要将自然联接扩展为上述序列运算符,然后使用关系代数定律使它们浮动。您会看到由于名称
的投影,投影消失了,选择标准与上面的选择融合在一起。即使您从未改变自己答案的含义,您最终还是会得到与实际答案完全相同的树!
To see this, you might want to expand the natural join to the above sequence of operators, and float them around using the laws of relational algebra. You'll see that the projection disappears due to the projection to name
, and the selection criterion is fused with the selection above. You'll end up with exactly the same tree as the actual answer, even though you never changed the meaning of your own answer!
我能想到您的讲师可以交替使用这些概念的一个原因:您的讲师希望您理解这些概念 可以互换使用,因为自然连接只是捷径(尽管这值得商)。)
I can think of one reason why your lecturer uses these concepts interchangeably: your lecturer wants you to understand that these concepts can be used interchangeably, because "the natural join is just a shortcut" (though that's debatable).
这篇关于关系代数-笛卡尔积与自然连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!