Oracle官网的关于join如何使用的小测试(Quiz on Joins)

Oracle数据库的关于join如何使用的小测试

贴下我的测试结果
在这里插入图片描述
原题链接:点击访问🔗

题目:

有两个表,plch_departments 和 plch_employees ,共有一个相同字段 department_id ;
现在要将两个表格中三个字段一起查询输出。

题目中提供的方法是 :
通过 WHERE 条件语句进行比较两表中的department_id,相同时则输出对应的字段。

CREATE TABLE plch_departments (
  department_id INTEGER PRIMARY KEY
 ,department_name VARCHAR2 (100))
/

BEGIN
   INSERT INTO plch_departments
        VALUES (100, 'Marketing');

   INSERT INTO plch_departments
        VALUES (200, 'Catering');

   COMMIT;
END;
/

CREATE TABLE plch_employees
(
   employee_id     INTEGER
 , last_name       VARCHAR2 (100)
 , salary          NUMBER
 , department_id   INTEGER    REFERENCES plch_departments (department_id)
)

/

BEGIN
   INSERT INTO plch_employees
        VALUES (100
              , 'Jobs'
              , 1000000
              , 100);

   INSERT INTO plch_employees
        VALUES (200
              , 'Ellison'
              , 1000000
              , 200);

   INSERT INTO plch_employees
        VALUES (300
              , 'Gates'
              , 1000000
              , 200);

   COMMIT;
END;
/
Which of the choices contain a query that returns the same dataset as the following query:

SELECT e.last_name, e.salary, d.department_name
  FROM plch_employees e, plch_departments d
 WHERE e.department_id = d.department_id
以下是选项:

Choice 1 √

SELECT e.last_name, e.salary, d.department_name
  FROM plch_employees e NATURAL JOIN plch_departments d
  • Use the Natural Join syntax to join two tables based on column names they have in common, in this case department_id.

  • NATURAL JOIN 时两表必须有一样的列(这里是 department_id )


Choice 2 √

SELECT e.last_name, e.salary, d.department_name
  FROM plch_employees e JOIN plch_departments d USING (department_id)
  • JOIN…USING is much like the Natural Join, but in this case you specify the columns to use in the join.

  • USING 后的字段即公共字段


Choice 3 ×

选错了,虽然和选项2很类似,但 USING 后的字段必须用括号括起来,我以为有不有都可以。
在这里插入图片描述


Choice 4 √

SELECT e.last_name, e.salary, d.department_name
  FROM    plch_employees e
       JOIN
          plch_departments d
       ON (e.department_id = d.department_id)
  • The JOIN…ON syntax is used to join table on the fields you specify. They need not be of the same name and as long as there can be an implicit typecast, they don’t even have to be of the same type.
  • JOIN....ON字段名和字段类型不必相同

Choice 5 ×

SELECT e.last_name, e.salary, d.department_name
  FROM plch_employees e CROSS JOIN plch_departments d
  • A Cross Join joins every record in the first table with every record in the second table therefore resulting in a Cartesian product, which does not give us the result we wanted for this quiz.

  • Of course, there may come a time when you actually do need a Cartesian product. In this case, the ANSI syntax is more explicit and manageable than the ‘old’ Oracle syntax - with which Cartesian products are the result of leaving off predicates in the WHERE clause.

  • CROSS JOIN 将第一个表中的每个记录与第二个表中的每个记录联接在一起,产生笛卡尔积。

这里不知道笛卡尔积,可以看下图:
在这里插入图片描述


Choice 6 √

这里也选错了,看到cross join 就没选了。
其实这里的字段department_id值对应是一对一的,一个 100 200,一个 100 200 300,where条件语句,确保了一对一,cross join没有啥影响。

SELECT e.last_name, e.salary, d.department_name
  FROM plch_employees e CROSS JOIN plch_departments d
 WHERE e.department_id = d.department_id
  • A Cross Join joins every record in the first table with every record in the second table. The where clause then limits the result of the query. This choice looks an awful lot like the ‘old’ Oracle syntax, just a bit more verbose.

  • De advised though that creating a cartesian product and then filtering it may not be very performant and may put a big stress on the available memory.

  • 这里也说了下,比较冗余的查询语句,造成内存负担。


扩展阅读

参考 《Oracle9i表连接》
在这里插入图片描述
感觉和MySQL的差别就在于 NATURAL JOIN、 USING、CROSS JOIN这几个,还好有MySQL的基础,通过上面的选项进行理解起来倒是不难。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值