總結的新學的oracle數據庫,適合學過的新手複習


                05_Oracle

每週一看 升官發財 (0^0)

  1. DDL數據定義語言 語句自動提交
    create drop truncate alter
  2. DML數據操作語言 用commit,rollback結束當前事物
    update delete insert
  3. TCL事務控制語言 :commit rollback savepoint(保留點)
  4. DCL數據控制語言 :grant
  5. d: 用數字表達一週內的第幾天 1代表週日
    day: 全拼表達星期幾(sunday)
    month:
    mon: 簡拼表達月(mar)
    months_between(”,”): 相差多少月
    add_months(,n): 加n個月
    next_day(”,’星期’): 出現下個參數的日期
    last_day(”): 同月的最後一天
  6. 數據類型
    number
    char
    varchar2(一定要有值)(空格敏感)
  7. 一些關鍵字
    exists 存在
    default 缺省(默認)
    sysdate 系統時間
    show user 顯示用戶名
    rollback 回滾(到上次提交後)
    commit 提交(舊事物的結束,新事物的開始)
    where 過濾行
    group by 分組
    having 過濾組
    order by 排序(自然小-->大,asc升序)desc降序
    || 拼接運算符
    distinct 消除重複行
    case 1 when 2 then 3 when 4 then 5 else 6 end;
    decode(1,2,3,4,5,6)
    cross join
    inner join on 內連接
    outer(left /right ) join on
    union/union all 並
    intersect 交
    minus 差
    rownum 僞列 行標號1,2,3…
    rowid 僞id
    constraint 給約束寫個名字,可以不寫(也是系統自動創建
    索引的名字)
    primary key 主鍵約束
    unique key 唯一約束
    not null 非空約束
    check(col_name ….)
    references foreign key
    多個表級約束用’,’分隔.
    外鍵後面加:
    on delete cascade; delete父表時將刪除子表對應的行
    on delete set null; delete 父表時將子表對應的列賦值爲 null
    transaction 事物(undo segment 未提交保存的地方)

    synonym 同義詷 create synonym ytf for account;
    inline view
    view 約束 with check option,with read only;
    index (index segment,rowid)
  8. 單行函數
    數值函數:round,trunc
    轉換函數:to_char(,”),to_date(,”),to_number
    字符類型:upper 大寫,lower 小寫,initcap 首字母大寫,
    concat 拼接 length lpad 右對齊 rpad
    一般函數 nvl,ltrim/rtrim(去空格的函數 fm也行寫字符串裏面)
    組函數
    max,min,count,sum,avg
  9. –過濾條件
    in like is_null between_and =
    and or not <>,!=,^=
    col_name like ‘h_%’ escape ‘\’; ‘找h_開頭的
    (case when 1=2… then 3…
    when 1=4…then 5…
    else 6…
    end)=decode(1,2,3,4,5,6);
  10. from-where-group_by-having-select-order_by
    –執行順序
  11. alter session set nls_date_format=
    ‘yyyy mm dd hh24:mi:ss’;
    –改變session中的日期格式
  12. alter table tab_name drop constraint_name/primary key;
    –刪除約束/刪除主鍵約束
  13. alter table tab_name drop(col_name)
    –刪除一列
  14. alter table tab1_name add 表級約束語法(constraint constraint_name foreign key(col_name) references tab2_name(col_name))
    –增加約束
  15. alter table tab_name add(c1 char)
    –增加一列
  16. alter table tab_name modify(col_name null);
    –將not null改爲null列
  17. alter index index_name rebuild;
    –重建索引
  18. alter procedure/package/function p_name compile;
    –編譯過程/包/函數
  19. alter package pkg_ytf compile body;
    –編譯包體
  20. alter trigger trig_name compile;
    –編譯觸發器
  21. alter trigger trig_name disable/enable
    –觸發器無效/有效
  22. alter table tab_name disable/enable all triggers;
    –該表所有觸發器無效/有效
  23. create index index_name on table_name(col);
    –按照某表的列創建索引

  24. create table table_name (
    col_name1 類型,col_name2 類型….);
    –創建表

  25. create table table_name as select…;
    –創建表,類型和數據與子查詢結果一樣
  26. create or replace view as select….;
    –創建視圖
  27. create sequence se_name start with 1(缺省 1) maxvalue 5
    cache 3 ; (day309.sql(day08 42頁面))
    –創建序列號
  28. create or replace procedure p_name(
    v_name in/out/in_out type…)]is[可以定義變量]
    begin exception end;
    –創建過程
  29. create or replace function f_name(…) return type is
    [可以定義變量] begin end;
    –創建函數
  30. create or replace package pkg_name is end;
    –創建包
  31. create or replace package body pkg_name
    is begin <初始化代碼> end;
    –創建包體

  32. insert into table_name values();
    –添加一行數據
    insert into table_name select….;
    –添加子查詢的數據

  33. delete from table_name where…;
    –刪除行,表數據刪除高水位線沒動,表佔的空間沒有釋放,可以回滾
  34. drop table tab_name purge;
    –刪除表
  35. drop index/view/sequence/procedure/function/package _name;
    –刪除索引/視圖/序列號
  36. truncate table table_name;
    –刪除所有記錄,DDL語句自動提交,沒有回滾,空間釋放,不可以回滾
  37. update table_name set col_name=….where…;
    –改數據

select * from table_name;
select * from (select….);
–查詢


/*************PL/SQL************************/


  1. –procedure language/SQL
    4個關鍵字 declare begin exception end
  2. type rc_name is record(變量名1 類型1,變量名2 類型2…);
    –記錄類型的聲明
  3. type type_name is table of val_type index by key_type
    –關聯數組聲明 index_by_table
    關聯數組的方法:exists(i)第i個元素是否存在,避免拋出異常
    count返回聯合數組的元素個數,空數組值是0
    first/last 返回最小/最大下標號,爲空則返回 null
    next(n)/prior(n)返回第n個元素的前一個元素下標和後一個
    元素的下標,如果不存在,則返回 null
    trim(n) 從最後一個元素刪除n個元素
    delete 刪除所有元素 delete(n)刪除第n個 delete(m,n)刪除m到n
    遍歷數組,由於下標可以不連續,要判斷元素是否存在v_name.exists(i)
  4. v_id account.id%type(v_id的類型是表account中id列的類型)
    v_a account%rowtype(v_a的類型是記錄類型,包含account中所有列的類型)
    if .. then end;
    if .. then .. else .. end;
    if .. then elsif then .. else .. end;
  5. –分支
    loop exit when .. end loop;
    while .. loop .. end loo;
    for i in 下限..上限 loop…end loop;
  6. –循環
    動態語句:execute immediate ‘DDL,DML’;
    cursor cur_name is select * from account;(返回0或者多條記錄)
    對cursor %isopen cursor是open其值爲true
    %notfound %found%rowcount
    隱式cursor SQL%….
    cursor–open–fetch–close–用cursor的順序
  7. 異常: ORACL預定義異常
    NO_DATA_FOUND 沒有數據 too_many_rows多行 invalid_cursor 沒open
    zero_divide 除0 dup_val_on_index重複索引 value_error值類型不對

  8. 非預定義異常:先聲明異常,綁定異常
    pragma exception_init(e_name,-0001)

  9. 用戶自定義異常:先聲明異常,綁定異常,拋出異常
    (raise e_name),再捕獲
    sqlcode:返回當前錯誤代碼 sqlerrm:返回當前錯誤的消息文本,用文件保存
    procedure 參數是形參in不能作賦值目標 out有個返回值 in_out

  10. 想要在存儲過程中用DDL語句,要DBA直接給用戶授權
    grant create table to user_name;
  11. 調用其他用戶的過程,必須由過程的屬主授予權限
    grant execute on proc_name to user_name
  12. exec p_name;可以直接執行一個過程/函數…
    綁定變量 variable a number; exec :a:=1;
    print :a;打印綁定變量的值

  13. 難點: trigger 觸發器 (enable,disable)
    觸發時間:before,after
    觸發事件:insert,update,delete
    觸發器類型:statement,row
    觸發順序:before_statement before_row after_row after_statement
    create or replace trigger tri_name
    before/after insert/update/delete
    on tab_name
    for each row
    declare
    begin
    <執行語句>
    end;

       |--------|---:old ---|---:new ---|
    
        insert       null      要插入的值
    
        update       原始值      新值
    
        delete       原始值      null 
    

    :old,:new會在當條SQL語句(包含trigger)執行完畢後纔會改變,不是瞬發的

  14. create or replace procedure
  15. 匿名程序(declare begin exception end)。
  16. 過程頭(in;out;in out;形參,實參,對於out,in out 型參數要求實參必須是變量)。
  17. 用戶 角色 權限 怎麼在存儲過程中正確執行DDL語句。
  18. 變量(for 循環變量 局部變量 全局變量 綁寫變量)。
  19. SQL語句分析過程soft parse 軟分析,hard parse硬分析,程序員要寫帶定變量的規範的sql。
  20. 靜態sql,動態sql。靜態sql效率高。
    PL/SQL中的靜態SQL
  21. racle 在解析sql時會把plsql中定義的變量轉爲綁定變量,減少了硬分
    析次數
  22. server process將執行完的SQL cache起來,cursor不關閉,當再次執行
    SQL時,不需要軟分析
    過程中的參數會自動轉化爲綁定變量

匿名塊
數據類型(記錄 集合 遍歷 方法)變量 控制結構SQL(ddl dml select)
cursor 表態sql動態sql 綁定變量sql(編譯(soft parse;hard parse)
執行)session exception()
有名塊
procedure function 參數問題
package變量(局部 全局 綁定)
trigger dml(update insert delete)
PL/SQL的特點
結構化模塊化編程
良好的可移植性 跨平臺(操作系統)
良好的可維護性
提升系統性能
(最大問題)不便於向異構數據庫移植應用程序


–面試 一定要知道的 添彩哦
truncate table table_name;–刪除大表快 刪除高水位線
FTS全表掃描,找到HWM高水位線,之下所有的Date bolck

scan based index基於索引的掃描

transaction 事物

server process oracletarena
每創建一個連接,oracle服務器端多一個server prcess進程,
oracle中多了一個session

數據庫上的併發連接

JDBC與PL/SQL的優缺點:靜態 動態 的優缺點

編譯(軟 硬分析) 執行

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