Oracle培训(二十八)——Oracle 11g 第四章知识点总结——多表查询
Oracle培训(二十八)——Oracle 11g 第四章知识点总结——多表查询
知识点预览
多表查询
多表查询
1. 从多个表中获取获取数据
2. 笛卡尔集
a) 笛卡尔集会在下面条件下产生:
省略连接条件
连接条件无效
所有表中的所有行互相连接
b) 为了避免笛卡尔集, 可以在 WHERE 加入有效的连接条件。
3. 连接的类型
a) Oracle 提供的连接 (8i 或更早):
i. Equijoin
ii. Non-equijoin
iii. Outer join
iv. Self join
b) 适用于SQL: 1999的连接
i. Cross joins
ii. Natural joins
iii. Using clause
iv. Full or two sided outer joins
v. Arbitrary join conditions forouter joins
4. Oracle 连接
a) 使用连接在多个表中查询数据。
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2;
b) 在 WHERE 字句中写入连接条件。
c) 在表中有相同列时,在列名之前加上表名前缀。
5. 等值连接
20. 右外联接
21. 满外联接
22. 增加连接条件
23. 示例
a) 每个子查询当做临时表
多表查询 分步思想 化整为零思想
b) 查询SALES部门的所有雇员的经理名字
c) 查询雇员所在部门的部门名称
--查询出“Americas”所在的地区id Select region_id from regions whereregion_name = ‘Americas’;--查询出地区id是上述结果的这个国家的ID selectc.country_id,c.country_name,r.region_namefrom countries c,( Selectregion_id,region_name fromregions whereregion_name = 'Americas') rwherec.region_id=r.region_id;--查询出有哪些地方在上一步的范围里 selectl.city,l.location_id,t1.country_name,t1.region_namefrom locations l, (selectc.country_id cid,c.country_name,r.region_name fromcountries c,( Selectregion_id,region_name fromregions whereregion_name = 'Americas') r wherec.region_id=r.region_id) t1wherel.country_id=t1.cid; --查询落在上步结果里的部门selectd.department_id,d.department_name,t2.city,t2.country_name,t2.region_namefrom departments d ,(selectl.city,l.location_id,t1.country_name,t1.region_name fromlocations l, (selectc.country_id cid,c.country_name,r.region_name fromcountries c,( Selectregion_id,region_name fromregions whereregion_name = 'Americas') r wherec.region_id=r.region_id) t1 wherel.country_id=t1.cid ) t2whered.location_id=t2.location_id; --用等值连接实现:selecte.first_name,d.department_name,l.cityfrom employees e, departmentsd,locations l,countries c,regions rwhere e.department_id = d.department_id and d.location_id=l.location_idand l.country_id=c.country_id and c.region_id=r.region_id;
k) hr用户解锁,并设置密码123
解锁 以管理员身份登录sys system
Alter user hr account unlock;
设置密码
Alteruser hr identified by 123;
Conn hr/123;