數據庫中的視圖、序列及索引

一、視圖

  語法:create view 視圖名稱 as 子查詢

  例:創建一視圖,包含全部的20部門的僱員信息(僱員編號,姓名,工作,僱傭日期)

   create view emp20 as select empno,ename,job,hiredate from emp wheredeptno = 20;

  刪除視圖:drop view 視圖名稱

  替換視圖:create or replact 視圖名稱 as 子查詢

    更換視圖時不需要先執行刪除操作,系統會爲用戶自動刪除及重建

    create or replact view emp20 as select * fromemp where deptno = 20;

  查詢部門名稱、部門人數、平均工資以及最低工資的僱員姓名

  1selectd.deptno,d.dname,ed.c,ed.a,e.ename from emp e,(select deptno,count(empno) c,avg

(sal) a,min(sal) m from emp e group bydeptno) ed,dept d where e.deptno = d.deptno and

e.deptno = ed.deptno and e.sal = ed.m;

  2、先建立一張視圖,然後再做查詢

   create view ed as select deptno,count(empno) c,avg(sal) a,min(sal) mfrom emp e group by

deptno;

   select d.deptno,d.dname,ed.c,ed.a,e.ename from emp e,ed,dept d wheree.deptno = d.deptno

and e.deptno = ed.deptno and e.sal = ed.m;

 

 with check option  ---> 創建視圖後不能再進行更新操作

  :create or replace view emp20 asselect * from emp where deptno =20 with check option;

    update emp20 set deptno = 30 where empno = 7788;  //系統報錯,提示:視圖  with check

option where  子句違規

    update emp20 set ename = '斯科特' where empno = 7788;  //更新成功

     總結:with約束後,再更新視圖時,除了創建條件不能更新,其他的字段均可更新。即取決於where

後面的條件

 with read only  ---> 創建的視圖只讀,即只能讀取操作

  例:create or replace view emp20 asselect * from emp where deptno = 20 with read only;

     update emp20 set ename = '斯科特' where empno = 7788;  //提示錯誤:無法對只讀視圖進行

DML 操作

二、序列

  語法:create sequence 序列名

       [increment by n] 每次增長的長度

       [start with n] 從第幾個序列開始

       [{maxvalue n | nomaxvalue}] 最大值

       [{minvalue n | nominvalue}] 最小值

       [cycle | nocycle] 增長到最大值後,回到最小值,重新開始增加

       [{cachen | nocache}]斷開後重新連接,從n開始繼續增長

  序列創建完成之後,所有的自動增長由用戶自己處理,序列的兩種操作:

 nextVal ---> 取得序列的下一個內容

 currVal ---> 取得序列的當前內容

  例:創建一個序列

    create sequence myseq;

     建立一張驗證序列的表

    create table testseq(next number,curr number);

     向表中增加數據

    insert into testseq(next,curr)values(myseq.nextval,myseq.currval);

     刪除序列:drop sequence 序列名

三、索引

  將某張表中的某一列放入一張索引表中,通過查找索引表就能得到對應的數據

  語法:create index 索引表名

 

  Oracle的索引表

  1B樹索引(二叉樹的形式)默認

  2、位圖索引

  如果一張表中有主鍵,那麼會自動創建索引或手動創建:

 create index myindex on emp(deptno);---創建索引表

 create index myindex on emp(deptno,job);----創建兩張索引表


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