Oracle操作2

一、視圖

視圖的概念:視圖就是提供一個查詢的窗口,所有的數據來自原表。

0、將emp表創建到當前用戶中,方便後面的操作

---查詢語句創建表
create table emp as select * from scott.emp;
select * from emp;

1、創建視圖【必須有dba權限】

create view v_emp as select ename, job from emp;

2、查詢視圖

select * from v_emp;

3、修改視圖【不推薦修改視圖,大部分公司創建視圖後不會去修改它】

update v_emp set job='CLERK' where ename='ALLEN';
commit;

4、創建只讀視圖

create view v_emp1 as select ename, job from emp with read only;

5、視圖的作用

---第一:視圖可以屏蔽掉一些敏感字段。
---第二:保證總部和分部數據及時統一。

二、索引

索引的概念:索引就是在表的列上構建二叉樹

1、創建單列索引

create index idx_ename on emp(ename)
---單列索引觸發規則,條件必須是索引列中的原始值。
---單行函數,模糊查詢,都會影響索引的觸發
select * from emp where ename='SCOTT'

2、創建複合索引

create index idx_enamejob on emp(ename, job);
---複合索引中第一列爲優先檢索列
---如果要觸發複合索引,必須包含有優先檢索列的原始值。
select * from emp where ename='SCOTT' and job='xx';---觸發複合索引
select * from emp where ename='SCOTT' or job='xx';---不觸發索引
select * from emp where ename='SCOTT';---觸發的是單列索引。

三、pl/sql編程語言

0、plsql的概念

---pl/sql編程語言是對sql語言的擴展,使得sql語言具有過程化編程的特性。
---pl/sql編程語言比一般的過程化編程語言,更加靈活高效。
---pl/sql編程語言主要用來編寫存儲過程和存儲函數等。

1、聲明方法

---賦值操作可以使用:=也可以使用into查詢語句賦值
---declare與begin之間是定義變量,begin和end之間寫一些編程邏輯結構
declare
   i number(2) := 10; --類似 int i = 10
   s varchar2(10) := '小明';
   ena emp.ename%type; --引用型變量  定義ena類型是emp表ename字段相同類型
   emprow emp%rowtype;--記錄型變量 emprow爲emp表中一行的數據類型
begin
  dbms_output.put_line(i); --類似 System.out.println(i)
  dbms_output.put_line(s);
  select ename into ena from emp where empno = 7788;--將查詢語句賦給ena
  dbms_output.put_line(ena);
  select * into emprow from emp where empno = 7788;
  dbms_output.put_line(emprow.ename || '的工作爲:' || emprow.job);-- ||類似+
end;

2、pl/sql中的if判斷

---輸入小於18的數字,輸出未成年
---輸入大於18小於40的數字,輸出中年人
---輸入大於40的數字,輸出老年人
declare
  i number(3) := ⅈ--&加變量名 代表輸入一個值
begin
  if i<18 then
     dbms_output.put_line('未成年');
  elsif i<40 then
    dbms_output.put_line('中年人');
  else
    dbms_output.put_line('老年人');
  end if;
end;

3、pl/sql中的loop循環【用三種方式輸出數字1到10】

---while循環
declare
  i number(2) := 1;
begin
  while i<11 loop
    dbms_output.put_line(i);
    i := i+1;
  end loop;
end;
---exit循環【用得多】
declare
  i number(2) := 1;
begin
  loop
    exit when i>10;
    dbms_output.put_line(i);
    i := i+1;
  end loop;
end;
---for循環
declare
 
begin
  for i in 1..10 loop
    dbms_output.put_line(i);
  end loop;
end;

4、遊標【可以存放多個對象,多行記錄】

---輸出emp表中所有員工的姓名
declare
   cursor c1 is select * from emp;
   emprow emp%rowtype;
begin
   open c1;
      loop
         fetch c1 into emprow;
         exit when c1%notfound;
         dbms_output.put_line(emprow.ename);
      end loop;
   close c1;
end;
----給指定部門員工漲工資
declare
   cursor c2(eno emp.deptno%type) 
                 is select empno from emp where deptno = eno;
                 en emp.empno%type;
begin
   open c2(10);
      loop
        fetch c2 into en;
        exit when c2%notfound;
        update emp set sal=sal+100 where empno=en;
        commit; 
      end loop;
   close c2;
end;
----查詢10號部門員工信息
select * from emp where deptno = 10;

四、存儲過程和存儲函數

概念:存儲過程就是提前已經編譯好的一段pl/sql語言,放置在數據庫端。

       可以直接被調用。這一段pl/sql一般都是固定步驟的業務。

1、存儲過程

----給指定員工漲100塊錢
create or replace procedure p1(eno emp.empno%type)
is
 
begin
  update emp set sal = sal+100 where empno = eno;
  commit;
end;
 
 
select * from emp where empno = 7788;
----測試p1
declare 
 
begin
  p1(7788);
end;

2、存儲函數

----通過存儲函數實現計算指定員工的年薪
create or replace function f_yearsal(eno emp.empno%type) return number
is 
  s number(10);
begin
  select sal*12+nvl(comm, 0) into s from emp where empno=eno;
  return s;
end;
 
----測試f_yearsal
----存儲函數在調用的時候,返回值需要接受
declare
  s number(10);
begin
  s := f_yearsal(7788);
  dbms_output.put_line(s);
end;

3、out類型參數如何使用【存儲過程】

---使用存儲過程來算年薪
create or replace procedure p_yearsal(eno emp.empno%type, yearsal out number)
is
  s number(10);
  c emp.comm%type;
begin
  select sal*12, nvl(comm, 0) into s, c from emp where empno = eno;
  yearsal := s+c;
end;
 
---測試p_yearsal
declare
  yearsal number(10);
begin
  p_yearsal(7788, yearsal);
  dbms_output.put_line(yearsal);
end;

4、into和out類型參數的區別是什麼

---凡是涉及到into查詢語句賦值或者:=賦值操作的參數,都必須使用out來修飾

5、存儲過程和存儲函數的區別

---語法區別:關鍵字不一樣,
------------存儲函數比存儲過程多了兩個return。
---本質區別:存儲函數有返回值,而存儲過程沒有返回值。
------------如果存儲過程想實現有返回值的業務,我們就必須使用out類型的參數
------------即使是存儲過程使用了out類型的參數,其本質也不是真的有了返回值
------------而是在存儲過程內部給out類型參數賦值,在執行完畢後,我們直接拿到輸出類型參數的值
 
----我們可以使用存儲函數有返回值的特性來自定義函數。
----而存儲過程不能用來自定義函數。
----案例需求:查詢出員工姓名,員工所在部門名稱。
 
----使用傳統方式來實現需求
select e.ename, d.dname
from emp e, dept d
where e.deptno = d.deptno
----使用存儲函數來實現提供一個部門編號,輸出一個部門名稱
create or replace function fdna(dno dept.deptno%type) return dept.dname%type
is
  dna dept.dname%type;
begin
  select dname into dna from dept where deptno = dno;
  return dna;
end;
---使用fdna存儲函數來實現案例需求:查詢出員工姓名,員工所在部門名稱。
select e.ename, fdna(e.deptno)
from emp e;

五、觸發器

概念:觸發器,就是制定一個規則,在我們做增刪改操作時,只要滿足該規則,自動觸發,無需調用。

1、語句級觸發器:

【不包含for each row的觸發器。在指定的操作語句操作之前或之後執行一次,不管這條語句影響了多少行 。】

----插入一條記錄,輸出一個新員工入職
create or replace trigger t1
after
insert
on person
declare
 
begin
  dbms_output.put_line('一個新員工入職');
end;
---觸發t1
insert into person values('王五', 19, '男');
commit;
select * from person;

2、行級觸發器:

【包含for each row的就是行級觸發器。觸發語句作用的每一條記錄都被觸發。】

【加for each row是爲了使用:old或者:new對象或者一行記錄。】

---不能給員工降薪
---raise_application_error(-20001~-20999之間, '錯誤提示地信息');
create or replace trigger t2
before
update
on emp
for each row
declare
 
begin
  if :old.sal>:new.sal then
     raise_application_error(-20001, '不能給員工降薪');
  end if;
end;
 
----觸發t2
update emp set sal=sal-1 where empno = 7788;
commit;

3、觸發器實現主鍵自增。【行級觸發器】

---分析:在用戶插入操作之前,拿到即將插入的數據,
------給該數據中的主鍵列賦值。
create or replace trigger auid
before
insert
on person
for each row
declare
 
begin
  select s_person.nextval into :new.pid from dual;
end;
--查詢person表數據
select * from person;
---使用auid實現主鍵自增
insert into person (pname) values('a');
commit;

六、java調用oracle

	/**
    * java調用oracle普通查詢
    * @throws Exception
    */
   @Test
   public void javaCallOracle() throws Exception {
       //加載數據庫驅動
       Class.forName("oracle.jdbc.driver.OracleDriver");
       //得到Connection連接
       Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.154.128:1521:orcl",
               "itheima", "itheima");
       //得到預編譯的Statement對象
       PreparedStatement pstm = connection.prepareStatement("select * from emp where empno = ?");
       //給參數賦值
       pstm.setObject(1, 7788);
       //執行數據庫查詢操作
       ResultSet rs = pstm.executeQuery();
       //輸出結果
       while(rs.next()) {
           System.out.println(rs.getString("ename"));
       }
       //釋放資源
       rs.close();
       pstm.close();
       connection.close();
   }

   /**
    * java調用存儲過程
    * @throws Exception
    */
   @Test
   public void javaCallProcedure() throws Exception {
       //加載數據庫驅動
       Class.forName("oracle.jdbc.driver.OracleDriver");
       //得到Connection連接
       Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1187.129:1521:orcl",
               "crazer", "crazer");
       //得到預編譯的Statement對象
       CallableStatement pstm = connection.prepareCall("{call p_yearsal(?, ?)}");

       //給參數賦值
       pstm.setObject(1, 7788);
       pstm.registerOutParameter(2, OracleTypes.NUMBER);
       //執行數據庫查詢操作
       pstm.execute();
       //輸出結果[第二個參數]
       System.out.println(pstm.getObject(2));
       //釋放資源
       pstm.close();
       connection.close();
   }

   /**
    * java調用存儲函數
    * @throws Exception
    */
   @Test
   public void javaCallFunction() throws Exception {
       //加載數據庫驅動
       Class.forName("oracle.jdbc.driver.OracleDriver");
       //得到Connection連接
       Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.187.129:1521:orcl",
               "crazer", "crazer");
       //得到預編譯的Statement對象
       CallableStatement pstm = connection.prepareCall("{?=call f_f1(?)}");

       //給參數賦值
       pstm.setObject(2, 20);
       pstm.registerOutParameter(1, OracleTypes.VARCHAR);
       //執行數據庫查詢操作
       pstm.execute();
       //輸出結果[第二個參數]
       System.out.println(pstm.getObject(1));
       //釋放資源
       pstm.close();
       connection.close();
   }
   
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc14</artifactId>
    <version>10.2.0.4.0</version>
    <scope>compile</scope>
</dependency>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章