oracle上課 學習2

來源:互聯網
上載者:User

標籤:員工   封裝   tput   oop   commit   bms   amp   簡單   檔案   

1.1.  訓練描述

使用遊標,列印emp中20號部門的所有員工的資訊

操作步驟答案

declare

  cursor c_emp  is select * from emp where deptno=10;

  v_row emp%rowtype;

 begin

   open c_emp;

   loop

     fetch c_emp into v_row;

     exit when c_emp%notfound;

     dbms_output.put_line(v_row.empno||‘:‘||v_row.ename);

   end loop;

 close c_emp;   

 end;

1.2.  訓練描述

彈出框中輸入一個年齡資料,如果數值大於150,拋自訂的異常,顯示一句話“此人年紀太大”

操作步驟答案

declare

v_age number:=&nums;

v_exception exception;  --自訂異常 v_exception

begin

  if v_age>150 then

    --拋異常

    raise v_exception;

  end if;

  

  exception

    when v_exception then

      dbms_output.put_line(‘此人年紀太大‘);

end;

 1.3.  訓練描述

建立一個帶out參數的預存程序 ,計算指定員工的年薪,並且用7788號員工測試

操作步驟
  1. 建立預存程序pro_yearsal
  2. 使用預存程序。

 

操作步驟答案
  1. 建立預存程序pro_yearsal

create or replace procedure pro_emp_sal(v_no  number,v_year_sal out number)

is

begin

   select  sal*12+ nvl(comm,0) into v_year_sal from emp where empno=v_no;

end;

  1. 使用預存程序。

declare

v_sal number(8,2);

begin

  pro_emp_sal(7369,v_sal);

  dbms_output.put_line(v_sal);

end;

   簡述一下資料表空間、使用者、表之間的關係?

 

 一個資料表空間相當於mysql的一個資料庫的概念,oracle是分配資料表空間,而mysql是建立資料庫,表的資料,是由使用者放入某一個資料表空間的某個表裡,而這個資料表空間會隨機把這些表資料放到一個或者多個資料檔案中。

由於 oracle 的資料庫不是普通的概念,oracle 是由使用者和資料表空間對資料進行管理和存放的。但是表不是由資料表空間去查詢的,而是由使用者去查的。因為不同使用者可以在同一個資料表空間建立同一個名字的表,而是通過不同的使用者來進行區分。

一個oracle資料庫系統可以建立多個資料庫,每個資料庫稱為執行個體,在執行個體裡可以分配多個資料表空間,每個資料表空間裡可以建立多個資料表,而每個資料表空間裡的表資料都儲存在一個資料檔案裡,要查詢資料表空間裡的表資料就需要在本執行個體下給資料表空間指定個使用者,由使用者來訪問資料表空間裡的表。

 

 

 

 

 

4.  視圖

 

4.1. 簡述什麼是視圖

 

  視圖是一個虛擬表,不存放資料,資料來源是原始的表

 

視圖可以使複雜sql查詢簡單化,每一個視圖封裝了一條條複雜的SQL語句

 

4.2. 建立視圖

 

   create view 視圖名稱 as select 語句

 

  例如:create view view_emp20 as select empno,ename,job,mgr,hiredate from emp where deptno=20;

 

4.3. 使用視圖

 

select * from 視圖名

 

不可以修改視圖裡的表資料,因為它不是個真實的實體表,如果改了資料會亂資料,導致不一致,我們建立視圖時指定為指讀視圖

 

create view view_emp20 as select empno,ename,job,mgr,hiredate from emp where deptno=20 with read only;

 

5.  序列

 

5.1. 簡述什麼序列

 

   在許多的資料表之中都存在一種稱為自動成長列的操作,但是在Oracle之中,沒有這種自動成長列,是需要使用者手工的控制,這樣做主要是為了開發方便,序列是oracle資料庫提供的用於實現id自增長功能。

 

5.2. 建立一個序列

 

   文法:

 

CREATE SEQUENCE sequence

 

[INCREMENT BY n]   --每次增長n

 

[START WITH n]     --初始值,從n開始

 

例如:create sequence person_sqc;

 

序列建立完成後,它的自動成長需要使用者手動觸發

 

nextval:取得序列的下一個內容

 

currval:取得序列的當前值

 

 

 

5.3.序列的使用

 

      Insert into person  values(person_sqc.nextval,  “zhangsan”,1.null,”修正”)

 

6.  索引

 

6.1. 簡述索引

 

索引是用於加速資料存取的資料對象,索引可以看做一本書的目錄,通過目錄可以很快的找到需要的內容。資料庫裡的索引就是為了提升查詢速度(資料量大的情況下)。

 

6.2.建立單行索引

 

create index 索引名稱 on 表名(列)

 

6.3. 建立複雜索引

 

create index 索引名稱 on 表名(列,列)

 

6.4. 索引的測試

 

   在員工表中插入500萬的資料,

 

declare

 

 

 

begin

 

  for i in 1..5000000 loop

 

    insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)

 

    values (emp_seq.nextval, emp_seq.currval, ‘CLERK‘, 7902, sysdate,

 

dbms_random.value(800,3000) , null, 10);

 

  end loop;

 

  commit;

 

end;

 

查詢工資等於1500的所有員工資訊

 

沒有索引的情況下我們查詢:

 

select * from emp where sal=1500;

 

在查詢條件工資欄位建立索引

 

create index emp_sal_index on emp(sal);

 

在有索引的情況下進行查詢:

 

Select * from emp where sal = 1500;

 

oracle上課 學習2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.