oracle實戰第二天--基本查詢與複雜查詢

內容介紹

1.oracle的表的管理。

2.基本查詢。

3.複雜查詢。

4.oracle數據庫的創建。

 

期望目標

1.掌握oracle表的管理(創建/維護)。

2.掌握對oracle表的各種查詢技巧。

3.學會創建新的oracle數據庫。

 

表名和列的命名規則:

1.必須以字母開頭。

2.長度不能超過30字符。

3.不能使用oracle的保留字。

4.只能使用如下字符A-Z,a-z,0-9,$,#等。

 

Oracle支持的數據類型

 

1.字符型

Char 定長 最大2000字符。

例子:char(10)  ‘小明’前四個字符放’小明’,後添6個空格補全。

Varchar2(20) 變長 最大4000字符。

例子:varchar2(10)  ‘小明’ oracle分配四個字符。這樣可以節省空間。

Clob(character large object)  字符型大對象 最大4G。

 

2.數字型

Number範圍

-10的38次方到10的38次方

可以表示整數,也可以表示小數。

 

Number(5,2)

表示一個小數有5位有效數,2位小數

範圍-999.99  999.99

 

Number(5)

表示一個五位整數

範圍-99999  99999

 

3.日期類型

Date 包括年月日和時分秒。

Timestamp這是oracle9i對date數據類型的擴展。

 

4.圖片

Blob 二進制數據 可以存放圖片/聲音 4G

 

Oracle表的管理

怎樣創建表:

建表

學生表

create table student
(
       sno number(4),
       sname varchar2(20),
       sex char(2),
       birthday date,
       sal number(7,2)
);

 

班級表

create table classes
(
       cid number(2),
       cname varchar2(40)
);

 

修改表

添加一個字段

alter table student add (cid number(2));

修改字段的長度

alter table student modify (sanme varchar2(30));

修改字段的類型或是名字(不能有數據)

alter table student modify (sname char(30));

刪除一個字段

alter table student drop column sal;

修改表的名字

rename student to stu;

刪除表

drop table student;

 

 

 

添加數據

所有字段都插入

insert into student values ('A001','zhangsan','man','01-05月-05',10);

Oracle中默認的日期格式爲’dd-mon-yy’

改日期的格式:

alter session set nls_date_format = 'yyyy-mm-dd';

修改後就可以用我們熟悉的方式進行數據插入

insert into student values('A002','Mike','male','2003-05-09',10);

 

插入部分字段

insert into student(sid,sname,sex) values('A003','john','female');

插入空值

insert into student(sid,sname,sex,birth) 
values('A004','martin','male',null);

 

 

修改數據

 

修改一個字段

update student set sex = 'male' where sid = 'A001'

 

修改多個字段

update student set sex = 'male', birth = '1980-04-01'
where sid = 'A001'

 

修改含有null值的數據

update student set birth = '1900-01-01' where birth is null;

 

 

刪除數據

 

刪除所有記錄,表結構還在,寫日誌,可以恢復的,速度慢。

delete from student;

刪除表的結構和數據。

drop table student;

刪除一條記錄

delete from student where sid = 'A001';

刪除表中所有記錄,表結構還在,不寫日誌,無法找回刪除的記錄,速度快。

truncate table student;

 

 

一個有經驗的DBA         ,在開始工作前會先做一個保存點

Savepoint aa;

以便於在操作出現錯誤時,可以返回到操作之前的狀態

rollback to aa;

 

 

表的基本查詢

以scott用戶中的表(emp,dept)作演示;

Sql>clear;--清屏

查看錶結構:

desc dept;

查詢所有列

select * from dept;

查詢指定列

select ename,sal,job,deptno from emp;

如何取消重複行

select distinct deptno,job from emp;

顯示操作時間

set timing on;

演示查詢所有列和查詢指定列耗時的差別

create table users
(
 userid varchar2(20),
 username varchar2(30),
 userpss varchar2(30)
);

insert into users values('A0001','zhangfachao','[email protected]');

--重複執行這條語句直到插入8192行

insert into users(userid,username,userpss) select * from users;

效果對比

select * from users;

8192 rows selected

Executed in 102.5 seconds

 

select username from users;

8192 rows selected

Executed in 66.86 seconds

從結果可以看出,查詢所有列比查詢指定列耗時得多。

查詢指定一行記錄

select ename,deptno,job,sal from emp where ename='SMITH';

使用算術表達式和列的別名

select ename,sal*12 as yearsal from emp;

如何處理null值

select sal*13+nvl(comm,0)*13 as yearsal,ename from emp;

Nvl函數用於某字段爲空時,給一個默認值。

如何連接字符串(||)

select ename || ' is a ' || job from emp;

 

使用where子句

顯示工資高於3000的員工

select ename,sal,job from emp where sal > 3000;

查出1982.1.1後入職的員工

select ename,sal,job from emp where hiredate > '1-1月-1982';

顯示工資在2000到2500的員工情況

select ename,sal,job from emp where sal between 2000 and 2500;

 

如何使用like操作符:

%:表示0到多個字符

_:表示單個字符

顯示首字符爲S的員工姓名和工資

select ename,sal from emp where ename like 'S%';

顯示第三個字母爲大寫O的所有員工的姓名和工資

select ename,sal from emp where ename like '__O%';

 

在where條件中使用in:

顯示empno爲7844,7788,7698…的僱員情況

select * from emp where empno in (7844,7788,7698);

使用is null的操作符:

顯示沒有上級的僱員的情況

select * from emp where mgr is null;

 

 

使用邏輯操作符號

查詢工資高於500或是崗位爲manager的僱員,同時還要滿足他們的姓名首寫字母爲大寫的J

select * from emp where (sal >500 or job='MANAGER')
and ename like 'J%';

 

使用order by字句

按照工資從低到高的順序顯示僱員的信息

select * from emp order by sal asc;

按照部門號升序而僱員的工資降序排列

select * from emp order by deptno asc,sal desc;

 

使用列的別名排序(中文的列名要使用””號括起來)

select ename,sal*12 "年薪" from emp order by "年薪" asc;

 

 

Oracle表複雜查詢

說明:在實際應用中經常需要執行復雜的數據統計,經常需要顯示多張表的數據,主要用到了select語句

數據分組-max,min,avg,sum,count

如何顯示所有員工中最高工資和最低工資

select max(sal),min(sal) from emp;

顯示所有員工的平均工資和工資總和

select avg(sal),sum(sal) from emp;

計算共有多少員工

select count(*) from emp;

 

擴展要求

請顯示工資最高的員工的名字,工作崗位。

select ename,job from emp where sal in (select max(sal) from emp);

請顯示工資高於平均平均工資的員式信息。

select * from emp where sal > (select avg(sal) from emp);

 

 

Group by和having子句

Group by用於對查詢的結果分組統計

Having子句用於限制分組顯示結果

如何顯示每個部門的平均工資和最高工資

select min(sal),avg(sal),max(sal),deptno from emp group by deptno;

顯示每個部門的每種崗位的平均工資和最高工資

select min(sal),avg(sal),max(sal),deptno,job 
from emp 
group by deptno,job;

顯示平均工資低於2000的部門號和它的平均工資

select min(sal),avg(sal),max(sal),deptno
from emp
group by deptno
having avg(sal) > 2000
order by deptno;

 

 

對數據分組的總結

1.分組函數只能出現在選擇列表、having、order by子句中。

2.如果在select語句中同時包含有group by, having,order by,那麼他們的順序是

Group by

Having

Order by

3.在選擇列中如果有列,表達式和分組函數,那麼這些列和表達式必須有一個出現在group by子句中,否則就會出錯。

如:

select min(sal),avg(sal),max(sal),deptno
from emp
group by deptno
having avg(sal) < 2000;

這裏deptno一定要出現在group by中。

 

 

Oracle表複雜查詢

多表查詢

多表查詢是指基於兩個以上的表或是視圖的查詢。在實際應用中,查詢單個表可能不能滿足你的需求(如顯示sales部門位置和其員工的姓名),這種情況下需要使用到dept表和emp表。

規定:多表查詢的條件是至少不能少於表的個數減1

顯示僱員員,僱員工資及所在部門的名字(笛卡爾集)

select ename,sal,dname from dept,emp where dept.deptno = emp.deptno;

如何顯示部門爲10的員工名、工資、部門名稱。

select e.ename,e.sal,d.dname 
from emp e,dept d 
where e.deptno= d.deptno
and e.deptno = 10;

顯示各個員工的姓名,工資及其工資的級別。

select e.ename,e.sal,s.grade
from emp e,salgrade s
where e.sal between s.losal and s.hisal;

 

擴展要求:

顯示僱員名,僱員工資及所在部門的名字,並按部門排序。

select e.empno,e.sal,d.dname
from emp e,dept d
where e.deptno = d.deptno
order by d.deptno;

 

 

自連接

自連接是指在同一張表的連接查詢

顯示某個員工的上級領導的名字

select e1.ename worker,e2.ename boss
from emp e1,emp e2
where e1.mgr = e2.empno;

擴展要求:

只顯示”FORD”的上級

select e1.ename worker,e2.ename boss
from emp e1,emp e2
where e1.mgr = e2.empno
and e1.ename = 'FORD';

 

 

子查詢

子查詢是指嵌入在其它sql語句中的select語句,也叫嵌套查詢

 

單行子查詢

單行子查詢是指只返回一行數據的子查詢語句

請思考:如何顯示與SMITH同一部門的所有員工?

select * from emp
where deptno = (select deptno from emp where ename = 'SMITH');

 

多行子查詢

多行子查詢指返回多行數據的子查詢。

請思考:如何查詢和部門10的工作相同的僱員的名字、崗位、工資、部門號。

select * from emp 
where job in (select distinct job from emp where deptno = 10);

 

在多行子查詢中使用all操作符

請思考:如何顯示工資比部門30的所有員工的工資高的員工的姓名、工資和部門號

select ename,sal,deptno 
from emp
where sal > all (select sal from emp where deptno = 30);

擴展要求:

想想還有沒有別人的查詢方法。

select ename,sal,deptno 
from emp
where sal > (select max(sal) from emp where deptno = 30);

 

 

在多行子查詢中使用any操作符

請思考:如何顯示工資比部門30的任意一個員工的工資高的員工的姓名、工資、部門號

select ename,sal,deptno 
from emp
where sal > any (select sal from emp where deptno = 30);

擴展要求:

想想還有沒有別的方法

select ename,sal,deptno 
from emp
where sal > (select min(sal) from emp where deptno = 30);

 

 

多列子查詢

多列子查詢是指查詢返回多個列數據的子查詢語句。

請思考:如何查詢與SMITH的部門和崗位完全相同的所有僱員。

select * from emp
where (deptno,job) =
(select deptno,job from emp where ename='SMITH');

 

在from 子句中使用子查詢

請思考:如何顯示高於自己部門平均工資的員工的信息。

select e1.ename,e1.sal,e2.avgsal,e1.deptno from emp e1,
(select deptno,avg(sal) avgsal from emp group by deptno) e2
where e1.deptno = e2.deptno
and e1.sal > e2.avgsal;

 

這裏需要說明的是,當在from子句中使用子查詢時,該子查詢會被當作爲一個視圖來對待,因此叫作內嵌視圖,當在from子句中使用子查詢時,必須給子查詢指定別名。

 

 

分頁查詢

Oracle分頁一共有三種:

假設有七萬條記錄

1.rownum分頁(花了0.1秒,排第二,查詢效率一般,容易理解)

select * from 
(select t.*,rownum rn from 
(select * from emp) t where rownum <10
)where rn > 4;

在分頁基礎上的幾個查詢變化:

a.指定查詢列,只需修改最裏層的子查詢

select * from 
(select t.*,rownum rn from 
(select ename,deptno,sal,job from emp) t where rownum <10
)where rn > 4;

b.如何排序

select * from 
(select t.*,rownum rn from 
(select ename,deptno,sal,job from emp order by deptno desc) t where rownum <10
)where rn > 4;

 

2.根據rowid來分(花了0.03秒,排第一,查詢效率最高,最不容易理解)

select * from emp
where rowid in 
(select rid from 
(select rownum rn ,rid from 
(select rowid rid,deptno from emp order by deptno descwhere rownum<10)
where rn>4) order by deptno desc;

 

3.按分析函數來分(花了1.01秒,排第三,查詢效率最差,最容易理解)

select * from

(select t.*,row_number() over (order by deptno desc) rk from emp t)
where  rk<10 and rk >4;

 

推薦使用第一和第二種。

 

用查詢結果創建新表

這個命令是一種快捷的建表方法

create table t_emp(id,name,sal,job,deptno)
as select empno,ename,sal,job,deptno from emp;

 

 

合併查詢

有時在實際應用中,爲了合併多個select語句的結果,可以使用集合操作符號

Union

union all

intersect

minus

a.union

該操作符用於取得兩個結果集的並集。當使用該操作符時,會自動去掉結果集中重複行,並按第一列默認升序排序。

select ename,sal,job from emp where sal > 2500
union 
select ename,sal,job from emp where job = 'MANAGER';

 

b.union all

該操作相似於union,但是它不會取消重複行,而且不會排序。

select ename,sal,job from emp where sal > 2500
union all
select ename,sal,job from emp where job = 'MANAGER';

 

c.intersect

使用該操作符用於取得兩個結果集的交集。

select ename,sal,job from emp where sal > 2500
intersect
select ename,sal,job from emp where job = 'MANAGER';

 

d.minus

使用該操作符用於取得兩個結果集的差集,它只會顯示存在第一個集合中,而不顯示存在第二個集合中的數據。

select ename,sal,job from emp where sal > 2500
minus
select ename,sal,job from emp where job = 'MANAGER';

 

 

創建數據庫有兩種方法:

1,通過oracle提供的嚮導工具database configuration assistant。

2,我們可以手工步驟直接創建。

發佈了3 篇原創文章 · 獲贊 9 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章