Oracle 常用的十大 DDL 對象

  創建表

create table test3 (tid number,tname varchar2(20),hiredate date default sysdate);
create table emp20 as select * from emp where deptno=20;
create table empinfo as select e.empno,e.ename,e.sal,e.sal*12 annsal,d.dname
      from emp e, dept d where e.deptno=d.deptno

 

  修改表

alter table test3 add photo blob;
alter table test3 modify tname varchar2(40);
alter table test3 drop column photo;
alter table test3 rename column tname to username;
rename test3 to test5;

 

  刪除表

 

drop table test5;(沒有正真的刪除,只是放入了oracle的回收站中)
    show recyclebin(查看回收站)
    purge recyclebin;(清空回收站)
            (管理員沒有回收站)
    flashback table TESTSAVEPOINT to before drop;(閃回回收站中的表)

drop table TESTSAVEPOINT purge;

 


  表/列約束:

 

     check : 檢查約束 gender varchar2(2) check (gender in ('男','女')),
        create table student
            (
              sid number constraint student_pk primary key,  //主鍵(索引)
              sname varchar2(20) constraint student_name_notnull not null, //非空
              gender varchar2(2) constraint student_gender check (gender in ('男','女')), //檢查
              email varchar2(40) constraint student_email_unique unique   //唯一
                                 constraint student_email_notnull not null, //非空
              deptno number constraint student_fk references dept(deptno) on delete set null //外鍵(級聯置空)
            );  (on delete cascade :級聯刪除)

 

 

view(視圖) 虛表
  創建視圖(要有視圖權限):

 

        create view empinfoview
        as
        select e.empno,e.ename,e.sal,e.sal*12 annsal,d.dname
        from emp e, dept d
        where e.deptno=d.deptno;  //可以進行DML操作,但是不建議該操作.
        
        create or replace view empinfoview
        as
        select e.empno,e.ename,e.sal,e.sal*12 annsal,d.dname
        from emp e, dept d
        where e.deptno=d.deptno
        with read only;   //屏蔽了 DML操作.

 

 

  刪除視圖:

drop view empinfoview;

sequence(序列) :維護表的主鍵,但是回滾等操作可能會出現裂縫.
  創建序列:
 

 

        create sequence myseq; //創建序列
        select myseq.nextval from dual; //下移指針,返回序列值
        select myseq.currval from dual; //返回當前指針序列值.
        
        create table testseq (tid number,tname varchar2(20));
        insert into testseq values(myseq.nextval,'aaa'); //使用序列維護主鍵(類似自動增長列).

 

 

  刪除序列

drop sequence myseq;

index(索引) :類似目錄,增加查詢效率
  自動創建:
    當定義了  PRIMARY   KEY   和   UNIQUE   之後,自動在相應的列上創建唯一性索引.

  創建索引:

        create index myindex on emp(deptno); //B樹索引
        create bitmap index myindex on emp(deptno); //位圖索引索引(適合查詢)

  刪除索引:

drop index myindex;

 

synonym(同義詞) :表的別名 (爲了安全)
  創建同義詞:

        create synonym myemp for emp; //私有同義詞
        create public synonym myemp for emp; //公有同義詞

 

procedure(存儲過程)
  創建存儲過程
  (不帶參數的)

 

        create or replace procedure sayHelloWorld  //不帶參數
        as
          --說明部分
        begin
           dbms_output.put_line('Hello World');

        end;

  (帶參數) 

 

        --給指定的員工漲100,(帶參數)    
        create or replace procedure raisesalary(eno in number) //帶一個輸入參數參數的
        as
           --定義變量保存漲前的薪水
           psal emp.sal%type;
        begin
           --得到漲前的薪水
           select sal into psal from emp where empno=eno;

           --漲100
           update emp set sal=sal+100 where empno=eno;

            --要不要commit(不要,調用者提交回滾)

           dbms_output.put_line('漲前:'||psal||'   漲後:'||(psal+100));

        end;

 

  帶有out參數的存儲過程(可以有返回值(OUT))

 

        create or replace procedure queryempinfo(eno in number,
                                            pename out varchar2,
                                            psal   out number,
                                            pjob   out varchar2)
        as
        begin
          select ename,sal,empjob into pename,psal,pjob from emp where empno=eno;
        end;


  調用存儲過程(sql中):

 

 

        1. exec sayHelloWorld();
        2. begin
             sayHelloWorld();
             sayHelloWorld();
           end;
           

 

 

function(存儲函數)
  創建一個存儲函數

 

        create or replace function queryempincome(eno in number)
        return number
        as
           --定義變量保存月薪和獎金
           psal emp.sal%type;
           pcomm emp.comm%type;
        begin
           select sal,comm into psal,pcomm from emp where empno=eno;

           --返回年收入
           return psal*12+nvl(pcomm,0);
        end;

 

 //原則,只有一個返回值使用存儲函數,否則使用存儲過程.


package(程序包)
  包頭

 

        CREATE OR REPLACE PACKAGE MYPAKCAGE AS 

          type empcursor is ref cursor;
          procedure queryEmpList(dno in number, empList out empcursor);

        END MYPAKCAGE;

 

 

  包體

 

        CREATE OR REPLACE PACKAGE BODY MYPAKCAGE AS

          procedure queryEmpList(dno in number, empList out empcursor) AS
          BEGIN

            open empList for select * from emp where deptno=dno;

          END queryEmpList;

        END MYPAKCAGE;

 

 


trigger(觸發器): 類似對insert,update,delete的監聽器
  創建觸發器:
    語句級觸發器(每條語句操作一次,無論改變多少行)

 

        create trigger abcd       
        after/before  insert/delete/update[of 列名]
        on emp
        declare
        begin
           dbms_output.put_line('成功操作了表格');
        end;

 

 

    /*
    觸發器應用一:實施複雜的安全性檢查
    禁止在非工作時間插入新員工

    週末:to_char(sysdate,'day') in ('星期六','星期日')
    上班前 下班後:to_number(to_char(sysdate,'hh24')) not between 9 and 17
    */

 

        create or replace trigger securityemp
        before insert
        on emp
        begin
          if to_char(sysdate,'day') in ('星期六','星期日','星期三') or
             to_number(to_char(sysdate,'hh24')) not between 9 and 17 then
             --禁止insert
             raise_application_error(-20001,'禁止在非工作時間插入新員工');     
          end if;

        end;

 

 

   行級觸發器(每改變一行操作一次) 

 

        create or replace trigger checksalary
        before update
        on emp
        for each row              //有此行就是行級觸發器
        begin
          --if 漲後的薪水  < 漲前的薪水 then
          if :new.sal < :old.sal then
            raise_application_error(-20002,'漲後的工資不能少於漲前的工資。
                                    漲前:'||:old.sal||'  漲後:'||:new.sal);
          end if;
        end;

 

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