Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在自然连接上使用WHERE子句_Mysql_Sql_Natural Join - Fatal编程技术网

Mysql 在自然连接上使用WHERE子句

Mysql 在自然连接上使用WHERE子句,mysql,sql,natural-join,Mysql,Sql,Natural Join,我正在学习《数据库系统概念》第六版,该书以一所大学的数据库为例。 该数据库的架构如下所示。粗体表示主键: department(dept_name, building, budget) course(course_id, title, dept_name, credits) instructor(ID, name, dept_name, salary) section(course_id, sec_id, semester, year, building, room number, time_s

我正在学习《数据库系统概念》第六版,该书以一所大学的数据库为例。 该数据库的架构如下所示。粗体表示主键:

department(dept_name, building, budget) course(course_id, title, dept_name, credits) instructor(ID, name, dept_name, salary) section(course_id, sec_id, semester, year, building, room number, time_slot_id) teaches(ID, course_id, sec_id, semester, year) prereq(course_id, prereq_id) 此外,作者还说:

“相比之下,以下SQL查询计算的结果不同:

选择名称、标题
从讲师自然连接到教授自然连接课程;
要了解原因,请注意讲师和讲师的自然连接包含属性 (ID、名称、部门名称、薪资、课程ID、sec ID),而课程关系包含 属性(课程id、标题、系名、学分)。因此,这些属性的自然连接 two将要求来自两个输入的dept name属性值为 相同,除了要求课程id值相同之外。此查询 然后将省略所有(讲师姓名、课程名称)对,其中讲师 在讲师所在部门以外的部门教授课程。 另一方面,前面的查询正确地输出了这些对。”

现在,我尝试了两种查询,以了解其中的差异

mysql> select * from instructor;
+-------+------------+------------+----------+
| ID    | name       | dept_name  | salary   |
+-------+------------+------------+----------+
| 10101 | Srinivasan | Comp. Sci. | 65000.00 |
| 12121 | Wu         | Finance    | 90000.00 |
| 15151 | Mozart     | Music      | 40000.00 |
| 22222 | Einstein   | Physics    | 95000.00 |
| 32343 | El Said    | History    | 60000.00 |
| 33456 | Gold       | Physics    | 87000.00 |
| 45565 | Katz       | Comp. Sci. | 75000.00 |
| 58583 | Califieri  | History    | 62000.00 |
| 76543 | Singh      | Finance    | 80000.00 |
| 76766 | Crick      | Biology    | 72000.00 |
| 83821 | Brandt     | Comp. Sci. | 92000.00 |
| 98345 | Kim        | Elec. Eng. | 80000.00 |
+-------+------------+------------+----------+
12 rows in set (0.00 sec)

mysql> select * from course;
+-----------+----------------------------+------------+---------+
| course_id | title                      | dept_name  | credits |
+-----------+----------------------------+------------+---------+
| BIO-101   | Intro. to Biology          | Biology    |       4 |
| BIO-301   | Genetics                   | Biology    |       4 |
| BIO-399   | Computational Biology      | Biology    |       3 |
| CS-101    | Intro. to Computer Science | Comp. Sci. |       4 |
| CS-190    | Game Design                | Comp. Sci. |       4 |
| CS-315    | Robotica                   | Comp. Sci. |       3 |
| CS-319    | Image Processing           | Comp. Sci. |       3 |
| CS-347    | Database System Concepts   | Comp. Sci. |       3 |
| EE-181    | Intro. to Digital Systems  | Elec. Eng. |       3 |
| FIN-201   | Investment Banking         | Finance    |       3 |
| HIS-351   | World History              | History    |       3 |
| MU-199    | Music Video Production     | Music      |       3 |
| PHY-101   | Physical Principles        | Physics    |       4 |
+-----------+----------------------------+------------+---------+

mysql> select * from teaches;
+-------+-----------+--------+----------+------+
| ID    | course_id | sec_id | semester | year |
+-------+-----------+--------+----------+------+
| 76766 | BIO-101   | 1      | Summer   | 2009 |
| 76766 | BIO-301   | 1      | Summer   | 2010 |
| 10101 | CS-101    | 1      | Fall     | 2009 |
| 45565 | CS-101    | 1      | Spring   | 2010 |
| 83821 | CS-190    | 1      | Spring   | 2009 |
| 83821 | CS-190    | 2      | Spring   | 2009 |
| 10101 | CS-315    | 1      | Spring   | 2010 |
| 45565 | CS-319    | 1      | Spring   | 2010 |
| 83821 | CS-319    | 2      | Spring   | 2010 |
| 10101 | CS-347    | 1      | Fall     | 2009 |
| 98345 | EE-181    | 1      | Spring   | 2009 |
| 12121 | FIN-201   | 1      | Spring   | 2010 |
| 32343 | HIS-351   | 1      | Spring   | 2010 |
| 15151 | MU-199    | 1      | Spring   | 2010 |
| 22222 | PHY-101   | 1      | Fall     | 2009 |
+-------+-----------+--------+----------+------+
15 rows in set (0.00 sec)

mysql> select name, title
-> from instructor natural join teaches, course
-> where teaches.course_id = course.course_id;
+------------+----------------------------+
| name       | title                      |
+------------+----------------------------+
| Crick      | Intro. to Biology          |
| Crick      | Genetics                   |
| Srinivasan | Intro. to Computer Science |
| Katz       | Intro. to Computer Science |
| Brandt     | Game Design                |
| Brandt     | Game Design                |
| Srinivasan | Robotica                   |
| Katz       | Image Processing           |
| Brandt     | Image Processing           |
| Srinivasan | Database System Concepts   |
| Kim        | Intro. to Digital Systems  |
| Wu         | Investment Banking         |
| El Said    | World History              |
| Mozart     | Music Video Production     |
| Einstein   | Physical Principles        |
+------------+----------------------------+
15 rows in set (0.00 sec)

mysql> select name, title
    -> from instructor natural join teaches natural join course;
+------------+----------------------------+
| name       | title                      |
+------------+----------------------------+
| Crick      | Intro. to Biology          |
| Crick      | Genetics                   |
| Srinivasan | Intro. to Computer Science |
| Katz       | Intro. to Computer Science |
| Brandt     | Game Design                |
| Brandt     | Game Design                |
| Srinivasan | Robotica                   |
| Katz       | Image Processing           |
| Brandt     | Image Processing           |
| Srinivasan | Database System Concepts   |
| Kim        | Intro. to Digital Systems  |
| Wu         | Investment Banking         |
| El Said    | World History              |
| Mozart     | Music Video Production     |
| Einstein   | Physical Principles        |
+------------+----------------------------+
15 rows in set (0.00 sec)
从结果来看,我看不出有什么不同,而且我不明白为什么会有不同。
与书中所述不同,讲师和教师的自然连接包含属性ID、名称、部门名称、薪水、课程ID、sec ID、学期、年份

这本书是正确的。这些查询不是等价的。样本数据(detp_名称值不同):

就个人而言,我不会使用任何建议的查询:

-- mixing natural join with old-comma syntax
select name, title
from instructor natural join teaches, course
where teaches.course_id = course.course_id;
--Output
--name      title
--Prof X    Course_1

-- natural join(potentially dangerous-depends on names of columns)
select name, title
from instructor natural join teaches natural join course;
-- 0 rows selected
并将其改写为:

SELECT i.name, c.title
FROM instructor i
JOIN teaches t
  ON t.ID = i.ID
JOIN course c
  ON t.course_id = c.course_id;

您从未显示过
课程
表格的样本数据,但书籍有时会出现打字错误。是的,您是对的。我刚刚编辑了它。@ginos书是对的。我不明白为什么自然连接与第一个显式连接有任何不同。从我所看到的,只有连接列具有相同的名称。不要使用
自然连接
!我认为这是可憎的,因为它甚至不使用正确的声明的键连接的外键关系。相反,使用适当、明确、标准的
join
语法,使用
on
使用
子句。谢谢@Lukasz Szozda
SELECT i.name, c.title
FROM instructor i
JOIN teaches t
  ON t.ID = i.ID
JOIN course c
  ON t.course_id = c.course_id;