Oracle小結1

Oracle常用命令

一、打開Oracle數據庫方式

a)        Dos sqlplus

快捷方式:Windows徽標鍵+ R 快速打開運行窗口,鍵入CMD

打開Dos 然後按照提示輸入用戶名密碼

b)        圖形化工具:開始-說有程序-oracle程序-應用程序開發-sqlplus

c)        瀏覽器方式打開:

     http://127.0.0.1:5560/isqlplus

        http://localhost:5560/isqlpus

     注:oracle11g開始就不支持isqlplus

二、Oracle客戶端

a)        PL/SQL

b)        Toad

三、SQL語句

1、更改用戶

    alter user scott account unlock;

       解鎖scott用戶

 

2、顯示數據庫表信息

    1.desc 表名

3select語句

1)        查詢條件若在雙引號中則區分大小寫

若查詢條件中含有“號 則可用\” 轉譯

2)        select 3*3 from dualdual是系統自帶的一張空表,計算數據時可以使用該表

3)        任何含有空值的算術表達式的計算結果是空值

4)        set linesize 200;--用於設定每行顯示的寬度

5)        set pagesize 30;--設置顯示的頁數

 

4distinct

    1.select distinct a from ;

    2.select distinct a,b from ;

       去除查詢結果中相同內容

5where

a)        select * from emp where empno = 10;

b)        select * from emp where empno <> 10;

c)        select * from emp where ename = 'hebe';

d)        select * from emp where sal (not) between 800 and 1500;

e)        select * from emp where comm is (not) null;

f)         select * from emp where ename (not) in ('smith','philip','jay');

g)        select * from emp where ename like '_A%';

h)        select * from emp where ename like '_\%a%';系統默認轉義符是\,可以自己指定轉義符

select * from emp where ename like '_$%a%' escape '$';

以上說明$爲用戶自定義的轉義符

也可以用兩個 代表的轉譯

 

6order by 語句

    1.select * from dept;

    2.select * from dept order by dept desc;(默認爲asc)

            注意: desc爲降序 asc 爲升序

3.select ename, sal, deptno from emp where sal > 2000 order by deptno asc,ename   desc;

 

7sql函數 

1.select ename from emp where ename not like '_A%' and sal > 1500

    order by sal desc;

    2.select lower(ename) from emp;

    3.select ename from emp where lower(ename) like '_a%';

 4.select substr(ename,2,3) from emp;從第二字符截,一共截三個字符。

    5.select chr(65) from dual;結果爲A  ASCII碼轉成字符

    6.select ascii('a') from dual;結果爲65 的到字符的ASCII

    7.select round(35.572) from dual;結果爲36

    8.select round(35.572,2) from dual;結果爲35.57

    9.select round(35.572,-1) from dual;結果爲40

910 兩例 解析:以 . 爲起點左爲負右爲正,得到取近似值的位數 然後進行四捨五入計算

    10.select to_char(sal,'$99,999.9999') from emp;

to_char函數主要用於對日期和數字進行格式化

11.select to_char(sal,'L99,999.9999') from emp;人民幣符號,L代表本地符號。

    12.select birthdate from emp;

    顯示爲:BIRTHDATE

    ---------------------

    22-3-87

    改爲:select to_char(birthdate,'YYYY-MM-DD HH24:MI:SS') from emp;

    13.to_data函數

select ename,birthdate from emp where birthdate > to_date('1987-3-22 11:22:33','YYYY-MM-DD HH24:MI:SS');不能直接寫birthdate>'1987-2-22 11:22:33'會出現格式不匹配,因爲表中格式爲DD-MM月-YY,

    14.select sal from emp where sal > to_number('$12,444.99','$99,999.99');

15.select ename, sal*12+nvl(comm,0) from 這樣防止comm爲空時,sal*12相加也爲空的情況

                               如果你某個字段爲空,但是你想讓這個字段顯示0

nvl(字段名,0),就是當你選出來的時候,這個字段雖然爲空,但是顯示的是0,當然這個0也可以換成其他東西,如:12

8Group function 組函數

    1.select max(sal) from emp;

    2.select min(sal) from emp;

    3.select to_char(avg(sal), '$999,999,999.99') from emp;

    4.select round(sum(sal),2) from emp;

    5.select count(*) from emp where sal > 1500;

    6.select count(comm) from emp;

    7.select count(distinct deptno) from emp;

 

9Group by語句

    1.select avg(sal) from emp group by deptno;

    2.select deptno,avg(sal) from emp group by deptno;

    3.select deptno,job,max(sal) from emp group by deptno,job;

 4.求薪水值最高的人的名稱select ename,max(sal) from emp;出錯,因爲max只能有一個值,但是ename的值可能有好幾個,不能匹配。

    Group by 語句應注意,出現在select中的字段,如果沒有出現在組函數中,必須出現在Group  by語句中。

 

10Having對分組結果篩選

    1.where是對單條記錄進行篩選,Having是對分組結果進行篩選

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

2.查詢工資大於2000的僱員,按照部門編號進行分組,分組後平均薪水大於1500,按工資倒序排列 ,且顯示平均工資

select deptno,avg(sal) from emp where sal > 2000 group by deptno having avg(sal) >  1500 order by avg(sal) desc;

 

11、子查詢

    1.select 語句中嵌套select 語句,求哪些人工資在平均工資之上.

    select ename,sal from emp where sal > (select avg(sal) from emp);

    2.查找每個部門掙錢最多的那個人的名字.

 select ename, deptno from emp where sal in (select max(sal) from emp group by deptno) 查詢會多值.正確寫法是:

    應把select max(sal),deptno from emp group by deptno當成一個表,語句如下:

    select ename,sal from emp join (select max(sal) max_sal,deptno from emp group by         deptno) t on (emp.sal = t.max_sal and emp.deptno = t.deptno);

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