oracle培訓第一天

0.常用命令
tnsping orcl(Transparent Network Substrate透明網絡底層)
查看監聽狀態lsnrctl status
監聽停止 lsnrctl stop
監聽啓動 lsnrctl start
sqlplus /nolog
conn /as sysdba
數據庫關閉 shutdown immediate,shutdown abort
數據庫啓動 startup
upgrade
select status from v$instance;
alter user scott account unlock;
alter user scott identified by bsoft;

1. oracle命令類別
   數據庫操縱語言 DML:select,insert,delete,update,merge

merge into products p using newproducts np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name
when not matched then
insert values(np.product_id, np.product_name, np.category)

數據定義語言 DDL:create ,alter,drop,truncate,rename,comment
create table TEST
ID VARCHAR2(10),
NAME VARCHAR2(30)
);

ALTER TABLE test ADD address varchar2(20); 
ALTER TABLE test MODIFY address varchar2(30); 
ALTER TABLE test RENAME column address TO newaddress;
ALTER TABLE test DROP COLUMN newaddress; 
ALTER TABLE test RENAME TO test1;

rename test1 to test;
comment on table test is '測試表';
select * from user_tab_comments where TABLE_NAME='TEST';
comment on column TEST.NAME is '名稱' ;
select * from user_col_comments where TABLE_NAME='TEST' and column_name='NAME';

事務控制語言 TCL: commit,rollback,savepoint

     insert into test values(1,'a'); 
     savepoint A; 
     insert into test values(2,'b');
     savepoint B; 
     insert into test values(3,'c');
     savepoint C; 
     rollback to A; 
     commit; 
     
數據控制語言 DCL: grant,revoke
grant dba to scott;
revoke dba from scott;

2.常用函數
字符函數
select upper('bsoft') from dual;
select initcap('bsoft') from dual;
select initcap('bSOFT') from dual;
select concat('welcome ',concat('to ','bsoft')) from dual;
select substr('welcome to bsoft',12,7) from dual;
select instr('welcome to bsoft','bsoft',11) from dual;
select length('bsoft張振磊') from dual;
select lengthb('bsoft張振磊') from dual;
select lpad('張振磊',10,'1') from dual;
select rpad('bsoft',11,'bsoft') from dual;
select replace('bsoft張振磊','張','zhang') from dual;
select trim('b' from 'bbbbsoftbzzlbb') from dual;

數值函數
select round(45.5,0) from dual;
select round(155.1,-1) from dual;
select round(155.1,-2) from dual;
select trunc(155.1) from dual;
select trunc(155.23,-1) from dual;
select mod(11,3) from dual;

日期函數
select to_date('2016.08.11 10:10:10','yyyy-mm-dd hh24:mi:ss') from dual;
select sysdate + 10 from dual;
select sysdate,sysdate + 10/24 from dual;
select ename,hiredate,sysdate,(sysdate - hiredate)/365 from scott.emp;
select ename,hiredate,sysdate,months_between(sysdate,hiredate)/12 from emp;
select add_months(sysdate,1) from dual;
select last_day(sysdate) from dual;
select next_day(sysdate,7) from dual;
select round(sysdate,'month') from dual;
select trunc(sysdate+10,'month') from dual;
select round(sysdate,'year') from dual;
select trunc(sysdate,'year') from dual;

幾個有用的函數
select job,sal,decode(job,'CLERK',sal*1.1,'SALESMAN',sal*1.2,sal) from emp;
select job,sal,case job when 'CLERK' then sal*1.1 when 'SALESMAN' then sal*1.2 else sal end from emp;

select job,sal,case when job='CLERK' then sal*1.1 when job='SALESMAN' then sal*1.2 else sal end from emp;
select case when 1=1 then 2 when 2=2 then 3 end from dual;
select ename,job,sal,case when sal>=5000 then '高級' when sal >=3000 then '中級' else  '低級' end from emp;

select distinct job from emp;

select sys_context('userenv','ip_address') from dual;
select sys_context('userenv','sid') from dual;
select sys_context('userenv','terminal') from dual


select ascii('d') from dual;
select chr(100) from dual;


3.sql數據類型

字符型
char()固定字長 最大2000
varchar2()可變字長 最大4000
create table test(a char(3));
insert into test
select 1 from dual;
select length(a) from test;

create table test2(a varchar(3))
insert into test2
select 1 from dual;
select length(a) from test2;

alter user scott quota unlimited on users;



數值型
number(p,s)
int

create table test3(a number(4,2));
insert into test3
values(111.11);

create table test4(a int);
insert into test4
values(111.11);
commit;
select * from test4;

日期型
date
timestamp
timestamp with time zone
timestamp with local time zone

create table test5(a date,b timestamp,c timestamp with time zone,d timestamp with local time zone);
insert into test5
values(sysdate,sysdate,sysdate,sysdate);
select * from test5


數據類型的轉換
隱性類型轉換,顯性類型轉換

隱性類型轉換
select * from emp where empno ='7788';
select length(sysdate) from dual;
length(sysdate) =9?
select * from nls_session_parameters where parameter='NLS_DATE_FORMAT';
select * from nls_instance_parameters where parameter='NLS_DATE_FORMAT';
select * from nls_database_parameters where parameter='NLS_DATE_FORMAT';
千年蟲

select '12.5' + 11 from dual;
select 10 + '12.5' ||11 from dual;
select 10 + ('12.5' ||11) from dual

顯性類型轉換
to_char
to_date
to_number

select ename,to_char(hiredate,'DD-MON-YY') from emp;
select ename,to_char(hiredate,'yyyy-mm-dd'),to_char(hiredate,'fmyyyy-mm-dd') from emp;
select ename,sal,to_char(sal,'L999.99') from emp;
select to_date('2016.08.31','yyyy.mm.dd') from dual;
select to_number('$2016','$9999.99') from dual;


4.where 子句中常用的運算符

算數運算符
+-*/
邏輯運算符
not ,and ,or
比較運算符
單行比較=,>,>=,<=,<
多行比較符>any,>all,<any,<all,in,not in
模糊比較like(%,_)
特殊比較is null
()優先級最高
between and
select ename,sal from emp where sal between 3000 and 5000;

轉義
create table test6(a varchar(20));
insert into test6
values('AZZL');
insert into test6
values('A%ZZL');

select * from test6 where a like 'A%%'
select * from test6 where a like 'A\%%' escape '\'
'' 和 ""
''內表示字符
"" 別名 ,保持原樣
select a from test6
select "a" from test6
select "A" from test6

連續兩個'表示轉義
select empno||'''s name is ' ||ename from emp
in 檢驗一個值是否在一個列表中
select * from emp where empno in(7788,7900)

交互輸入變量符&
select * from emp where empno=&emp_no

使用邏輯操作符
select * from emp where sal >1000 and job ='CLERK';
select * from emp where sal >1000 or job ='CLERK';
select * from emp where job not in('CLERK');

5.分組函數

最重要的5個分組函數
sum(),avg(),count(),max(),min()

select sum(sal) sum,avg(sal) avg,max(sal) max,min(sal) min ,count(*) count from emp;
select job, sum(sal) sum,avg(sal) avg,max(sal) max,min(sal) min ,count(*) count from emp group by job;
select min(hiredate),max(hiredate) ,min(ename),max(ename)  from emp ;
select count(*),count(comm) from emp ;
select deptno,avg(comm) from emp group by deptno;
select deptno,avg(nvl(comm,0)) from emp group by deptno;


group by 創建組
一旦使用了group by ,select後面只能有兩種列:組函數和分組特性列
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;
select deptno,avg(sal) from emp where avg(sal)>2000 group by deptno;
select deptno,avg(sal) from emp where sal>2000 group by deptno;
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000 order by avg(sal);
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000 order by job;

select from where group by having order by

分組函數的嵌套
單行函數可以嵌套任意層,分組函數最多可以嵌套兩層
select sum(sal) from emp group by deptno;
select avg(sum(sal)) from emp group by deptno;
select count(avg(sum(sal))) from emp group by deptno;


6.數據限定與排序

where限定 from後面的表或視圖,限定的選項只能是表的列或列單行函數或列表達式,where後不可以直接使用分組函數
select job,ename,sal from emp where sal >2000;
select job,ename,sal from emp where length(sal)>3;
select job,ename,sal from emp where sal +comm >2000;

having 限定group by的結果,限定的選項必須是group by 後的聚合函數或分組列,不可以直接使用where後的限定選項
select deptno,sum(sal) from emp group by deptno having deptno=10;
select deptno,sum(sal) from emp group by deptno having sum(sal)>8000;

排序
order by 總是在一個select語句的最後邊
排序可以使用列名,列表達式,列函數,列別名,列位置編號,select的投影列可不包含排序列,除指定的列位置編號外

升序asc 降序 desc,有空值的列的排序,缺省(升序)時,null排在最後面
使用多個列排序,多列用逗號隔開,每列後面可以指定升降序
select ename,sal from emp order by sal;
select ename,sal salary from emp order by salary;
select ename,sal salary from emp order by 2;
select ename,sal,comm ,sal+comm from emp order by sal +comm;
select deptno,avg(sal) from emp group by deptno order by avg(sal) desc;
select ename,job,sal+comm from emp order by 3 desc;
select ename,job,sal+comm from emp order by 3 desc nulls last;
select ename,job,deptno from emp order by deptno asc,job desc;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章