-------------------------------------------------------------------------------------準備篇mysql
col empno for 9999;
col ename for a10;
col job for a10;
col mgr for 9999;
col hiredate for a12;
col sal for 9999;
col comm for 9999;
col deptno for 99;
set pagesize 20;
col tname for a20;
set pagesize 80; sql
-------------------------------------------------------------------------------------通用函數和條件判斷函數服務器
使用NVL(a,b)通用函數,統計員工年收入,NVL()做用於任何類型,即(number/varchar2/date)
通用函數:參數類型能夠是number或varchar2或date類型
select ename,sal*12+NVL(comm,0) from emp;oracle
使用NVL2(a,b,c)通用函數,若是a不爲NULL,取b值,不然取c值,統計員工年收入
select ename,sal*12+NVL2(comm,comm,0) from emp;函數
使用NULLIF(a,b)通用函數,在類型一致的狀況下,若是a與b相同,返回NULL,不然返回a,比較10和10.0是否相同
select NULLIF(10,'10') from dual;測試
使用SQL99標準通用語法中的case表達式,將職位是分析員的,工資+1000;職位是經理的,工資+800;職位是其它的,工資+400
case 字段
when 條件1 then 表達式1
when 條件2 then 表達式2
else 表達式n
end
課後請參考<MySQL5.X的手冊>-12.2這個章節
select ename "姓名",job "職位",sal "漲前工資",
case job
when 'ANALYST' then sal+1000
when 'MANAGER' then sal+800
else sal+400
end "漲後工資"
from emp; 優化
使用oracle專用語法中的decode()函數,職位是分析員的,工資+1000;職位是經理的,工資+800;職位是其它的,工資+400
decode(字段,條件1,表達式1,條件2,表達式2,...表達式n)
select ename "姓名",job "職位",sal "漲前工資",
decode(job,'ANALYST',sal+1000,'MANAGER',sal+800,sal+400) "漲後工資"
from emp; hibernate
單引號出現的地方以下:
1)字符串,例如:'hello'
2)日期型,例如:'17-12月-80'
3)to_char/to_date(日期,'YYYY-MM-DD HH24:MI:SS')code
雙引號出現的地方以下:
1)列別名,例如:select ename "姓 名" from emp
2)to_char/to_date(日期,'YYYY"年"MM"月"DD"日" HH24:MI:SS')‘’號中的英文字符大小寫不敏感視頻
-------------------------------------------------------------------------------------多行函數
函數:oracle服務器先事寫好的一段具備必定功能的程序片斷,內置於oracle服務器,供用戶調用
單行函數:輸入一個參數,輸出一個結果,例如:upper('baidu.com')->BAIDU.COM
多行函數:輸入多個參數,或者是內部掃描屢次,輸出一個結果,例如:count(*)->14
統計emp表中員工總人數
select count(*) from emp;
*號適用於表字段較少的狀況下,若是字段較多,掃描多間多,效率低,項目中提倡使用某一個非null惟一的字段,一般是主鍵
統計公司有多少個不重複的部門
select count(distinct deptno) from emp;
統計有佣金的員工人數
select count(comm) from emp;
注意:今天講的這些多個行函數,不統計NULL值
員工總工資,平均工資,四捨五入,保留小數點後0位
select sum(sal) "總工資",round(avg(sal),0) "平均工資"
from emp;
查詢員工表中最高工資,最低工資
select max(sal) "最高工資",min(sal) "最低工資"
from emp;
入職最先,入職最晚員工
select max(hiredate) "最晚入職時間",min(hiredate) "最先入職時間"
from emp;
多行函數:count/sum/avg/max/min
按部門求出該部門平均工資,且平均工資取整數,採用截斷
select deptno "部門編號",trunc(avg(sal),0) "部門平均工資"
from emp
group by deptno;
(繼續)查詢部門平均工資大於2000元的部門
select deptno "部門編號",trunc(avg(sal),0) "部門平均工資"
from emp
group by deptno
having trunc(avg(sal),0) > 2000;
(繼續)按部門平均工資降序排列
select deptno "部門編號",trunc(avg(sal),0) "部門平均工資"
from emp
group by deptno
having trunc(avg(sal),0) > 2000
order by 2 desc;
除10號部門外,查詢部門平均工資大於2000元的部門,方式一【having deptno<>10】
select deptno,avg(sal)
from emp
group by deptno
having deptno<>10;
除10號部門外,查詢部門平均工資大於2000元的部門,方式二【where deptno<>10】
select deptno,avg(sal)
from emp
where deptno<>10
group by deptno;
提倡
顯示部門平均工資的最大值
select max(avg(sal)) "部門平均工資的最大值"
from emp
group by deptno;
思考:顯示部門平均工資的最大值和該部門編號?
select max(avg(sal)) "部門平均工資的最大值",deptno "部門編號"
from emp
group by deptno;
錯誤
group by 子句的細節:
1)在select子句中出現的非多行函數的全部列,【必須】出如今group by子句中
2)在group by子句中出現的全部列,【可出現可不現】在select子句中
where和having的區別:
where:
1)行過濾器
2)針對原始的記錄
3)跟在from後面
4)where可省
5)先執行
having:
1)組過濾器
2)針對分組後的記錄
3)跟在group by後面
4)having可省
5)後執行
oracle中綜合語法:
1)select子句-----必須
2)from子句-------必須,不知寫什麼表了,就寫dual
3)where子句------可選
4)group by子句---可選
5)having子句-----可選
6)order by 子句--可選,若是出現列名,別名,表達式,字段
-------------------------------------------------------------------------------------多表查詢
員工表emp和部門表dept的笛卡爾集(笛卡爾集表=列數之和,行數之積,笛卡爾集表內中有些數據是不符合要求的)
select emp.ename,dept.dname
from emp,dept;
使用等值鏈接/內鏈接(只能使用=號),顯示員工的編號,姓名,部門名,使用表別名簡化
select emp.empno,emp.ename,dept.dname,dept.deptno
from emp,dept
where emp.deptno = dept.deptno;
使用非等值鏈接(不能使用=號,其它符號能夠,例如:>=,<=,<>,betwen and等),顯示員工的編號,姓名,月薪,工資級別
select e.empno,e.ename,e.sal,s.grade
from emp e,salgrade s
where e.sal between s.losal and s.hisal;
內鏈接查詢:只能查詢出符合條件的記錄
外鏈接查詢:既能查詢出符合條件的記錄,也能根據一方強行將另外一個方查詢出來
使用外鏈接,按部門10,20,30,40號,統計各部門員工人數,要求顯示部門號,部門名,人數
部門號 部門名 人數
10 ACCOUNTING 3
20 RESEARCH 5
30 SALES 6
40 OPERATIONS 0
等值鏈接/非等值鏈接/內鏈接:只會查詢出多張表中,根據某個字段匹配,符合條件的記錄,不符合條件的記錄是不會存在的
左外鏈接[是oracle專用的,不是SQL99規則]:
select dept.deptno "部門號",dept.dname "部門名",count(emp.empno) "人數"
from dept,emp
where dept.deptno = emp.deptno(+)
group by dept.deptno,dept.dname;
右外鏈接:
select dept.deptno "部門號",dept.dname "部門名",count(emp.empno) "人數"
from dept,emp
where emp.deptno(+) = dept.deptno
group by dept.deptno,dept.dname;
使用左外鏈接,按部門10,20,30,40號,統計各部門員工人數,要求顯示部門號,部門名,人數,且按人數降序排列
select dept.deptno "部門號",dept.dname "部門名",count(emp.empno) "人數"
from dept,emp
where dept.deptno = emp.deptno(+)
group by dept.deptno,dept.dname
order by 3 desc;
使用自鏈接,顯示"SMITH的上級是FORD"這種格式
select users.ename || '的上級是' ||boss.ename
from emp users,emp boss
where users.mgr = boss.empno;
只有13條記錄,不含有KING
基於上述問題,將KING的上級是「」顯示出來
select users.ename || '的上級是' ||boss.ename
from emp users,emp boss
where users.mgr = boss.empno(+);
14條記錄
注意:自鏈接也用到內鏈接和外鏈接
-------------------------------------------------------------------------------------子查詢
子查詢的做用:查詢條件未知的事物
查詢條件已知的問題:例如:查詢工資爲800的員工信息
查詢條件未知的問題:例如:查詢工資爲20號部門平均工資的員工信息
一個條件未知的問題,能夠分解爲多個條件已知的問題
查詢工資比WARD高的員工信息
第一:查詢WARD的工資?
select sal from emp where ename = 'WARD';
第二:查詢工資比1250高的員工信息?
select * from emp where sal > 1250;
子查詢:
select *
from emp
where sal > (
select sal
from emp
where ename = 'WARD'
);
查詢部門名爲'SALES'的員工信息(方式一:子查詢)
第一:查詢部門名爲'SALES'的編號?
select deptno from dept where dname = 'SALES';
第二:查詢部門號爲30的員工信息?
select * from emp where deptno = 30;
子查詢:
select *
from emp
where deptno = (
select deptno
from dept
where dname = 'SALES'
);
子查詢細節:
1)子查詢與父查詢能夠針對同一張表
2)子查詢與父查詢能夠針對不一樣張表
3) 子查詢與父查詢在傳統參數時,數量要相同
4) 子查詢與父查詢在傳統參數時,類型要相同
5) 子查詢與父查詢在傳統參數時,含義要相同
查詢部門名爲'SALES'的員工信息(方式二:多表查詢)
select emp.*
from dept,emp
where (dept.deptno=emp.deptno) and (dept.dname='SALES');
查詢每一個員工編號,姓名,部門名,工資等級(三表查詢,這三張表並沒有外健關聯)
select e.empno,e.ename,d.dname,s.grade
from emp e,dept d,salgrade s
where (e.deptno=d.deptno) and (e.sal between s.losal and s.hisal);
查詢工資最低的員工信息(單行子查詢,使用=號)
第一:查詢出工資最低是多少?
select min(sal) from emp;
第二:查詢工資爲800的員工信息?
select * from emp where sal = 800;
子查詢:
select *
from emp
where sal = (
select min(sal)
from emp
);
查詢部門名爲'ACCOUNTING'或'SALES'的員工信息(多行子查詢,使用in關鍵字)
第一:查詢部門名爲'ACCOUNTING'或'SALES'的部門編號?
select deptno from dept where dname in ('ACCOUNTING','SALES');
第二:查詢部門號爲10或30號的員工信息?
select * from emp where deptno in (10,30);
子查詢:
select *
from emp
where deptno in (
select deptno
from dept
where dname in ('ACCOUNTING','SALES')
);
查詢工資比20號部門【任意any】一個員工工資【低<】的員工信息(多行子查詢,使用any關鍵字)
第一:查詢20號部門的全部工資?
select sal from emp where deptno = 20;
第二:查詢工資比(800,2975,3000,1100,3000)任意一個低的員工信息?
select * from emp where sal < any (800,2975,3000,1100,3000);
在oracle看來,<any就等於<集合中最大的那個值
子查詢:
select *
from emp
where sal <any (
select sal
from emp
where deptno = 20
);
查詢工資比30號部門【全部all】員工【低<】的員工信息(多行子查詢,使用all關鍵字)
第一:查詢出30部門全部員工的工資?
select sal from emp where deptno = 30;
第二:查詢工資比(1600,1250,1250,2850,1500,950)中全部的工資都低的員工信息?
select * from emp where sal <all (1600,1250,1250,2850,1500,950);
子查詢:
select *
from emp
where sal <all (
select sal
from emp
where deptno = 30
);
注意:學員們,不容易理解的幾個概念:
單行函數:輸入一個參數,輸出一個結果
多行函數:掃描多個參數,輸出一個結果
單行子查詢:子查詢只會返回一個結果,例如:800,父查詢用=/<>/>=/<=這些符號來比較
多行子查詢:子查詢會返回多於一個結果,例如:30,20,父查詢用in/any/all這些符號來比較
當多表查詢,子查詢同時能解決問題時,按以下優先方案選擇:
多表查詢-->子查詢
注意:上述結果不是說多表查詢能夠替代子查詢,某些狀況下,只能用子查詢解決,例如:oracle分頁
-------------------------------------------------------------------------------------集合查詢
使用並集運算,查詢20號部門或30號部門的員工信息
select * from emp where deptno = 20
union
select * from emp where deptno = 30;
注意:
union:二個集合中,若是都有相同的,取其一
union all:二個集合中,若是都有相同的,都取
使用set time/timing on,打開時間的開關
set time on;
set time off;
使用set tim/timing off,關閉時間的開關
set timing on;
set timint off;
使用交集運算[intersect],查詢工資在1000-2000和1500-2500之間的員工信息(方式一)
select * from emp where sal between 1000 and 2000
intersect
select * from emp where sal between 1500 and 2500;
用where行過濾,查詢工資在1000-2000和1500-2500之間的員工信息(方式二)
select *
from emp
where (sal between 1000 and 2000) and (sal between 1500 and 2500);
使用差集運算[minus],查詢工資在1000-2000,但不在1500-2500之間的員工信息(方式一)
select * from emp where sal between 1000 and 2000
minus
select * from emp where sal between 1500 and 2500;
使用where行過濾,查詢工資在1000-2000,但不在1500-2500之間的員工信息(方式二)
select *
from emp
where (sal between 1000 and 2000) and (sal not between 1500 and 2500);
集合查詢的細節:
1)集合操做時,必須確保集合列數是相等
select empno,ename,sal,comm from emp where deptno = 20
union
select empno,ename,sal from emp where deptno = 30;錯
2)集合操做時,必須確保集合列類型對應相同
select empno,ename,sal,comm from emp where deptno = 20
union
select empno,ename,sal,hiredate from emp where deptno = 30;錯
3)A union B union C = C union B union A
select * from emp where deptno = 10
union
select * from emp where deptno = 20
union
select * from emp where deptno = 30;
4)當多個集合操做時,結果的列名由第一個集合列名決定
select empno "編號",ename "姓名",sal "薪水" from emp where deptno = 20
union
select empno,ename,sal from emp where deptno = 10;
當多表查詢,子查詢,集合查詢都能完成一樣任務時,按以下優化方案選擇:
多表查詢->子查詢->集合查詢
-------------------------------------------------------------------------------------oracle分頁
回顧mysql分頁
用limit關鍵字
查詢users表中前二條記錄
select * from users limit 0,2
或
select * from users limit 2;
0表示第一條記錄的索引號,索引號從0開始
2表示最多選取二個記錄
查詢出users前三條記錄
select * from users limit 0,3
或
select * from users limit 3
查詢出users第2條到第4條記錄
select * from users limit 1,3;
回顧hibernate分頁API
Query.setFirstResult(0);
Query.setMaxResult(3);
什麼是rownum,有何特色
1)rownum是oracle專用的關健字
2)rownum與表在一塊兒,表亡它亡,表在它在
3)rownum在默認狀況下,從表中是查不出來的
4)只有在select子句中,明確寫出rownum才能顯示出來
5)rownum是number類型,且惟一連續
6)rownum最小值是1,最大值與你的記錄條數相同
7)rownum也能參與關係運算
* rownum = 1 有值
* rownum < 5 有值
* rownum <=5 有值
* rownum > 2 無值
* rownum >=2 無值
* rownum <>2 有值 與 rownum < 2 相同
* rownum = 2 無值
8)基於rownum的特性,咱們一般rownum只用於<或<=關係運算
顯示emp表中3-8條記錄(方式一:使用集合減運算)
select rownum "僞列",emp.* from emp where rownum<=8
minus
select rownum,emp.* from emp where rownum<=2;
顯示emp表中3-8條記錄(方式二:使用子查詢,在from子句中使用,重點)
select xx.*
from (select rownum ids,emp.* from emp where rownum<=8) xx
where ids>=2;
注意:在子查詢中的別名,不可加""引號
顯示emp表中5-9條記錄
select yy.*
from (select rownum ids,emp.* from emp where rownum<=9) yy
where ids>=5;
注意:在項目中,from後臺可能有真實表名,也可能用子查詢看做的表名,
同時真實表和子查詢看做的表要作鏈接查詢
-------------------------------------------------------------------------------------建立表和約束
回顧MySQL建立表語句users(id整型/name字符串/birthday日期型,默認今天)
drop table if exists users;
create table if not exists users(
id int(5) auto_increment primary key,
name varchar(4) not null,
birthday date default '2015-4-27'
);
使用oracleSQL,建立用戶表users(id整型/name字符串/birthday日期/sal整型,默認今天)
create table users(
id number(5) primary key,
name varchar2(8) not null unique,
sal number(6,2) not null,
birthday date default sysdate
);
進入回收站
drop table users;
查詢回收站中的對象
show recyclebin;
閃回,即將回收站還原
flashback table 表名 to before drop;
flashback table 表名 to before drop rename to 新表名;
完全刪除users表
drop table users purge;
清空回收站
purge recyclebin;
測試以下類型
(1)number(5):
insert into users(id,name,sal) values(1,'A',6666.66);
insert into users(id,name,sal) values(11,'AA',6666.66);
insert into users(id,name,sal) values(111,'AAA',6666.66);
insert into users(id,name,sal) values(1111,'AAAA',6666.66);
insert into users(id,name,sal) values(99999,'AAAAA',6666.66);
insert into users(id,name,sal) values(100000,'AAAAAA',6666.66); 錯
5表示最多存99999
(2)number(6,2):
col sal for 9999.99
insert into users(id,name,sal) values(1,'A',6.66);
insert into users(id,name,sal) values(11,'AA',66.666);
insert into users(id,name,sal) values(111,'AAA',666.6666);
insert into users(id,name,sal) values(1111,'AAAA',6666.66666);
insert into users(id,name,sal) values(11111,'AAAAA',66666.666666);錯
number(6,2)
其中2表示最多顯示2位小數,採用四捨五入,不足位數補0,同時要設置col ... for ...
其中6表示小數+整數很少於6位
其中整數位數不得多於4位,能夠等於4位
(3)varchar2(8):
insert into users(id,name,sal) values(1,'A',7777.77);
insert into users(id,name,sal) values(2,'AA',7777.77);
insert into users(id,name,sal) values(3,'AAA',7777.77);
insert into users(id,name,sal) values(4,'AAAA',7777.77);
insert into users(id,name,sal) values(5,'AAAAA',7777.77);
insert into users(id,name,sal) values(6,'AAAAAA',7777.77);
insert into users(id,name,sal) values(7,'AAAAAAA',7777.77);
insert into users(id,name,sal) values(8,'AAAAAAAA',7777.77);
insert into users(id,name,sal) values(9,'AAAAAAAAA',7777.77);錯
insert into users(id,name,sal) values(1,'哈',7777.77);
insert into users(id,name,sal) values(2,'哈哈',7777.77);
insert into users(id,name,sal) values(3,'哈哈哈',7777.77);
insert into users(id,name,sal) values(4,'哈哈哈哈',7777.77);
insert into users(id,name,sal) values(5,'哈哈哈哈哈',7777.77);錯
8表示字節
GBK 趙 2字節
(4)date:默認格式爲:'27-4月-15'
(5)CLOB【Character Large OBject】:大文本對象,即超過65565字節的數據對象,最多存儲4G
(6)BLOB【Binary Large OBject】:大二進制對象,即圖片,音頻,視頻,最多存儲4G
爲emp表增長image列,alter table 表名 add 列名 類型(寬度)
alter table emp
add image blob;
修改ename列的長度爲20個字節,alter table 表名 modify 列名 類型(寬度)
alter table emp
modify ename varchar2(20);
刪除image列,alter table 表名 drop column 列名
alter table emp
drop column image;
重名列名ename爲username,alter table 表名 rename column 原列名 to 新列名
alter table emp
rename column ename to username;
將emp表重命名emps,rename 原表名 to 新表名
rename emp to emps;
注意:修改表時,不會影響表中原有的數據
筆試題:有【1000億】條會員記錄,如何用最高效的方式將薪水字段清零,其它字段內容不變?
第一:從emp表中刪除sal字段
alter table emp
drop column sal;
第二:向emp表中添加sal字段,且內容默認0
alter table emp
add sal number(6) default 0;
修改表不可回滾
建立表customers(單)和orders(多),使用primary key/not null/unique/default/foreign key約束
要體現【on delete cascade/on delete set null】
需求:刪除客戶,級聯刪除他全部的訂單
delete from customers where id = 1;
需求:刪除客戶,不級聯刪除他全部的訂單,只是將外健設置爲NULL
delete from customers where id = 1;
create table customers(
id number(3) primary key,
name varchar2(4) not null unique
);
insert into customers(id,name) values(1,'A');
insert into customers(id,name) values(2,'B');
create table orders(
id number(3) primary key,
isbn varchar2(6) not null unique,
price number(3) not null,
cid number(3),
--constraint cid_FK foreign key(cid) references customers(id) on delete cascade
constraint cid_FK foreign key(cid) references customers(id) on delete set null
);
insert into orders(id,isbn,price,cid) values(1,'isbn10',10,1);
insert into orders(id,isbn,price,cid) values(2,'isbn20',20,1);
insert into orders(id,isbn,price,cid) values(3,'isbn30',30,2);
insert into orders(id,isbn,price,cid) values(4,'isbn40',40,2);
建立表students,包括id,name,gender,salary字段,使用check約束【性別只能是男或女,薪水介於6000到8000之間】 create table students( id number(3) primary key, name varchar2(4) not null unique, gender varchar2(2) not null check ( gender in ('男','女') ), salary number(6) not null check ( salary between 6000 and 8000 ) ); insert into students(id,name,gender,salary) values(1,'哈哈','中',6000);錯 insert into students(id,name,gender,salary) values(2,'呵呵','男',5000);錯 insert into students(id,name,gender,salary) values(3,'嘻嘻','女',7000);對