在SQLPLUS下,實現中-英字符集轉換
alter session set nls_language='AMERICAN';
alter session set nls_language='SIMPLIFIED CHINESE';
主要知識點:
1、有關表的操做
1)建表數據庫
create table test as select * from dept; --從已知表複製數據和結構
create table test as select * from dept where 1=2; --從已知表複製結構但不包括數據
2)插入數據:
insert into test select * from dept;session
2、運算符
算術運算符:+ - * / 能夠在select 語句中使用
鏈接運算符:|| select deptno|| dname from dept;
比較運算符:> >= = != < <= like between is null in
邏輯運算符:not and or
集合運算符: intersect ,union, union all, minus
要求:對應集合的列數和數據類型相同
查詢中不能包含long 列
列的標籤是第一個集合的標籤
使用order by時,必須使用位置序號,不能使用列名
例:集合運算符的使用:
intersect ,union, union all, minus
select * from emp intersect select * from emp where deptno=10 ;
select * from emp minus select * from emp where deptno=10;
select * from emp where deptno=10 union select * from emp where deptno in (10,20); --不包括重複行
select * from emp where deptno=10 union all select * from emp where deptno in (10,20); --包括重複行oracle
三,經常使用 ORACLE 函數
sysdate爲系統日期 dual爲虛表
一)日期函數[重點掌握前四個日期函數]
1,add_months[返回日期加(減)指定月份後(前)的日期]
select sysdate S1,add_months(sysdate,10) S2,
add_months(sysdate,5) S3 from dual;
2,last_day [返回該月最後一天的日期]
select last_day(sysdate) from dual;
3,months_between[返回日期之間的月份數]
select sysdate S1, months_between('1-4月-04',sysdate) S2,
months_between('1-4月-04','1-2月-04') S3 from dual
4,next_day(d,day): 返回下個星期的日期,day爲1-7或星期日-星期六,1表示星期日
select sysdate S1,next_day(sysdate,1) S2,
next_day(sysdate,'星期日') S3 FROM DUAL
5,round[舍入到最接近的日期](day:舍入到最接近的星期日)
select sysdate S1,
round(sysdate) S2 ,
round(sysdate,'year') YEAR,
round(sysdate,'month') MONTH ,
round(sysdate,'day') DAY from dual
6,trunc[截斷到最接近的日期]
select sysdate S1,
trunc(sysdate) S2,
trunc(sysdate,'year') YEAR,
trunc(sysdate,'month') MONTH ,
trunc(sysdate,'day') DAY from dual
7,返回日期列表中最晚日期
select greatest('01-1月-04','04-1月-04','10-2月-04') from dual函數
二)字符函數(可用於字面字符或數據庫列)
1,字符串截取
select substr('abcdef',1,3) from dual
2,查找子串位置
select instr('abcfdgfdhd','fd') from dual
3,字符串鏈接
select 'HELLO'||'hello world' from dual;
4, 1)去掉字符串中的空格
select ltrim(' abc') s1,
rtrim('zhang ') s2,
trim(' zhang ') s3 from dual
2)去掉前導和後綴
select trim(leading 9 from 9998767999) s1,
trim(trailing 9 from 9998767999) s2,
trim(9 from 9998767999) s3 from dual;
5,返回字符串首字母的Ascii值
select ascii('a') from dual
6,返回ascii值對應的字母
select chr(97) from dual
7,計算字符串長度
select length('abcdef') from dual
8,initcap(首字母變大寫) ,lower(變小寫),upper(變大寫)
select lower('ABC') s1,
upper('def') s2,
initcap('efg') s3 from dual;
9,Replace
select replace('abc','b','xy') from dual;
10,translate
select translate('abc','b','xx') from dual; -- x是1位
11,lpad [左添充] rpad [右填充](用於控制輸出格式)
select lpad('func',15,'=') s1, rpad('func',15,'-') s2 from dual;
select lpad(dname,14,'=') from dept;
12, decode[實現if ..then 邏輯]
select deptno,decode(deptno,10,'1',20,'2',30,'3','其餘') from dept;
三)數字函數
1,取整函數(ceil 向上取整,floor 向下取整)
select ceil(66.6) N1,floor(66.6) N2 from dual;
2, 取冪(power) 和 求平方根(sqrt)
select power(3,2) N1,sqrt(9) N2 from dual;
3,求餘
select mod(9,5) from dual;
4,返回固定小數位數 (round:四捨五入,trunc:直接截斷)
select round(66.667,2) N1,trunc(66.667,2) N2 from dual;
5,返回值的符號(正數返回爲1,負數爲-1)
select sign(-32),sign(293) from dual;
四)轉換函數
1,to_char()[將日期和數字類型轉換成字符類型]
1) select to_char(sysdate) s1,
to_char(sysdate,'yyyy-mm-dd') s2,
to_char(sysdate,'yyyy') s3,
to_char(sysdate,'yyyy-mm-dd hh12:mi:ss') s4,spa
to_char(sysdate, 'hh24:mi:ss') s5,
to_char(sysdate,'DAY') s6 from dual;
2) select sal,to_char(sal,'$99999') n1,to_char(sal,'$99,999') n2 from emp
2, to_date()[將字符類型轉換爲日期類型]
insert into emp(empno,hiredate) values(8000,to_date('2004-10-10','yyyy-mm-dd'));
3, to_number() 轉換爲數字類型
select to_number(to_char(sysdate,'hh12')) from dual; //以數字顯示的小時數
五)其餘函數
user:
返回登陸的用戶名稱
select user from dual;
vsize:
返回表達式所需的字節數
select vsize('HELLO') from dual;
nvl(ex1,ex2):
ex1值爲空則返回ex2,不然返回該值自己ex1(經常使用)
例:若是僱員沒有佣金,將顯示0,不然顯示佣金
select comm,nvl(comm,0) from emp;
nullif(ex1,ex2):
值相等返空,不然返回第一個值
例:若是工資和佣金相等,則顯示空,不然顯示工資
select nullif(sal,comm),sal,comm from emp;
coalesce:
返回列表中第一個非空表達式
select comm,sal,coalesce(comm,sal,sal*10) from emp;
nvl2(ex1,ex2,ex3) :
若是ex1不爲空,顯示ex2,不然顯示ex3
如:查看有佣金的僱員姓名以及他們的佣金
select nvl2(comm,ename,') as HaveCommName,comm from emp;
六)分組函數
max min avg count sum
1,整個結果集是一個組
1) 求部門30 的最高工資,最低工資,平均工資,總人數,有工做的人數,工種數量及工資總和
select max(ename),max(sal),
min(ename),min(sal),
avg(sal),
count(*) ,count(job),count(distinct(job)) ,
sum(sal) from emp where deptno=30;
2, 帶group by 和 having 的分組
1)按部門分組求最高工資,最低工資,總人數,有工做的人數,工種數量及工資總和
select deptno, max(ename),max(sal),
min(ename),min(sal),
avg(sal),
count(*) ,count(job),count(distinct(job)) ,
sum(sal) from emp group by deptno;
2)部門30的最高工資,最低工資,總人數,有工做的人數,工種數量及工資總和
select deptno, max(ename),max(sal),
min(ename),min(sal),
avg(sal),
count(*) ,count(job),count(distinct(job)) ,
sum(sal) from emp group by deptno having deptno=30;
3, stddev 返回一組值的標準誤差
select deptno,stddev(sal) from emp group by deptno;
variance 返回一組值的方差差
select deptno,variance(sal) from emp group by deptno;
4, 帶有rollup和cube操做符的Group By
rollup 按分組的第一個列進行統計和最後的小計
cube 按分組的全部列的進行統計和最後的小計
select deptno,job ,sum(sal) from emp group by deptno,job;
select deptno,job ,sum(sal) from emp group by rollup(deptno,job);
cube 產生組內全部列的統計和最後的小計
select deptno,job ,sum(sal) from emp group by cube(deptno,job);翻譯
7、臨時表
只在會話期間或在事務處理期間存在的表.
臨時表在插入數據時,動態分配空間
create global temporary table temp_dept
(dno number,
dname varchar2(10))
on commit delete rows;
insert into temp_dept values(10,'ABC');
commit;
select * from temp_dept; --無數據顯示,數據自動清除
on commit preserve rows:在會話期間表一直能夠存在(保留數據)
on commit delete rows:事務結束清除數據(在事務結束時自動刪除表的數據)code
========================== 鏈接操做 ================================
例若有A,B兩張表:
A表 B表
a b c d
1 0 4 7
2 9 1 5
select * from A,B where A.a = B.c
等同於
select * from A join B on A.a = B.c
結果:
a b c d
1 0 1 5
左鏈接:
select * from A,B where A.a = B.c(+)
等同於
select * from A left join B on A.a = B.c
即以A表查詢爲主,附帶查詢出知足A.a = B.c條件的B表中的結果
結果:
a b c d
1 0 1 5
2 9
右鏈接:
select * from A,B where A.a(+) = B.c
等同於
select * from A right join B on A.a = B.c
此查詢爲左鏈接,即以A表查詢爲主,附帶查詢出知足A.a = B.c條件的B表中的結果
結果:
c d a b
1 5 1 0
4 7
===============================================================
===================== oracle 函數 ===============================
DECODE:
decode(條件,值1,翻譯值1,值2,翻譯值2,...值n,翻譯值n,缺省值)
A表
a b c
1 3 0
3 2 9
1 0 5
3 7 7
5 5 9
select decode(A.a,1,4,3,8),A.b,A.c form A
意思:若是A.a的值爲1,則將A.a的值查詢爲4,若是A.a的值爲3,則將A.a的值查詢爲8
結果:
a b c
4 3 0
8 2 9
4 0 5
8 7 7
5 5 9
select decode(A.a,1,4,8),A.b,A.c form A
意思:若是A.a的值爲1,則將A.a的值查詢爲4,不然A.a的值查詢爲8
結果:
a b c
4 3 0
8 2 9
4 0 5
8 7 7
8 5 9
select decode(A.a,1,4,3,8,7),A.b,A.c form A
意思:若是A.a的值爲1,則將A.a的值查詢爲4,若是A.a的值爲3,則將A.a的值查詢爲8,不然查詢爲7
結果:
a b c
4 3 0
8 2 9
4 0 5
8 7 7
7 5 9
===============================================================
==========================SEQUENCE=====================================
SEQUENCE: CREATE SEQUENCE LOCATIONS_SEQ
INCREMENT BY 1
START WITH 1
MAXVALUE 9900
MINVALUE 1
SELECT LOCATIONS_SEQ.currval FROM DUAL ;
SELECT LOCATIONS_SEQ.nextval FROM DUAL ;
============================SEQUENCE===================================
============================函數===================================
日期函數:
add_months(d,n) 日期d加n個月
last_day(d) 包含d的月份的最後一天的日期
month_between(d,e) 日期d與e之間的月份數,e先於d
new_time(d,a,b) a時區的日期和時間d在b時區的日期和時間
next_day(d,day) 比日期d晚,由day指定的周幾的日期
sysdate 當前的系統日期和時間
greatest(d1,d2,...dn) 給出的日期列表中最後的日期
least(d1,k2,...dn) 給出的日期列表中最先的日期
to_char(d [,fmt]) 日期d按fmt指定的格式轉變成字符串
to_date(st [,fmt]) 字符串st按fmt指定的格式轉成日期值,若fmt忽略,st要用缺省格式
round(d [,fmt]) 日期d按fmt指定格式舍入到最近的日期
trunc(d [,fmt]) 日期d按fmt指定格式截斷到最近的日期
分組函數:
avg([distinct/all] n) 列n的平均值
count([all] *) 返回查詢範圍內的行數包括重複值和空值
count([distinct/all] n) 非空值的行數
max([distinct/all] n) 該列或表達式的最大值
min([distinct/all] n) 該列或表達式的最小值
stdev([distinct/all] n) 該列或表達式的標準誤差,忽略空值
sum([distinct/all] n) 該列或表達式的總和
variance([distinct/all] n) 該列或表達式的方差,忽略空值
數值函數:
abs(m) m的絕對值
mod(m,n) m被n除後的餘數
power(m,n) m的n次方
round(m[,n]) m四捨五入至小數點後n位的值(n缺省爲0)
trunc(m[,n]) m截斷n位小數位的值(n缺省爲0)
字符函數:
initcap(st) 返回st將每一個單詞的首字母大寫,全部其餘字母小寫
lower(st) 返回st將每一個單詞的字母所有小寫
upper(st) 返回st將每一個單詞的字母所有大寫
concat(st1,st2) 返回st爲st2接st1的末尾(可用操做符"||")
lpad(st1,n[,st2]) 返回右對齊的st,st爲在st1的左邊用st2填充直至長度爲n,st2的缺省爲空格
rpad(st1,n[,st2]) 返回左對齊的st,st爲在st1的右邊用st2填充直至長度爲n,st2的缺省爲空格
ltrim(st[,set]) 返回st,st爲從左邊刪除set中字符直到第一個不是set中的字符。缺省時,指的是空格
rtrim(st[,set]) 返回st,st爲從右邊刪除set中字符直到第一個不是set中的字符。缺省時,指的是空格
replace(st,search_st[,replace_st]) 將每次在st中出現的search_st用replace_st替換,返回一個st。缺省時,刪除search_st
substr(st,m[,n]) n=返回st串的子串,從m位置開始,取n個字符長。缺省時,一直返回到st末端
length(st) 數值,返回st中的字符數
instr(st1,st2[,m[,n]]) 數值,返回st1從第m字符開始,st2第n次出現的位置,m及n的缺省值爲1 orm