Oracle第三课之PLSQL
Pl/SQL
學習目標
1、認識plsql
2、變量、常量
3、匿名塊
4、分支語句
5、循環
6、事務
學習內容
1、plsql
sql:結構化的查詢語言:
insert、update、delete、select
表鏈接查詢:
內連接:返回多表中都匹配(==)的數據
外連接:左(所有數據)、右(所有數據)
笛卡爾乘積:
plsql:過程化的SQL語言;擴充了:變量、常量、分支語句、循環、數據結構;匿名塊的形式寫命令
2、變量、常量
匿名塊
declare -- 聲明 變量 begin -- 開始exception -- 可以有 處理異常end; --結束變量:
declare -- 聲明 變量-- 變量名 數據類型v_sal number(10,2); v_name varchar2(30);begin -- 開始select sal into v_sal from emp where ename='333';-- 默認的服務器輸出命令-- || 字符串拼接夫 類似于java中的+dbms_output.put_line('二郎的工資是:'||v_sal);exception -- 可以有 處理異常-- no_data_found :預定義好的一個異常類型when no_data_found thendbms_output.put_line('沒有找到合適的數據');end; --結束常量:
-- 常量 declare--constant pi constant number(10,6):=3.1415926;r number(3):=20;area number(10,3); --面積 begin-- pi:=5.14; 常量不允許賦值area:=pi*r*r;dbms_output.put_line('面積是:'||area);end;3、數據類型:
數字:
number(38),number(總位數,小數的位數)時間:
date datetime字符串:
char:固定長度的字符類型 :char(N) :學號、身份證號、手機號;查詢效率高 varchar2:可變長度的字符類型 varchar2(N):合理利用存儲空間;名字、地址、.... 2000大類型:
BLOB:大的二進制類型 CLOB:大的文本類型引用類型:
列引用類型:
列名%type declarev_name emp.ename%type; --跟某列的數據類型一致v_sal emp.sal%type; beginselect ename,sal into v_name,v_sal from emp where empno=66;dbms_output.put_line('名字:'||v_name||'工資是:'||v_sal);end;行引用類型:
表名%rowtype declarev_emp emp%rowtype; --行引用類型 beginselect * into v_emp from emp where empno=66;dbms_output.put_line(v_emp.EMPNO||v_emp.ename);end;記錄類型:
-- 存儲:編號、部門名稱、員工的名字、工資 declare--聲明結構類型 type emp_record is record(dno dept.deptno%type,dname dept.dname%type,ename emp.ename%type,sal emp.sal%type);-- 聲明結構類型的變量 只能存放一行數據v_emp emp_record; beginselect dt.deptno,dt.dname,e.ename,e.sal into v_emp from dept dt,emp e where dt.deptno=e.deptno and e.empno=66;dbms_output.put_line(v_emp.dno||v_emp.ename);end;4、分支語句
if分支語句:
if 條件 then... end if;if 條件 then... else... end if;if 條件 then elsif 條件 then elsif 條件 then elsif 條件 then else end if; declarev_sal emp.sal%type; beginselect sal into v_sal from emp where empno=8000;if v_sal>=10000 thendbms_output.put_line('西藏');elsif v_sal>=6000 and v_sal<10000 thendbms_output.put_line('黃浦江邊.....');elsedbms_output.put_line('白開水就著饅頭');end if; end;case…end結構:
select deptno, case deptno when 3 then '科技部' when 4 then '信息部' end 部門 from dept;select deptno, case when deptno=3 then '科技部' when deptno=4 then '信息部' end 部門 from dept;5、循環
loop…end loop;
declarev_i number(10):=1; beginloop --循環dbms_output.put_line(v_i);v_i:=v_i+1;exit when v_i>10; --退出條件end loop; --結束循環end;while循環
declarev_i number(10):=1; beginwhile v_i<=10 loop --循環dbms_output.put_line(v_i);v_i:=v_i+1;end loop; --結束循環end;for循環
begin--reverse :反轉 for vi in reverse 1..10 loopdbms_output.put_line(vi);end loop; end;6、事務
事務?:transaction
確保一系列數據操作作為整體執行的一種機制。要么都執行,要么都不執行(回滾);
四大特性:
原子性(A):事務作為整體執行,要么執行,要么不執行。
一致性?:事務完成前 后 ,數據要保持一致狀態。
隔離性(I):多個事務在并發執行時,事務之間是相互隔離的。
持久性(D):數據更新到數據庫中對數據庫的影響是永久的。
不同的隔離級別引發的一些問題:
臟讀: A事務正在操作數據,尚未提交 ,B事務讀取到了A事務中正在操作的數據。
不可重復讀:A事務正在讀取數據,B事務修改了A事務中的部分數據,A事務再次讀取的數據不一致。只需要鎖住部分數據就可以。
幻讀:A事務正在讀取數據,B事務插入了部分數據,A事務再次讀取的數據不一致。需要鎖住整張表。
設置事務的隔離級別:
1、讀未提交(read uncommit) : 引發:臟讀、不可重復讀、幻讀
2、讀已提交(read committed):(oralce默認的隔離級別) 引發:不可重復讀、幻讀
3、重復讀(repeatable read) (mysql的默認隔離級別) 引發:幻讀
4、串行化(serializable)
oracle數據庫中,默認寫的insert,update,delete命令都屬于事務的一部分,需要顯示的提交或者回滾。
三個命令:
commit :提交 rollback :回滾 savepoint :設置保存點 beginupdate emp set sal=sal+1000 where empno=66;update emp set sal=0 where empno=88;commit; --提交rollback; --回滾end; beginupdate emp set sal=sal+2000 where empno=66;savepoint a;update emp set sal=sal+2000 where empno=88;savepoint b;--commit; --提交rollback to a; --回滾commit;end;jdbc中如何控制事務:
public static void main(String[] args) {Connection connection=null;PreparedStatement statement=null;try {Class.forName("oracle.jdbc.OracleDriver");connection= DriverManager.getConnection("","scott","tiger"); // 關掉自動提交事務connection.setAutoCommit(false); // 第一條命令statement=connection.prepareStatement("insert ....");statement.setInt();statement.setInt();statement.setInt();statement.setInt();statement.executeUpdate(); // 第二條命令statement=connection.prepareStatement("insert ....");statement.setInt();statement.setInt();statement.setInt();statement.setInt();statement.executeUpdate();// 提交事務connection.commit();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {try { // 回滾事務connection.rollback();} catch (SQLException ex) {ex.printStackTrace();}e.printStackTrace();}}總結
1、plsql是什么?
2、變量如何聲明?
declare變量名 數據類型:=值;3、數據類型:
%type %rowtype4、示例熟練
總結
以上是生活随笔為你收集整理的Oracle第三课之PLSQL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ES6前端就业课第三课之class
- 下一篇: Oracle就业课第四课之子程序