在ORACLE9i之前,oracle语法基础是SQL/86标准,9i及之后的版本中支持SQL/92标准。
基表信息:products、purchases和product_types
SQL> select * from products; PRODUCT_ID PRODUCT_TYPE_ID NAME DESCRIPTION PRICE ---------- --------------- ------------------------------ -------------------------------------------------- ---------- 1 1 Modern Science A description of modern science 19.95 2 1 Chemistry Introduction to Chemistry 30 3 2 Supernova A star explodes 25.99 4 2 Tank War Action movie about a future war 13.95 5 2 Z Files Series on mysterious activities 49.99 6 2 2412: The Return Aliens return 14.95 7 3 Space Force 9 Adventures of heroes 13.49 8 3 From Another Planet Alien from another planet lands on Earth 12.99 9 4 Classical Music The best classical music 10.99 10 4 Pop 3 The best popular music 15.99 11 4 Creative Yell Debut album 14.99 12 My Front Line Their greatest hits 13.49 已选择12行。 SQL> select * from product_types; PRODUCT_TYPE_ID NAME --------------- ---------- 1 Book 5 Magazine 4 CD 3 DVD 2 Video 100 test 已选择6行。 SQL> SELECT * FROM PURCHASES; PRODUCT_ID CUSTOMER_ID QUANTITY ---------- ----------- ---------- 1 2 2 3 3 1 2 3 1 1 3 1 2 2 1 SQL>
1、使用Oracle连接语法(SQL/86版)
SQL> select p.name,pt.name type from products p,product_types pt where p.product_type_id=pt.product_type_id order by p.name; NAME TYPE ------------------------------ ---------- 2412: The Return Video Chemistry Book Classical Music CD Creative Yell CD From Another Planet DVD Modern Science Book Pop 3 CD Space Force 9 DVD Supernova Video Tank War Video Z Files Video 已选择11行。
2、使用SQL/92引入INNER JOIN和ON重写上面的查询
SQL> select p.name,pt.name type from products p inner join product_types pt on p.product_type_id=pt.product_type_id order by p.name; NAME TYPE ------------------------------ ---------- 2412: The Return Video Chemistry Book Classical Music CD Creative Yell CD From Another Planet DVD Modern Science Book Pop 3 CD Space Force 9 DVD Supernova Video Tank War Video Z Files Video 已选择11行。
3、SQL/92使用using代替on进行简化连接:
SQL> select p.name,pt.name type from products p inner join product_types pt using (product_type_id); NAME TYPE ------------------------------ ---------- Modern Science Book Chemistry Book Z Files Video Tank War Video Supernova Video 2412: The Return Video Space Force 9 DVD From Another Planet DVD Classical Music CD Pop 3 CD Creative Yell CD 已选择11行。 SQL>
------------USING子句不能使用表别名,否则报错: SQL> select p.name,pt.name type,product_type_id from products p inner join product_types pt using (p.product_type_id); select p.name,pt.name type,product_type_id from products p inner join product_types pt using (p.product_type_id) * 第 1 行出现错误: ORA-01748: 此处只允许简单的列名
4、同上,如果希望在查看product_type_id的值,则在SELECT子句中只能指定该列名,不能再在该列名前加表的别名
SQL> select p.name,pt.name type,product_type_id from products p inner join product_types pt using (product_type_id); NAME TYPE PRODUCT_TYPE_ID ------------------------------ ---------- --------------- Modern Science Book 1 Chemistry Book 1 Z Files Video 2 Tank War Video 2 Supernova Video 2 2412: The Return Video 2 Space Force 9 DVD 3 From Another Planet DVD 3 Classical Music CD 4 Pop 3 CD 4 Creative Yell CD 4 已选择11行。 SQL> select p.name,pt.name type,pt.product_type_id from products p inner join product_types pt using (product_type_id); select p.name,pt.name type,pt.product_type_id from products p inner join product_types pt using (product_type_id) * 第 1 行出现错误: ORA-25154: USING 子句的列部分不能有限定词 SQL>
5、使用SQL/92执行多表内连接
SQL/86: SQL> select c.first_name,c.last_name,p.name as product,pt.name as type from customers c,purchases pr,products p,product_types pt 2 where c.customer_id=pr.customer_id and p.product_id=pr.product_id and p.product_type_id=pt.product_type_id order by p.name; FIRST_NAME LAST_NAME PRODUCT TYPE ---------- ---------- ------------------------------ ---------- Cynthis Green Chemistry Book Steve White Chemistry Book Cynthis Green Modern Science Book Steve White Modern Science Book Steve White Supernova Video SQL/92: SQL> select c.first_name,c.last_name,p.name as product,pt.name as type from customers c inner join purchases pr using(customer_id) 2 inner join products p using (product_id) inner join product_types pt using (product_type_id) order by p.name; FIRST_NAME LAST_NAME PRODUCT TYPE ---------- ---------- ------------------------------ ---------- Cynthis Green Chemistry Book Steve White Chemistry Book Cynthis Green Modern Science Book Steve White Modern Science Book Steve White Supernova Video SQL>
6、使用SQL/92执行多列内连接
SELECT……
FROM TABLE1 INNER JOIN TABLE2
ON TABLE1.COLUMN1 = TABLE2.COLUMN1
AND TABLE1.COLUMN2=TABLE2.COLUMN2;
------------使用using进一步简化
SELECT ……
FROM TABLE1 INNER JOIN TABLE2
USING (COLUMN1,COLUMN2);
7、Oracle特有语法(+)执行外连接,而SQL/92标准使用left/right/full outer join执行外连接
不能使用(+)执行全外连接
SQL> select p.name,pt.name from products p, product_types pt 2 where p.product_type_id=pt.product_type_id(+) order by p.name; NAME NAME ------------------------------ ---------- 2412: The Return Video Chemistry Book Classical Music CD Creative Yell CD From Another Planet DVD Modern Science Book My Front Line Pop 3 CD Space Force 9 DVD Supernova Video Tank War Video Z Files Video 已选择12行。
7.1--使用SQL/92标准left outer join重写上面的查询
SQL> select p.name,pt.name from products p left outer join product_types pt using (product_type_id) order by p.name; NAME NAME ------------------------------ ---------- 2412: The Return Video Chemistry Book Classical Music CD Creative Yell CD From Another Planet DVD Modern Science Book My Front Line Pop 3 CD Space Force 9 DVD Supernova Video Tank War Video Z Files Video 已选择12行。
7.2--全外连接(两个表中有空值的行全列出)
SQL> select p.name,pt.name from products p full outer join product_types pt using(product_type_id) order by p.name; NAME NAME ------------------------------ ---------- 2412: The Return Video Chemistry Book Classical Music CD Creative Yell CD From Another Planet DVD Modern Science Book My Front Line Pop 3 CD Space Force 9 DVD Supernova Video Tank War Video Z Files Video Magazine test 已选择14行。
8、使用SQL/92执行自连接
SQL> select * from employees; EMPLOYEE_ID MANAGER_ID FIRST_NAME LAST_NAME TITLE SALARY ----------- ---------- ---------- ---------- -------------------- ---------- 1 James Smith CEO 800000 2 1 Ron Johnson Sales Manager 600000 3 2 Fred Hobbs Salesperson 150000 4 2 Susan Jones Salesperson 500000 SQL> select w.last_name || ‘ works for ‘ || m.last_name from employees w, employees m where w.manager_id=m.employee_id; W.LAST_NAME||‘WORKSFOR‘||M.LAST ------------------------------- Johnson works for Smith Jones works for Johnson Hobbs works for Johnson SQL> select w.last_name || ‘ works for ‘ || m.last_name from employees w inner join employees m on w.manager_id=m.employee_id; W.LAST_NAME||‘WORKSFOR‘||M.LAST ------------------------------- Johnson works for Smith Jones works for Johnson Hobbs works for Johnson
9、使用SQL/92执行自连接
select * from product_types,products; --SQL/82标准
select * from product_types cross join products;--SQL/92标准
时间: 2024-10-06 15:58:01