posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          oracle sql語句精講

          Posted on 2007-08-23 09:29 semovy 閱讀(469) 評(píng)論(0)  編輯  收藏 所屬分類: Oracle數(shù)據(jù)庫方面
          表 :建立表主要指定義下列信息:
               列定義
               完整性約束
               表所在表空間
               存儲(chǔ)特性
               可選擇的聚集
               從一查詢獲得數(shù)據(jù)
          語法如下:
          CREATE TABLE tablename (column1 datatype [DEFAULT expression] [constraint], column1 datatype [DEFAULT expression] [constraint], ……)
               例如:
               SQL> create table serv (serv_id number(10),serv_seq_nbr number(3))
          2      tablespace data_bill;
          視圖
          視圖是一個(gè)邏輯表,它允許操作者從其它表或視圖存取數(shù)據(jù),視圖本身不包含數(shù)據(jù)。視圖所基于的表稱為基表。
          引入視圖有下列作用:
               提供附加的表安全級(jí),限制存取基表的行或/和列集合。
               隱藏?cái)?shù)據(jù)復(fù)雜性。
               為數(shù)據(jù)提供另一種觀點(diǎn)。
               促使ORACLE的某些操作在包含視圖的數(shù)據(jù)庫上執(zhí)行,而不在另一個(gè)數(shù)據(jù)庫上執(zhí)行。
          2.1分組視圖
          [例1]簡單的分組視圖
          SQL> create or replace view dept_tot as
          2 select a.dname dept,sum(b.sal) total_sal from scott.dept a,scott.emp b
          3 where a.deptno=b.deptno group by a.dname;

          查看已建立。
          SQL> select * from dept_tot;

          DEPT                   TOTAL_SAL
          --------------               ---------
          ACCOUNTING       8750
          RESEARCH         10875
          SALES                 9400
          [例2]合計(jì)視圖
          [例]合計(jì)函數(shù)視圖實(shí)例
          SQL> create or replace view emp_no1 as
          2 select deptno,sum(sal) 工資和,sum(comm) 總和
          3 from scott.emp group by deptno;
          SQL> select * from emp_no1;
          DEPTNO   工資和     總和
          --------- --------- ---------
              10     8750
              20   10875
          30     9400     2200
          索引
          索引是種數(shù)據(jù)庫對(duì)象。對(duì)于在表或聚集的索引列上的每一值將包含一項(xiàng),為行提供直接的快速存取。在下列情況ORACLE可利用索引改進(jìn)性能:
               按指定的索引列的值查找行。
               按索引列的順序存取表。
          語法:
          create index <index_name> on <table_name(column1,column2…)>
          [storage語句] [其它語句];
          例如:
          SQL> create index idx_serv_01 on serv(srev_id,serv_seq_nbr)
          2   tablespace data_bill_idx
          3   storage (initial 5m next 5m);
          存儲(chǔ)函數(shù)
          語法:
          create [or replace] function 函數(shù)名(參數(shù)1,參數(shù)2……) RETURN 類型 IS | AS
               [局部變量說明]
               BEGIN
                     執(zhí)行語句;
               END;
          [例子]:
          建立oracle 的函數(shù)
          create or replace FUNCTION SumDeptSalary(dept_no in number)
          return number is
              total number(11,2);
          begin
          select sum(sal) into total from emp where deptno=dept_no;
          return(total);
          end;
          存儲(chǔ)過程
          語法:
          create [or replace] procedure 函數(shù)名(參數(shù)1,參數(shù)2……) IS | AS
               [局部變量說明]
               BEGIN
                     執(zhí)行語句;
               END;
          [例子]
          過程的輸入、輸出參數(shù)的用例
          create or replace procedure RaiseSalary(RaiseRate in number,empnum in number,
          outname out varchar2,outsal out number) is
          begin
          update emp set sal=sal*(1+RaiseRate);
          select ename into outname from emp where empno=empnum;
          select sal into outsal from emp where empno=empnum;
          end;
          函數(shù)pl/sql 中的過程參數(shù)調(diào)用示例
          set serveroutput on; //顯示變量值的屬性
          declare name varchar2(10);sal number;
          begin
          RaiseSalary(0.05,7566,name,sal);
          dbms_output.put_line(name);
          dbms_output.put_line(sal);
          end;:
          查找系統(tǒng)時(shí)間

          select sysdate from dual;
          系統(tǒng)日期的格式轉(zhuǎn)換
          select to_char(sysdate,'MM/DD/YYYY HH:MM:SS AM') from dual;
          分解日期
          年 select to_char(sysdate,'year') from dual;
          月 select to_char(sysdate,'mon') from dual;
          月中第幾日 select to_char(sysdate,'dd') from dual;
          一年中的第幾星期 select to_char(sysdate,'ww') from dual;
          季度 select to_char(sysdate,'q') from dual;
          一年中的第幾天 select to_char(sysdate,'ddd') from dual;
          查詢時(shí)間
          testdate1 date
          select * from testdate where to_char(trunc(testdate1),'yyyy/mm/dd')='2001/04/09'
          oracle 中使用trunc()函數(shù)把所有日期的時(shí)間值設(shè)置為12:00AM,消除sysdata中的時(shí)間
          trunc()函數(shù)可進(jìn)行時(shí)間加減
          在select中日期相減得出天數(shù)
          select trunc(sysdate)-trunc(hiredate) from emp where trunc(sysdate)-trunc(hiredate)>5000;
          case 語句使用decode()函數(shù)替代

          例如:select decode(deptno,10,'十',20,'二十',30,'三十') as chinesecode from emp;
                      列名   值 表示值
          decode列乘積的應(yīng)用
          decode(Yjtx.Lb,0,Yprckmx.Ypsl*Xmxx.Xmpzlbl,1,Yprckmx.Ypsl*Xmxx.Xmpzlbl,2,Yprckmx.Ypsl) as Ypsl
          使用decode函數(shù)計(jì)算
          select sum(decode(deptno,20,1))-sum(decode(deptno,30,1)) as d from emp;

          偽列
          偽列不是表中真正的列,只是特征與列相同
          ---currval和nextval偽列
          currval和nextval偽列與序列聯(lián)合使用。currval偽列返回被引用的序列的當(dāng)前值;
          nextval偽列增加序列的值,并返回序列的新值。只能用于select values 子句和set 子句
          表中主鍵列增加流水值
          insert into employee
          values(emp_id_seq.nextval,'stanton bernard');
          emp_id_seq為序列
          ---rownum偽列
          rownum偽列指出從表中檢索數(shù)據(jù)的次序。例如,值為1的rownum表明該記錄是從表中檢索的第一條記錄。
          rownum偽列最常見的用途是用于where子句。例如
          select * from emp where rownum <10;
          建立bh流水序列值
          create sequence seqczyqx
          start with 1
          increment by 1
          nocycle;
          生成表中沒有的列的方法
          select 'testchar' as dd,sysdate deptno from emp,dual;
              字符值   列名
          select 2342345234 as dd,sysdate deptno from emp,dual;
              數(shù)值     列名
          ________________________________________________________________________
          返回字符串的一部分
          select substr(ename,1,2) from emp;
          -----------------------------------------------------------------------------------集合操作
          select * from emp where deptno in(10,20);
          -----------------------------------------------------------------------------------
          在表中插入常量
          insert into testdate select 1,sysdate,sysdate from dual;
          -----------------------------------------------------------------------------------
          在select中對(duì)多個(gè)變量賦值
          SELECT SFBL ,DWBL into XMBL,DWBL FROM HZBL WHERE CFLBBM=CFLBBM AND HZLBBM=HZLBBM and Degree=Hzsf;
          -----------------------------------------------------------------------------------
          比較操作中的空問題
          is null;is not null
          使用nvl()函數(shù)
          select count(*),nvl(comm_amt,0) from comm;
          nvl函數(shù)用'0'值置換comm_amt表列中值為空的所有數(shù)據(jù)行
          ------------------------------------------------------------------------------
          ·      從另一張表改進(jìn)得到
          Sql語句:
          Create Table <新表> As
            Select <列的列表> From <舊表>
            Where <約束條件>[可選]
          ·      拷貝表結(jié)構(gòu)
            Create Table <表名> As
            Select <列的列表>From <舊表>
            Where 1=2
          主站蜘蛛池模板: 扎鲁特旗| 柏乡县| 阿尔山市| 郸城县| 海淀区| 谷城县| 美姑县| 宁蒗| 桐柏县| 循化| 婺源县| 巢湖市| 响水县| 淳化县| 东兰县| 和龙市| 石门县| 绥中县| 府谷县| 如东县| 嘉峪关市| 广州市| 姜堰市| 盐源县| 北京市| 苍南县| 鹤壁市| 建水县| 海原县| 灵寿县| 扬中市| 如东县| 龙江县| 晋江市| 阿拉善右旗| 沙田区| 蓝山县| 广德县| 邯郸市| 禄丰县| 光泽县|