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的基礎,通過上面的選項進行理解起來倒是不難。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章