62. 查詢員工表中 salary 前 10 的員工信息.
select last_name, salary
from (select last_name, salary from employees order by salary desc)
where rownum <= 10
說明: rownum "僞列" ---- 數據表本身並沒有這樣的列, 是 oracle 數據庫爲每個數據表 "加上的" 列.
可以標識行號.默認情況下 rownum 按主索引來排序. 若沒有主索引則自然排序.
注意: **對 ROWNUM 只能使用 < 或 <=, 而是用 =, >, >= 都將不能返回任何數據.
63. 查詢員工表中 salary 10 - 20 的員工信息.
select *
from(
select rownum rn, temp.*
from (
select last_name, salary
from employees e
order by salary desc
) temp
)
where rn > 10 and rn < 21
64. 對 oralce 數據庫中記錄進行分頁: 每頁顯示 10 條記錄, 查詢第 5 頁的數據
select employee_id, last_name, salary
from (
select rownum rn, employee_id, last_name, salary
from employees
) e
where e.rn <= 50 and e.rn > 40
注意: **對 oracle 分頁必須使用 rownum "僞列"!
select employee_id, last_name, salary
from (
select rownum rn, employee_id, last_name, salary
from employees
) e
where e.rn <= pageNo * pageSize and e.rn > (pageNo - 1) * pageSize
/*************************************************************************************************/
1. 使用表employees創建視圖employee_vu,其中包括姓名(LAST_NAME),員工號(EMPLOYEE_ID),部門號(DEPARTMENT_ID).
a) create or replace view employee_vu
b) as
c) select last_name,employee_id,department_id
d) from employees
2. 顯示視圖的結構
desc employee_vu;
3. 查詢視圖中的全部內容
SELECT * FROM employee_vu;
4. 將視圖中的數據限定在部門號是80的範圍內
a) create or replace view employee_vu
b) as
c) select last_name,employee_id,department_id
d) from employees
e) where department_id = 80
5. 將視圖改變成只讀視圖
create or replace view employee_vu
as
select last_name,employee_id,department_id
from employees
where department_id = 80
with read only