oracle 基础1
目??? 錄
?
一. 數(shù)據(jù)庫基礎(chǔ)部分??
第一章?????? oracle基礎(chǔ)介紹????????????????????????????
第二章? 數(shù)據(jù)查詢語言(QL)???????????????????????????
?? 第三章? 數(shù)據(jù)定義語言(DDL)??????????????????????????
?? 第四章? Oracle數(shù)據(jù)分區(qū)表??????????????????????????
?? 第五章? PL/SQL程序設(shè)計(Procedual Language)????????
?? 第六章? Oracle權(quán)限設(shè)置????????????????????????????
?? 第七章? 存儲過程與函數(shù)??????????????????????????????
?? 第八章? 動態(tài)SQL??????????????????????????????
?
二、體系結(jié)構(gòu)/數(shù)據(jù)庫管理
?? 第一章? Oracle9i 體系結(jié)構(gòu)與物理結(jié)構(gòu)管理?????????????
?? 第二章? Oracle9i邏輯結(jié)構(gòu)管理???????????????????????
?? 第三章? Oracle9i 內(nèi)存結(jié)構(gòu)管理????????????????????
?? 第四章? Oralce9i實(shí)例及進(jìn)程結(jié)構(gòu)???????????????????
?? 第五章? Oracle9i表空間管理????????????????? ???????
?? 第六章? Oralce9i數(shù)據(jù)庫用戶管理?????????????????????
?? 第七章? Oracle9i數(shù)據(jù)庫備份與恢復(fù)???????????????????
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
第一部分?? 數(shù)據(jù)庫基礎(chǔ)部分????????????????? 回目錄
?
第一章??????????????????? oracle基礎(chǔ)介紹
?
??? 1、Oracle9i數(shù)據(jù)庫系統(tǒng)結(jié)構(gòu)與產(chǎn)品組成
???????????????? 67% Oralce
??????????????? /
?????? Internet
??????????????? \
????????????? ???????? 9% sql server
?
?????? 中大型網(wǎng)站: Unix + Oracle + jsp
?????? 小型網(wǎng)站:?? NT + sql server + asp
?????????????? ?50% Oralce
?????? ???? ??????/
?? ????DBMS --? 17% DB2(針對IBM機(jī)器)
?????? ???? ??????\
?????? ?????? ?????5% sql server
?
???? ORALCE應(yīng)用系統(tǒng)開發(fā)工具:
?
????????????????????? form -> *.form
???????????????????? /
?????? ??? Develop200 --? report -> *.rdf
?????? ???????????????? \
?????? ?? ? ??????????????graphics -> *.ogd
????????????? oralce application server
?
?????? 用戶數(shù): 不會影響并發(fā)數(shù),沒有限制 ,只有企業(yè)版才有用戶數(shù)概念。
?????? 會話數(shù):
?????? 進(jìn)程數(shù): 操作系統(tǒng)的進(jìn)程數(shù)應(yīng)該是Oracle進(jìn)程數(shù)的兩倍
?
?
????? 登陸sql*plus
????? 用戶:/as sysdba
????? 查詢數(shù)據(jù)庫選件產(chǎn)品:
????? sql > select * from v$option;
????? sql > set linesize 1000? --設(shè)置行寬
????? sql > set pagesize 100?? --設(shè)置頁寬
????? sql > Clear scr????????? --清屏
????? sql > /????????????????? --重復(fù)執(zhí)行sql
?
sql > show parameter processes 進(jìn)程數(shù)
?
????? 1)關(guān)于Oracle9i數(shù)據(jù)庫的數(shù)據(jù)庫管理員用戶
??????? Sys: 在Oracle9i 9.0.1之前的數(shù)據(jù)庫中,默認(rèn)口令字為:change_on_install; 在9.2.0中無默認(rèn)口令字
?????? ??? System: 在Oracle9i 9.0.1之前的數(shù)據(jù)庫中,默認(rèn)口令字為manager;在9.2.0中無默認(rèn)口令字
?
?????? 安裝結(jié)束后,可以更改:
?????? SQL>grant connect to system identified by "1234";
?????? SQL>password --需要原口令
?
?????? Sys用戶登陸數(shù)據(jù)庫方法:
?????? $sqlplus "/as sysdba"??????? --sys用戶無口令字要求(缺省)
?????? $sqlplus "sys/xx@cc as sysdba"? --sys用戶有口令要求
???????
??????
?????? ???????????????????? 操作系統(tǒng)認(rèn)證 (進(jìn)入oracle不許要口令字)只限于本地(8i/9i/10g)
?????? ??????????????? ????/
?????? sys用戶口令認(rèn)證方式:
????????????? ???????? ???????\
????????????? ???????????????? oracle認(rèn)證 (7i/8)
?
??????? unix: oinstall (oracle8i/9i)
?????? ??? dba
???????
?????? 修改sys用戶口令字的方法:
?????? A 修改認(rèn)證方法。
?????? ? D:\oracle\ora92\network\admin\sqlnet.ora
?????? ? SQLNET.AUTHENTICATION_SERVICES= (NTS)
?????? ? unix 中要加上這一行。
?????? ? NTS? - 操作系統(tǒng)認(rèn)證
?????? ? NONE? - ORACLE 認(rèn)證
?
?????? B 修改口令字
?????? ? c:\>orapwd file=d:\oracle\oracle\roa92\database\pwdSID.ora password=abcd entries=30?? --Windows
?
?????? ? $orapwd file=/u01/app/oracle/product/9.2.0/dbs orapwSID password=abcd entries=30????? --Unix
?
?????? C 重新啟動oracle服務(wù)(Windows)
?????? ? 重新啟動數(shù)據(jù)庫(Unix)
?
?????? ? tkprof:跟蹤文件整理命令,將跟蹤文件轉(zhuǎn)換為文本文件
?????? ? 語法:
?????? ?? D:\>tkprof 跟蹤文件 文本文件
?????? ?? 跟蹤文件存貯目錄:
?????? ?? D:\>oracle\admin\db_name\udump\*.trc? (trace)
?????? ?? 跟蹤文件需要定期清理。(bdump 和 udump 目錄)
?
?????? ?? 跟蹤每一條命令:
?????????? sql>alter session set sql_trace=true;|false 立刻生效,當(dāng)前session生效。
?????? ?????? sql>exec dbms_session.set_sql_trace(true);|false?? 以包的形式執(zhí)行。
??????????
?????? ?????? sql>alter system set sql_trace=true scope=spfile; 永久修改,重起生效。
?
????? 2)分布式數(shù)據(jù)庫 (必須是企業(yè)版)
?????? ???? 分布式數(shù)據(jù)庫的組成:
?????? ?????? 全局?jǐn)?shù)據(jù)庫管理系統(tǒng):GDBMS
?????? ?????? 局部數(shù)據(jù)庫管理系統(tǒng):LDBMS
?????? ?????? 通訊驅(qū)動程序:CM
?????? ?????? 全局?jǐn)?shù)據(jù)庫字典:DICITIONARY
?
?????? ????? SQL*NET 網(wǎng)絡(luò)驅(qū)動程序
?????? ????? sql*net v1.0??????????????? oracle 7.3.4
?????? ????? sql*net v2.0??????????????? oracle 7.3
?????? ????? net8??????????????????????? oracle 8??? \
?????? ????? net8i?????????????????????? oracle 8i?? - connect pool
?????? ????? oracle net services???????? oracle 9i?? / 斷掉后自動重新連接
?
?????? ?Oracle 運(yùn)行的過程
?????? ?http://www.oracle.com
?????? ?http://www.oracle.com:234/cgi-hie/gun.go/
?????? ????????????????????????? /虛擬路徑/包名.過程/???????????????????????????
?????
????? 3)Oracle的網(wǎng)絡(luò)配置
?
?????????????????????? 協(xié)議
?????????????????? ??/
???????? 主機(jī)字符串 --? IP地址或服務(wù)器
?????? ????????? ???????\
?????? ????????????????? \??? 數(shù)據(jù)庫實(shí)例名(oracle7/8)
??????????????????? ????- /
????????????????? ???????\
????????????? ?????????????????? 數(shù)據(jù)庫服務(wù)名(oracle8i/9i/10g)
?
???????? 8i以上盡可能用數(shù)據(jù)庫服務(wù)名。
????????
???????? 數(shù)據(jù)庫標(biāo)準(zhǔn)參數(shù)
?????? ??? 數(shù)據(jù)庫名:db_name
?????? ??? 數(shù)據(jù)庫實(shí)例名:instance_name
?????? ??? 數(shù)據(jù)庫域名:db_domain? 區(qū)分同名數(shù)據(jù)庫
?????? ??? 數(shù)據(jù)庫服務(wù)名:service_names
?????? ??? 全局?jǐn)?shù)據(jù)庫名:global_db_name? 查詢遠(yuǎn)程表,用在鏈路上
?
?????? ??? ORACLE_SID?? --操作系統(tǒng)環(huán)境變量。
?
??????????? 實(shí)例名和數(shù)據(jù)庫名是一一對應(yīng)關(guān)系(大部分情況;雙機(jī)熱備系統(tǒng)是一對二關(guān)系,共享磁盤陣列)
??????????? 兩個同名數(shù)據(jù)庫不能遠(yuǎn)程傳數(shù)據(jù)。
?????? ??????? 數(shù)據(jù)庫名相同時,可以加上域名,來區(qū)分?jǐn)?shù)據(jù)庫。
??????????? 當(dāng)數(shù)據(jù)庫名加上域名不相同時,可以進(jìn)行遠(yuǎn)程數(shù)據(jù)傳輸。
?
?????? ??? cats.hn.gov.cn
?????? ??? ----? --------
????????? |?????? |
?????? ????? |?????? |-------db_domain
?????? ????? |
?????? ????? |------db_name
?
?????? ?題目:
?????? ?一個數(shù)據(jù)庫全局?jǐn)?shù)據(jù)庫名是:prod.us.oracle.com
?????? ? 問:該數(shù)據(jù)庫名:prod
?????? ???? 域名:無
?????? ???? 服務(wù)名:prod
????
???????????? 數(shù)據(jù)字典
?????? ???? 查詢數(shù)據(jù)庫名:? ????
?????? ???? sql>select name from v$database;
?????? ???? sql>show parameter db_name
?
?????? ???? 查詢數(shù)據(jù)庫實(shí)例名:
?????? ???? sql>select instance_name from v$instance;
?????? ???? sql>show parameter instance_name
?
?????? ???? 查詢數(shù)據(jù)庫域名:
?????? ???? sql>select value from v$parameter where name='db_domain';
?????? ???? sql>show parameter db_domain
?
?????? ???? 查詢數(shù)據(jù)庫服務(wù)名:
?????? ???? sql>select value from v$parameter where name='service_anmes';
?????? ???? sql>show parameter service_names
?
?????? ???? 查詢?nèi)謹(jǐn)?shù)據(jù)庫名:
?????? ???? sql>select * from global_name;
?
?????? ???? v打頭的是動態(tài)數(shù)據(jù)字典
?
?????? ? Unix 下網(wǎng)絡(luò)配置工具
?????? ? $netlasst(Orale8i)
?????? ? $netca(Oracle9i/10g)
??????????
?
?????? ?? 以sys用戶連接遠(yuǎn)程數(shù)據(jù)庫:
?????? ?? $sqlplus "sys/xx@unix01 as sysdba"
?
?????? ? 啟動Net configuration Assistant
?????? ???? 選擇版本,遠(yuǎn)端服務(wù)名,ip地址,重新設(shè)置連接名(在原名后加01等),重新登陸sql*plus. 使用用戶名遠(yuǎn)程登陸。
?????? ???? 從sql*plus 進(jìn)入命令行:host
?
?
????????? 手工配置:
?????? ???? Oracle網(wǎng)絡(luò)連接解析文件:tnsnames.ora
?????? ???? D:\oracle\ora92\network\admin\tnsnames.ora??? ?????????????-windows
?????? ???? /u01/app/oracle/product/9.2.0/network/admin/tnsnames.ora?? -unix
?
?????? ???? Oracle檢聽進(jìn)程配置文件:listener.ora
?????? ???? D:\oracle\ora92\network\admin\listener.ora???????????????? -windows
?????? ???? /u01/app/oracle/product/9.2.0/network/admin/listener.ora?? -unix
?
?????? ???? $lsnrctl start??? -啟動監(jiān)聽進(jìn)程
?????? ???? $lsnrctl status?? -顯示監(jiān)聽進(jìn)程狀態(tài)
?????? ???? $lsnrctl stop???? -停止監(jiān)聽進(jìn)程
?
???????????? 查詢當(dāng)前在先用戶
?????? ???? sql>select username,sid,serial#,machine from v$session;
?????? ???? 刪除用戶會話:
?????? ???? sql>alter system kill session 'sid,serial#';
?????? ???? 查詢用戶所使用的sql:
?????? ???? sql>select user_name,sql_text from v$open_cursor;
?
?????? ???? 查詢sql區(qū)的sql語句:
?????? ???? sql>select sql_text from v$sqlarea;
?
???????????? 方便客戶端連接。
?????? ???? 將主機(jī)字符串寫入注冊表:
?????? ???? regedit.exe->Local Machine->Software->ORACLE->HOMEO
?????? ???? 增加關(guān)鍵字:local
?????? ???? 鍵???? 值:主機(jī)字符串
?????? ???
?
????? 3)、Oracle9i 相關(guān)的數(shù)據(jù)庫語言
?????????? sql 標(biāo)準(zhǔn)化數(shù)據(jù)庫語言
?????????? sql*plus 數(shù)據(jù)庫接口語言
?????????? pl/sql 過程化語言
?
????? 4)、數(shù)據(jù)分區(qū)技術(shù)(相對于數(shù)據(jù)集中)
???????? list分區(qū)表-不能比大小
?????? ???? range -可以比大小
???????? LOB數(shù)據(jù)類型
?????? ???? BLOB:存儲二進(jìn)制數(shù)據(jù)。例如:圖象、聲音、視頻等、以代替LONG RAW類型(oracle里面已經(jīng)淘汰)。
???????? CLOB:存儲大字符類型數(shù)據(jù)。例如:網(wǎng)上論壇,個人簡歷,說明信息等等以代替LONG 類型。
?????? ???? NCLOB:
?????? ???? nls(National Language Support)字符集,oracle中n打頭的基本上和國家有關(guān)。
?????????? A、數(shù)據(jù)庫核心字符集設(shè)置參數(shù):
?????? ?????? nls_characterset:zhs16cgb231280 (支持版本:oracle7.3以上,不支持繁體字)
?????? ? ???????????????????zhs16gbk????? (支持版本:oracle8.0以上,包括韓文、日文)
???????????????????? ???????????? zhs32gb18030? (支持版本:oracle9i以上,在上一個上支持蒙文、維吾而文等)
?
?
????????????? 查詢核心字符集:
?????? ????? sql>select * from nls_database_parameters;
?????????? B、客戶端字符集設(shè)置參數(shù):
?????? ????? nls_lang:simplified chinese_china.zhs16gbk
?????? ????? Windows下注冊表中的NLS_LANG項(xiàng)的鍵值
?
?????? ????? Unix下設(shè)置字符集方法:
?????? ????? $NLS_LANG="simplified chinese"_china.zhs16gbk
?????? ????? $export NLS_LANG
?
?????? ????? 兩個機(jī)器的ORACLE核心字符集不同,ORACLE拒絕遷移。
?????? ????? 如果本機(jī)的ORACLE不是中文,需要設(shè)置字符集,再進(jìn)行遷移。
?
?????? ?? C、修改數(shù)據(jù)庫核心字符集的方法。
?????? ????? sql>update props$
?????? ????????? set value$='ZHS16GBK'
????????????? ? ????where name='NLS_CHARACTERSET';
?????? ????? sql>commit;
?????? ????? 修改后,重新啟動數(shù)據(jù)庫即可。
?
????????????? 核心字符集和客戶端一致的話,也可以顯示中文。
?????? ????????? BFILE:外部存儲,將數(shù)據(jù)存儲在服務(wù)器外部磁盤。
?????? ????????? 只存路徑,不管格式。
?
????? 5)、Oracle9i數(shù)據(jù)庫改變及新增功能。
????????? sql>set com v7????? --設(shè)置數(shù)據(jù)庫為oracle7.0,臨時型設(shè)置。不支持V6。
?????? ? OLTP 數(shù)據(jù)庫和前端交互較多,回退空間較大。
?????? ? 數(shù)據(jù)倉庫,改動較少,回退空間較小。
?
????? 6)、Oracle9i數(shù)據(jù)庫語言設(shè)計:
????????? sql*plus? => sql? 、 sql*plus 、 pl/sql
?
?????? ? SQL語言:
?????? ???????? 查詢語言(QL):????? Query Language
????????????? ?數(shù)據(jù)操縱語言(DML):Data Manpulation Language
????????????? ?數(shù)據(jù)控制語言(DCL):Data Control Language
????????????? ?數(shù)據(jù)定義語言(DDL):Data Definition Language
?
?
?
?
?
?
?
?
?
第二章? 數(shù)據(jù)查詢語言(QL)????????????????? 回目錄
2006-4-5
??? 一、數(shù)據(jù)字典:
????????? 數(shù)據(jù)字典是一組存儲系統(tǒng)信息的表、視圖、同義詞等,由系統(tǒng)自動維護(hù),用戶可以查詢。
?
????????? 數(shù)據(jù)字典分類:
?????? ????? user_xxx: 存儲用戶創(chuàng)建的數(shù)據(jù)庫實(shí)體,例如:user_tables,user_indexes,user_db_links等。
?????? ????? all_xxx:? 存儲用戶可以存取的數(shù)據(jù)庫實(shí)體,例如:all_tables,all_indexes,all_links等。
?????? ?????????????? sql>select table_name from all_tables;
????????? dba_xxx:? 數(shù)據(jù)庫管理員數(shù)據(jù)字典。例如:dba_tables,dba_indexes,dba_links等。
?????? ?????????????? sql>select owner,table_name,tablespace_name from dba_tables;
????????? v$xxx:動態(tài)數(shù)據(jù)庫字典。例如:v$database,v$session,v$sga(內(nèi)存數(shù)),v$license(最大會話數(shù)),v$sgastat(內(nèi)存使用狀況)等。
?????????
?????? ? sql>set timing on? --設(shè)置跟蹤,顯示執(zhí)行時間。
?????? ? sql>select * from all_objects;所有的數(shù)據(jù)字典,大概有3萬多個。
?????? ?
??? 二、日歷及日期顯示格式的設(shè)置
?????? ? 1、設(shè)置日歷顯示格式參數(shù):nls_calendar?
?????? ? sql>alter session set nls_calendar="Japaness Imperial";
?????? ? sql>alter session set nls_calendar="ROC Official";
?????? ? sql>alter session set nls_calendar="Gregorian";
?
? ????????2、設(shè)置日期顯示格式參數(shù):nls_date_format
?????? ???? sql>alter session set nls_date_format='格式';
?????? ???? 常用日期格式組合方法:
?????? ???? yyyy.mm.dd??? yy.mm.dd? yyyy/mm/dd? yy/mm/dd? yyyy-mm-dd?? yy-mm-dd
?????? ???? yyyy"年"mm"月"dd"日dy hh24:mi:ss AM" 加AM時24要去掉
???????????? 周的話加dy --星期
?
?????? ???? sql>select sysdate+2+2/24+20/1440 from dual;? --過兩天兩小時20分鐘
?????? ???? 查詢5年11個月之后的時間:
?????? ???? sql>select sysdate+to_yminterval('05-11') from dual;
?????? ???? 查詢5年11個月之前的時間:
???????????? sql>select sysdate+to_yminterval('-05-11') from dual;
?
?????? ???? 查詢年度:
?????? ???? sql>select extract(year from sysdate) from dual;
?????? ???? 查詢月度:
?????? ???? sql>select extract(month from sysdate) from dual;
?????? ???? 查詢?nèi)掌?#xff1a;
?????? ???? sql>select extract(day from sysdate) from dual;
?
?????? ?? 3、將日期顯示格式寫入注冊表
?????? ???? Local Machine->Software->ORACLE->HOMEO
?????? ???? 增加關(guān)鍵字:nls_date_format
?????? ???? 鍵??? 值 :日期格式(注意:無單引號)
?????? ???? unix下設(shè)置:
?????? ???? $NLS_DATE_FORMAT=“格式”
?????? ???? $export NLS_DATE_FORMAT
?
?????? ???? 也可以將日期格式設(shè)置寫入ORACLE的登陸自動執(zhí)行文件:glogin.sql
?????? ???? D:\oracle\ora92\sqlplus\admin\glogin.sql (windows)
?????? ???? /U01/app/oracle/product/9.2.0/sqlplus/admin/glogin.sql? (nuix)
?
?????? ?? 4、時區(qū)(Time Zones)
?????????? 查詢當(dāng)前時區(qū):
?????? ????? sql>select sessiontimezone from dual;
?????? ????? 設(shè)置時區(qū):
?????? ????? sql>alter session set time_zone='-7:00';
?????? ????? 查詢當(dāng)前時間:
?????? ????? sql>select sysdate,current_date from dual;
?????? ????? 2006-04-05 2006-04-04
?????? ????? current_date:當(dāng)?shù)貢r間。
?????? ????? sysdate:當(dāng)前時間。
?
??? 三、謂詞
?????? ???? A、 IN(NOT IN):等于或不等于列表中的任意表達(dá)式
?????? ???????? sql>select * from emp
?????? ???????????? where deptno in(20,30,40)
?????? ???? B、 (NOT)BETWEEN AND:表示從小到大的一個范圍
?????? ???????? sql>select * from emp
????????????? ???? where sal between 1000 and 2000;
?????? ???? C、(NOT)LIKE :字符型數(shù)據(jù)匹配查詢
?????? ???????? sql>select * from emp
????????????? ???? where ename like 'S%';
????????????? ?-:表示一個字符;
????????????? ?%:表示任意字符串;
?????? ???? D、偽例
?????? ??????? rowid:唯一行標(biāo)識
????????????? rownum:行號
?
?????? ???? 問題:
?????? ???????? 試查詢EMP表中工資最高的5名員工及工資值及序號。
????????????? ?sql>select ename,sal,rownum from(select ename,sal from emp order by sal desc)
????????????? ?where rownum<=5;
?
????????????? ?http://fh_sw:7778/isqlplus
?
?????? ????? E、空值(NULL)
?????? ???????? 判斷以下語句是否正確:
????????????? ?sql>select ename,sal,comm,sal+nvl(comm,0) from emp;
???????????????? nvl(c1,c2)=c1 c1 不是空值,
????????????? ?????????? =c2 c1 是空值。
?????? ???????? 類似函數(shù):
????????????? ?? nvl2(c1,c2,c3)=c2 c1不是空值
????????????? ???????????????? =c3 c1是空值
?
?????????? 有獎金的加100,沒獎金的直接給500
????????????? ??? sql>select ename,nvl2(comm,comm+100,500) from emp;
?
????????????? ??? coalesce(c1,c2,c3,,,)=c1??????????????????? c1不是空值
????????????? ???? ????????????????=coalesce(c2,c3,c4,,,) c1是空值。返回第一個不為空的值。
????????????? ??? sql>select * from emp where comm is null;
?
????????????? ? case:
????????????? ??? sql>select ename,(case when sal<1500 then '低收入'
????????????? ?????????????????????? when sal>=1500 and sal<=3000 then '中等收入'
?????????????????????????????????? ?????????? when sal>3000 then '高收入'
??????????????????????????? ?????????? else '收入狀況不詳'
??????????????????????????? ?????????? end) "收入狀態(tài)",sal?
????????????? ??????? from emp;
??????????????????????????? ????
????????????? ?? decode:
????????????? ?? sql>select ename,job,
????????????? ?????????????? decode(job,'MANAGER',sal+300,'SALESMAN',sal+200,'CLERK',sal+100,sal) R_sal
????????????? ??????? from emp;
???????????????????? 違法字符用雙引號,字符型和日期型用單引號。
?
??? 四、匯總與統(tǒng)計
?????? ?? 常用聚組函數(shù):
?????? ???? sum,max,min,avg,count,count distinct,stddev(標(biāo)準(zhǔn)差)
?????? ???? sql>select count(*),count(comm)
?????? ???????? from emp;????????????????????? 空不記數(shù)。
?????? ???? sql>select count(distinct job)
?????? ???????? from emp;????????????????????? 工作崗位有幾個。
?
?????? ?? 分組統(tǒng)計:服務(wù)對職工工資進(jìn)行合計。
?????? ???? 分部門、
?????? ???? sql>select deptno,job,sum(sal)
?????? ???????? from emp
????????????? ???? group by deptno,job;
?
?????? ???? 分部門、職務(wù)對工資進(jìn)行合計,并計算工資總合。
?????? ???? sql>select deptno,job,sum(sal)
?????? ???????? from emp
????????????? ???? group by rollup(deptno,job);
?
???????????? 分別按部門、職務(wù)對工資進(jìn)行合計
?????? ???? sql>select deptno,job,sum(sal)
?????? ???????? from emp
????????????? ???? group by grouping sets(deptno,job);
?
???? 題目:
?????? ?? 分別按年度、部門、職務(wù)對工資進(jìn)行合計。同一個年度的在一起,同一部門的在一起,同一個職務(wù)的在一起。
????????????? ? sql>select to_char(hiredate,'yyyy'),deptno,job,sum(sal)
????????????? ????? from emp
????????????? ????? group by grouping sets(to_char(hiredate,'yyyy'),deptno,job);
?
?? 五、連接查詢(Join)
????? 1、等值連接
???????? 問:SMITH在哪里工作?
?????? ???? sql>select ename,loc
?????? ?????????? from emp,dept
?????? ????????? where emp.deptno = dept.deptno and ename='SMITH';
??????????
?????? ???? 查詢出員工姓名、工作地點(diǎn)、所在部門(要求列出公司所有部門)
????????????? ?? sql>select ename,loc,dept.deptno
?????????????????????? from dept left outer join emp on dept.deptno=emp.deptno;
?
????????????? ?? sql>select ename,loc,dept.deptno
????????????? ?????? from emp left join dept on dept.deptno=emp.deptno;
?
????????????? ?? sql>select ename,loc,dept.deptno
????????????? ?????? from emp,dept
????????????? ?????? where emp.deptno(+)=dept.deptno;
?
?? 六、子查詢(Subqueries)
?????? 1、多級子查詢
????????? 哪些員工的工作與SMITH相同,或者工資比他高?
?????? ? ????select * from emp
????????? where ename<>'TURNER'
?????? ?? ?????and (sal > (select sal from emp where ename='TURNER' )
?????? ??? ?????or deptno=(select deptno from emp where ename='TURNER'));
?
?????? 2、哪個員工最早受雇?
????????? sql>select ename
?????? ?????? ???from emp
?????? ????? ????where hiredate=(select min(hiredate) from emp);
?
?????? 3、當(dāng)查詢結(jié)果返回值不止一個時,可以使用all,any,in,not in 等
????????? 題目:
?????? ????? 查詢出其他部門中比部門30中某一個員工工資高員工?
?????? ????? sql>select ename,sal
?????? ??????????? from emp
????????????? ?? ??where deptno !=30
????????????? ???? and sal > any (select sal from emp
????????????? ???????????????????? where deptno=30); 相當(dāng)于min(sal)
?
?????? 4、當(dāng)查詢返回結(jié)果多個一列時,主查詢語句的where可以使用括號括起來。
????????? 題目:
?????? ????? 查詢與FORD具有相同職務(wù)與工資的員工?
?????? ????? sql>select ename,job,sal
?????? ??????????? from emp
????????????? ?? where (job,sal)=(select job,sal from emp where ename='FORD');
?
?????? dml 和 dcl部分暫時不講。
?
?
????? ????????????????????
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
第三章 數(shù)據(jù)定義語言 (DDL) ???????????回目錄
?? 一、創(chuàng)建基表
????? 語法:
????????? sql>Create table 表名(
?????? ????? 列名1 數(shù)據(jù)類型 約束條件,
?????? ????? 列名2 數(shù)據(jù)類型 約束條件,
?????? ????? ,,,,)
?????? ? sql>Create table product(
?????? ????? p_name varchar2(20) unique,
?????? ????? p_id number(5) primary key,
?????? ????? p_date date not null,
?????? ????? p_loc char(30) default '鄭州',
?????? ????? constraint uk_p_name unique(p_name),
?????? ????? constraint pk_i_id primary key(p_id));
?????????????? /*(constraint nn_p_date not null,)*/
?????? ?????? check(p_id>11110 and p_id<99999)
?????? ?????? 以上sql有點(diǎn)問題
?????? ?? 查詢表約束:
?????? ?? sql>select * from user_constraints
?????? ?????? where table_name='PRODUCT';
?
?????? ?? 查詢列的默認(rèn)值:
?????? ?? sql>select column_name,data_default
?????? ???????? from user_tab_columns
?????? ???? ???where table_name='PRODUCT';
?
?????? ?? save c:\p1;
?????? ?? get c:\p1;
?
?????? ?? sql>insert into product(p_name,p_id,p_loc)
?????? ?????? values('計算機(jī)',12221,sysdate,default)
?
?????? ?? 約束條件的修改:
?????? ?? 刪除約束:
?????? ???? sql>alter table product drop primary key;?????????? --刪除主鍵約束
????????? ???sql>alter table product drop unique(p_name);??????? --刪除唯一性約束
?????? ???? sql>alter table product modify(p_date null) ;?????? --取消非空約束
?????? ???? sql>alter table product modify(p_loc default null); --取消默認(rèn)值
?
?????? ?? 增加約束條件:
?????? ???? sql>alter table product add primary key(p_id);
?????? ???? sql>alter table product add unique(p_name);
?????? ???? sql>alter table product add modify(p_date not null);
?????? ???? sql>alter table product add modify(p_loc default '河南');
?
??
?
二、數(shù)據(jù)類型
????? 字符型 varchar2(n)? n<=4000??? --空間不滿的時候回退空間
??????????? char(n)????? n<=2000??? --占滿空間
?
????? 數(shù)字型 number(n)????? 0-9.99(e125) 最大38位
??????????? number(n,d)???
?????? ??? int????????? 最大38位數(shù)
?
????? 日期型 date
?
????? 二進(jìn)制 long raw
?
????? 大對象 LOB
???????????? BLOB:存儲二進(jìn)制數(shù)據(jù)
?????? ??????? CLOB:存儲大字符數(shù)據(jù)
?????? ??????? NCLOB:外部數(shù)據(jù)
?
?
?????? ??? 創(chuàng)建含有LOB列的基表:
?????? ??? sql>Create table employee(
?????? ??????? name varchar(30),
????????????? id?? int primary key,
????????????? photo blob,
????????????? resume clob) lob(photo,resume)
????????????? store as(tablespace users chunk 2048 cache);
??????????? 說明:
?????? ??????? Chunk 表示讀取LOB列時的數(shù)據(jù)量。默認(rèn)2K
????????????? 經(jīng)常查詢LOB列時可以增加Cache,默認(rèn)NoCache
2006-4-6
?????? ??? BFILE數(shù)據(jù)類型的使用方法:
?????? ??? A:創(chuàng)建相應(yīng)的物理存儲路徑
?????? ??? B:使用Create directory創(chuàng)建目錄與物理路徑連接。
?????? ??? C:為用戶授予Read on directory 權(quán)限
?
?????? ??? 例如:D:\video
?????? ???????? sql>Create or Replace directory video as 'd:\video';
????????????? ?sql>grant read on directory video to stud20;
?
????????????? ?sql>Create table b_video(
????????????? ???? bname varchar2(20),
????????????? ???? bid int primary key,
????????????? ???? bvideo bfile);
?
???????????????? sql>Insert into b_video
????????????? ???? values('影象資料',1001,bfilename('video','2.bmp'));
?
?
?
?????? ??? 查詢基表原代碼:
?????? ??? sql>select dbms_metadata.get_ddl('TABLE','B_VIDEO') from dual;? --輸出SQL;
?????? ??? sql>select dbms_metadata.get_xml('TABLE','PRODUCT') from dual;? --輸出SQL;
?
?????? ??? sql>set long 1000
?
?? 三、創(chuàng)建臨時表(Temporary Table)
?????? sql>Create global temporary table temp_data(
?????????? name varchar2(20),
?????? ?? id int,
?????? ?? hdate date)
?????? ?? on commit delete rows 或
?????? ?? on commit preserve rows
?????? 其中,on commit delete rows:表示提交后數(shù)據(jù)被刪除
??????????? on commit preserve rows:表示會話中斷后被刪除
??????
?????? sql>set autocommit on 自動提交
?????? sql>set autocommit 5? 5次事務(wù)
?
?????? 查詢是否創(chuàng)建了臨時表:
?????? sql>select table_name,temporary from user_tables;
?
?
?? 四、創(chuàng)建聚集(Clusters)
????? 幾個表有關(guān)連,經(jīng)常做聯(lián)合查詢時,使用Clusters,比較節(jié)省空間,增加查詢速度。
????? 創(chuàng)建Cluster
????? sql>Create cluster prod_sales(p_id int);
????? 創(chuàng)建主表,指定Cluster關(guān)鍵字
????? sql>Create table prod(
????????? p_name varchar2(20),
?????? ? p_id int primary key) cluster prod_sales(p_id);
????? 創(chuàng)建子表,指定外鍵及Cluster關(guān)鍵字
????? sql>Create table sales(
????????? sname varchar2(20),
?????? ? p_id int,
?????? ? constraint fk_p_id foreign key(p_id) references
?????? ? prod(p_id)) cluster prod_sales(p_id);
?
????????? sql>select extent_id,bytes,blocks,segment_name from user_extents; 查看表的存儲空間
??
?? 五、創(chuàng)建對象類型表(Objects)
????? 創(chuàng)建對象類型:
????? sql>Create or Replace type emp_other as object(
????????? emp_id number(7),
?????? ? emp_name varchar2(20),
?????? ? emp_sal number(7,2));
?????? ? 需要使用'/'來執(zhí)行。
?
????? 創(chuàng)建含有對象類型的基表:
????? sql>Create table e_info(
????????? job varchar2(20),
????????? hdate date,
?????? ? other emp_other);
?????? 數(shù)據(jù)插入:
?????? sql>Insert into e_info
?????????? values('工程師',sysdate,emp_other(1234,'李大力',1500));
?????? 數(shù)據(jù)查詢:
?????? sql>select job,hdate,p1.other.emp_name,p1.other.emp_sal
?????????? from e_info p1;
?
?? 六、修改表結(jié)構(gòu)
????? 1、增加新列
??????? sql>alter table product add(
?????? ??? p_list number(7) constraint uk_p_list unique,
?????? ??? p_no int);
????? 2、刪除列 (支持版本:Oracle8i以上版本)
??????? sql>alter table product drop(p_list,p_no);
????? 3、修改表名
??????? sql>rename 原名 to 新名;
????? 4、修改列名 (支持版本:Oracle9i 9.2.0以上)
??????? sql>alter table prodctuct rename column p_name
?????? ??? to product_name;
????? 5、修改列寬
???????? sql>alter table product
?????? ???? modify(p_name varchar2(40));
?????? ???? 要求整列為空,才可以從大到小修改列寬。
????? 6、回收表中未使用空間
???????? sql>alter table product deallocate unused keep 80k; ----回收表中部分未使用空間。
?????? ?sql>alter table product deallocate unused; ----回收表中全部未使用空間。
?
?????? ?表中未使用空間:delete from emp; 刪除后不釋放空間。
?????? ????????????? 數(shù)據(jù)刪除后產(chǎn)生未使用空間。
????????????? ????? truncate table emp;傾空表,回收空間。
?????? ?
?????? ? HWM(high water mark 水印)
?????? ? 查詢表所占用空間大小:
?????? ? sql>select sum(bytes),sum(blocks)
?????? ????? from user_extents
?????? ????? where segment_name='EMP';
?????? ? sql>Create table payment as select ename,sal from emp;
?????? ? sql>Insert into payment select * from payment;
?
?? 七、創(chuàng)建序列(Sequences)
????? 創(chuàng)建序列的語法:
????? sql>Create sequence 序列名
????????? start with 起始編碼
?????? ? increment by 步長
?????? ? maxvalue 終止編碼;
?
????? sql>Create sequence id_code
????????? start with 11111
?????? ? increment by 1
?????? ? maxvalue 9999999 cycle cache;
?????? ? cycle 循環(huán),Nocycle不循環(huán)
?????? ? Nocache.
?
????? 使用序列的方法:
????????? id_code.currval? --取當(dāng)前值
?????? ? id_code.nextval? --取下一個值
?
?????? ? 首次使用時,應(yīng)使用nextval.
?
????????? sql>Create table icode(
?????? ????? code int primary key);
?????? ? sql>Insert into icode values(id_code.nextval);
?
?????? ? 查詢數(shù)據(jù)庫中的序列:
?????? ? sql>select sequence_name,increment_by,max_value
?????? ????? from user_sequences;
?????? ? 刪除序列:
?????? ? sql>drop sequence id_code;
?
?? 八、數(shù)據(jù)庫鏈路(Database Link)
????? 數(shù)據(jù)庫鏈路是分布式數(shù)據(jù)庫中的一種數(shù)據(jù)庫實(shí)體,通過鏈路可以實(shí)現(xiàn)數(shù)據(jù)的遠(yuǎn)程傳輸。
?
????? 創(chuàng)建數(shù)據(jù)庫鏈路的語法:
????? sql>Create database link 鏈路名 對方數(shù)據(jù)庫的全局?jǐn)?shù)據(jù)庫名。(同名數(shù)據(jù)庫不能建鏈路,同名數(shù)據(jù)庫加域名可以建鏈路)
????????? connect to 用戶名 identified by 口令字
?????? ? using '主機(jī)字符串';
????? sql>Create database link swora.us.oracle.com
????????? connect to stud20 identified by stud20
?????? ? using 'swora01';
?
????? 數(shù)據(jù)庫鏈路的使用:
????? sql>select * from tab@swora.us.oracle.com;
????? 刪除鏈路:
????? sql>drop database link swora.us.oracle.com;
????? 查詢數(shù)據(jù)鏈路:
????? sql>select db_link,username,password,host from user_db_links;
????? sql>column db_link format a20
????? sql>column host format a10
?
????? DDL操作不允許使用鏈路
?
????? 使用數(shù)據(jù)庫鏈路實(shí)現(xiàn)數(shù)據(jù)實(shí)時傳輸:
????? 數(shù)據(jù)插入觸發(fā)器:
????? sql>Create or Replace trigger Insert_dept
????????? before insert on dept
?????? ? for each row
?????? ? Begin
?????? ???? insert into dept@swora.us.oracle.com
?????? ???? values(:new.deptno,:new.dname,:new.loc);
?????? ? End Insert_dept;
????? 查詢錯誤:
????? sql>show error
????? 查詢觸發(fā)器:
????? sql>select trigger_name from user_triggers;
????? 查詢觸發(fā)器原代碼:
????? sql>select trigger_body from user_triggers
????????? where trigger_name='INSERT_DEPT';
????? sql>drop trigger 觸發(fā)器名;
?
????? 數(shù)據(jù)更新觸發(fā)器:
????? sql>Create or Replace trigger Update_dept
????????? before update on dept
?????? ? for each row
?????? ? Begin
?????? ??? update dept@swora.us.oracle.com
?????? ??? set deptno=:new.deptno,dname=:new.dname,loc=:new.loc
?????? ??? where deptno=:old.deptno and
?????? ????????? dname=:old.dname?? and
????????????? ? loc? =:old.loc;
?????? ? End Update_dept;
?
?
????? 數(shù)據(jù)刪除觸發(fā)器:
????? sql>Create or Replace trigger Delete_dept
????????? before delete on dept
?????? ? for each row
?????? ? Begin
?????? ??? delete from dept@swora.us.oracle.com
?????? ??? where deptno=:old.deptno;
?????? ? End Delete_dept;
?
????? 使用數(shù)據(jù)庫鏈路實(shí)現(xiàn)數(shù)據(jù)定時傳輸:
????? 快照是從主節(jié)點(diǎn)傳向輔節(jié)點(diǎn),兩個數(shù)據(jù)庫要分主節(jié)點(diǎn)和輔節(jié)點(diǎn)。
????? 創(chuàng)建快照(Snapshots)語法:
????? sql>Create snapshots 快照名
????????? refresh 刷新方式
?????? ? next??? 時間間隔
?????? ? with parmary key
?????? ? as select 子句:
?
????? 語法說明:
??????? 刷新方式:? complete?? ---完全刷新? 比較主/副節(jié)點(diǎn)的表,不同的話,刷新
?????? ????????? fast?????? ---快速刷新?
????????????? ? force????? ---強(qiáng)制刷新
??????? 時間間隔:? 以天為單位
????????????????? sysdate + 1/24 一小時加一次
????????????? ? Next_Day(trun(sysdate),'星期日')+22/24 每周日的22點(diǎn)
??????? with parmary key: 要求傳輸?shù)谋硪兄麈I
?
?????? 創(chuàng)建快照步驟:
?????? ?A、授予用戶創(chuàng)建快照權(quán)限(輔節(jié)點(diǎn))
?????? ??? sql>grant create snapshot to 用戶名; 需要系統(tǒng)管理員授權(quán)。
???????? B、創(chuàng)建快照(輔節(jié)點(diǎn))
?????? ?C、創(chuàng)建快照日志(主節(jié)點(diǎn))
?????? ??? sql>Create snapshot log on 表名;
?
?????? ??? sql>Create snapshot c_dept
?????? ??????? refresh force
??????????????? next sysdate +1/1440
??????????????? with primary key
????????????? as select * from dept@swora.us.oracle.com;
????????????
?????? ???? sql>drop snapshot c_dept;
????????? 建立、刪除日志要單獨(dú)進(jìn)行。
????????? 查找快照信息(輔節(jié)點(diǎn)查詢):
?????? ? sql>select name,master_owner,master,type,next,master_link
?????? ????? from user_snapshots;
?????? ? 其中:name? --快照名
?????? ?????? master_owner ---主節(jié)點(diǎn)用戶名
?????? ?????? master?????? ---主節(jié)點(diǎn)表名
?????? ?????? type:??????? --刷新方式
?????? ?????? next:??????? --時間間隔
?????? ?????? master_link? ---數(shù)據(jù)庫鏈路名
?
?????? ? 創(chuàng)建公共數(shù)據(jù)庫鏈路:
?????? ? 同名用戶的對聯(lián)。管理員創(chuàng)建,大家使用。
?????? ? sql>create public database link swora.us.oracle.com
?????? ????? using 'swora01';
?????? ? 查詢公共鏈路:
?????? ? sql>select owner,db_link,username,host
?????? ????? from all_db_links;
?
?? 九、創(chuàng)建視圖(Views)
????? 1、視圖的特點(diǎn)
???????? 不存儲數(shù)據(jù)
?????? ?數(shù)據(jù)來源于基表
?????? ?不能數(shù)據(jù)復(fù)制
?????? ?在同一個表上可以建多個視圖
????? 2、創(chuàng)建視圖的語法
???????? sql>Create or Replace view 視圖名 as select 子句
?????? ?sql>Create or Replace view manager
?????? ???? as select * from emp
?????? ???? where job='MANAGER';
?????? ?sql>select text from user_views
?????? ???? where view_name='MANAGER';
????? 3、創(chuàng)建視圖時的選項(xiàng):
???????? with read only????? ---創(chuàng)建只讀視圖
???????????? sql>Create or Replace view manager
?????? ???????? as select * from emp
?????? ???????? where job='MANAGER' with read only;
?
?????? ?with check option?? ---在通過視圖修改數(shù)據(jù)時,必須滿足創(chuàng)建視圖時where指出的條件。
?????? ???
?????? ???? sql>Create or Replace view part20
?????? ???????? as select empno,ename,hiredate,deptno
????????????? ?from emp
????????????? ?where deptno=20 with check option;
?????? ???? sql>Insert into part20
?????? ???????? values(1235,'李大力',sysdate,30);
????????????? ?此語句出錯(視圖 WITH CHECK OPTION 違反 where 子句)
?????? ???? sql>select * from part20;
?
?????? 4、為視圖列取別名
????????? 試判斷以下語句是否有錯誤?
?????? ? sql>create or Replace view pay as
?????? ????? select ename,sal,sal*12 sal_year,sal+nvl(comm,0) sal_month
?????? ????? from emp;
?
?????? ? sql>create or Replace view pay(c1,c2,c3,c4) as
?????? ????? select ename,sal,sal*12 ,sal+nvl(comm,0)
?????? ????? from emp;
?
?????? ????? %,#,$,\,/ 非法字符
?
?
?
?
?
?
?
?
?
?
第四章?? Oracle數(shù)據(jù)分區(qū)表???????? 回目錄
?
?? 一、什么是數(shù)據(jù)分區(qū)表
?????? 數(shù)據(jù)分區(qū)就是把表劃分為多個存儲區(qū)域。在創(chuàng)建表結(jié)構(gòu)時考慮分區(qū)方案,選取表中一個或多個列作為分區(qū)關(guān)鍵字,該關(guān)鍵字決定在哪些數(shù)據(jù)存儲在哪些分區(qū),所插入數(shù)據(jù)自動存儲到相應(yīng)分區(qū)。
????
?? 二、創(chuàng)建分區(qū)表
????? sql>Create table c_employee(
????????? name varchar2(20),
?????? ? id number(7),
?????? ? sal number(7,2)) partition by range(sal)
?????? ? (partition p1 values less than(500) tablespace users,
?????? ?? partition p2 values less than(800) tablespace indx,
?????? ?? partition p3 values less than(1500) tablespace odm,
?????? ?? partition p4 values less than(maxvalue) tablespace tools);
?????? ?? 不確定的數(shù)據(jù)存入p4。
?
????? 查詢表空間信息:
????? sql>select tablespace_name from dba_tablespaces;
????? 其中,temp,undotbs1不得使用。
????? 使用sql>c/100/200可以替換
?
??? 三、分區(qū)表查詢
?????? sql>select * from c_employee;
?????? sql>select * from c_employee partition(p1);
?????? sql>Create table c_part2
?????????? as select * from c_employee partition(p2);
?
??????? 查詢是否為分區(qū)表:
?????? sql>select table_name,partitioned from user_tables;
?????? 查看分區(qū)表結(jié)構(gòu):
?????? sql>select partition_name,high_value,tablespace_name
?????? ??? from user_tab_partitions
?????? ??? where table_name='C_EMPLOYEE';
?
2006-4-7
?
??? 四、分區(qū)表結(jié)構(gòu)的修改
??????? 1、刪除分區(qū)
?????? ?? sql>alter table c_employee drop partition p3;
?????? 2、增加分區(qū)
?????? ?? 如果有maxvalue區(qū),則不能增加區(qū),需要先刪除,如果有數(shù)據(jù)就比較麻煩,所以一般不設(shè)置maxvalue區(qū)。
?????? ?? 必須增加在最后一個區(qū)后面
?????? ?? sql>alter table c_employee add partition p3
?????? ?????? values less than(1500) tablespace users;
?????? 3、分區(qū)數(shù)據(jù)移動:將分區(qū)數(shù)據(jù)從一個表空間移動到另外一個表空間,將結(jié)構(gòu)和數(shù)據(jù)全部移動。
?????? ?? sql>alter table c_employee move partition p1
?????? ?????? tablespace tools;
?????? 4、修改分區(qū)名稱
?????? ?? sql>alter table c_employee rename partition p1 to part1;
?????? 5、分區(qū)的拆分(Split)在兩個區(qū)之間增加區(qū)。
?????? ?? sql>alter table c_employee split partition p3
?????? ?????? at(1000) into (partition p21,partition p31);
?????? 6、合并(Merge)必須向高的一方合并。
?????? ?? sql>alter table c_employee merge partitions p21,p31
?????? ?????? into partition p3;
?
??? 五、創(chuàng)建List分區(qū)表
?????? 用于數(shù)據(jù)不能比較大小的時候
?????? sql>Create table list_prod(
?????????? pname varchar2(20),
?????? ?? pid int,
?????? ?? ploc varchar2(20))partition by list(ploc)
?????? ?? (partition p1 values('廣東') tablespace users,
?????? ??? partition p2 values('河南') tablespace tools,
?????? ??? partition p3 values('陜西','河北') tablespace odm,
?????? ??? partition p4 values(null) tablespace indx,
?????? ??? partition p5 values(default) tablespace example);
?
?????? ??? null 沒有產(chǎn)地
?????? ??? default 其它情況
?????? ??? List分區(qū)表的使用和上面的一樣。
?
??????? 工具介紹
??????? Oracle Management Server(Oracle管理服務(wù)器配置) 可以把網(wǎng)上的多個數(shù)據(jù)庫放在Oracle上使用。
?????? 服務(wù):機(jī)器名:1521:SID
?????? SID實(shí)例名
???
??????? Oracle Enterprise Manager Console
??????? 管理員:sysman
?????? 口令字:oem_temp (default)
?
?
?
?
?
?
?
?
?
?
?
?
?
???????????????????????????? 第五章? Oracle權(quán)限設(shè)置?????????????????? 回目錄
??? 一、Oralce權(quán)限分類
?????? 系統(tǒng)權(quán)限:系統(tǒng)限制用戶在數(shù)據(jù)庫中的操作權(quán)限(用戶)
?????? 實(shí)體權(quán)限:某種權(quán)限用戶對于其他用戶實(shí)體的存取權(quán)限(實(shí)體)
?
??? 二、系統(tǒng)權(quán)限的管理
?????? 1、系統(tǒng)權(quán)限的分類
????????? DBA:權(quán)限組合,擁有全部特權(quán),是系統(tǒng)最高權(quán)限,只有DBA才可以創(chuàng)建數(shù)據(jù)庫
?????? ? RESOURCE:擁有RESOURCE權(quán)限的用戶可以登陸數(shù)據(jù)庫。可以創(chuàng)建數(shù)據(jù)庫實(shí)體,不可以創(chuàng)建數(shù)據(jù)庫結(jié)構(gòu)。
?????? ? CONNECT:擁有CONNECT權(quán)限的用戶可以登陸數(shù)據(jù)庫。(會話權(quán)),不可以創(chuàng)建數(shù)據(jù)庫實(shí)體。
?????? 2、系統(tǒng)權(quán)限的授權(quán)命令
??? ??????sys/system:
?????? ? sql>Create user acc01 identified by acc01;
?????? ? sql>grant connect,resource to acc01;
?????? ? 查詢用戶擁有的權(quán)限:
????????? sql>select * from user_role_privs;
?????? ? ADM:是否允許權(quán)限傳遞
?????? ? sql>select * from role_sys_privs;
?????? ? sql>select * from dba_role_privs;
?? ????3、系統(tǒng)權(quán)限傳遞
????????? sys/system:
?????? ? sql>grant connect,resource to acc01 with admin option;
?????? 4、系統(tǒng)權(quán)限回收
????????? sql>revoke connect,resource from acc01;
?????? ? DBA權(quán)限不要輕易授權(quán)
?????? ? 系統(tǒng)權(quán)限之間無級聯(lián):授出權(quán)限可以跨級收回
?????? ? 實(shí)體權(quán)限之間有級聯(lián):收回一個全部收回,不能跨用戶收取權(quán)限。
?
??? 三、實(shí)體權(quán)限管理
?????? 1、實(shí)體權(quán)限分類
????????? select,update,insert,delete,alter,index,execute,references,all.
?????? 2、實(shí)體權(quán)限授權(quán)命令
????????? scott:
?????? ? sql>grant select,update,delete on product to acc01;
?????? ? sql>grant select,update,delete on product to public;? --授權(quán)給public
?????? ? acc01:
?????? ? sql>select * from scott.product;
????????? 查詢用戶創(chuàng)建的基表:
?????? ? sql>select table_name from user_tables;
?????? ? 查詢用戶可以存取的基表:
?????? ? sql>select owner,table_name from user_tables;
?????? ? 查詢授出權(quán)限的表:
?????? ? sql>select grantee,owner,table_name,privilege
?????? ????? from user_tab_privs;
?????? 3、實(shí)體權(quán)限傳遞:with grant option
????????? scott:
?????? ? sql>grant select,update,delete on emp
?????? ????? to acc01 with grant option;
?????? ? acc01:
?????? ? sql>grant select on scott.emp to acc02;
??????? 3、實(shí)體權(quán)限的回收:
?????? ?? scott:
?????? ?? sql>revoke select,update on emp from acc01;
?????? ?? DBA可以管理實(shí)體用戶
?
??? 四、同義詞(Synonyms)
?????? 1、創(chuàng)建私有同義詞:
????????? acc01:
?????? ? sql>select * from scott.emp;
????????? sql>Create synonym emp
?????? ????? for scott.emp;
?????? ? sql>select * from emp;
????????? 創(chuàng)建同義詞代替數(shù)據(jù)鏈路
????????? sql>select * from dept@swora.us.oracle.com;
?????? ??sql>Create synonym y_dept
????????????? for dept@swora.us.oracle.com;
?
?????? 2、創(chuàng)建公共同義詞:
????????? 由DBA創(chuàng)建,所有用戶共同使用
?????? ? 語法:sql>Create public synonym 公共同義詞名 for 代替項(xiàng);
?????? ? scott:
?????? ? sql>Create table e_sal as select ename,sal from emp;
?????? ? sql>grant select on e_sal to public;
?????? ? system:
?????? ? sql>Create public synonym e_sal for scott.e_sal;
?????????
????????? 查詢私有同義詞:
?????? ? sql>select synonym_name ,table_owner,table_name,db_link
?????? ????? from user_synonyms;
?????? ? sql>column synonym_name format a10;
?????? ? sql>column db_link format a10;
?
?????? ? 查詢公共同義詞:
?????? ? sql>select synonym_name ,table_owner,table_name,db_link
?????? ????? from dba_synonyms;
?????? ? sql>column synonym_name format a10;
?????? ? sql>column db_link format a10;
?????? ? db_link:遠(yuǎn)程表
?
?????? ? 刪除公共同義詞:
?????? ? sql>drop public synonym e_sal;?? --管理員刪除
?
????????? 刪除私有同義詞:
?????? ? sql>drop synonym e_sal;????????? --用戶刪除
?
?????? ?????????? 第六章???? PL/SQL程序設(shè)計(Procedual Language)?? ??????? 回目錄?
??? 一、PL/SQL概述
?????? ROWTYPE? %rowtype 行類型
?????? 用于存儲表中的一行記錄
?????? Declare
?????? v_empno emp.empno%type:=&empno;
?????? r emp%rowtype;
?????? Begin
??????? select * into r from emp where empno=v_empno;
?????? dbms_output.put_line('姓名:'||r.ename||' |工資:'||r.sal||' |工作時間:'||r.hiredate);
?????? End;
?
?????? &:表示接收鍵盤輸入。
??????
?????? 打開屏幕打印開關(guān):
?????? SQL> set serveroutput on
??????
??????
?????? Rocode:記錄類型
??????? Declare
???????? v1 emp.empno%type:=&empno;
?????? ?type r_record is record(
?????? ??????? v1 varchar2(20),
????????????? v2 number(7,2),
????????????? v3 date);
?????? ?r r_record;
?????? Begin
?????? ?? select ename,sal,hiredate into r from emp
?????? ?? where empno=v1;
?????? ?? dbms_output.put_line('姓名:'||r.v1||' |工資:'||r.v2||' |工作時間:'||r.v3);
?????? End;
?
??????? Type: 表類型
?????? ?Declare
?????? ?? v1 emp.empno%type:=&v1;
?????? ?? type t_emp is table of varchar2(20) index by binary_integer;
?????? ?? t1 t_emp;
?????? ?? v_i integer;
?????? ?Begin
?????? ?? select 1 into v_i from dual;
?????? ?? select ename,sal into t1(v_i),t1(v_i+1) from emp where empno=v1;
?????? ?? dbms_output.put_line('編碼:'||v1||' |姓名:'||t1(v_i)||' |工資:'||t1(v_i+1));
?????? ?End;
?
??? 二、條件判斷語句
??????? Declare
???????? v1 emp.empno%type:=&empno;
?????? ?v2 number(7,2);
?????? ?v3 varchar2(20);
??????? Begin
?????? ?? select sal,ename into v2,v3 from emp
?????? ?? where empno=v1;
?????? ?? if v2<2000 then
?????? ???? Begin
?????? ?????? update emp set sal=sal+100
?????? ??????? where empno=v1;
????????????? dbms_output.put_line('員工:'||v3||' 工資已經(jīng)增加!');
?????? ???? End;
?????? ?? else dbms_output.put_line('員工:'||v3||' 工資已經(jīng)超過規(guī)定值,不增加!');
?????? ?? end if;
??????? End;
?
?
??? 三、循環(huán)語句
?????? Declare
???????? i int :=0;
?????? Begin
???????? loop
?????? ??? i:=i+1;
?????? ??? dbms_output.put_line('i的當(dāng)前值為i='||i);
?????? ??? exit when i=10;
?????? ?end loop;
?????? End;
?
?????? Declare
???????? i int :=0;
?????? Begin
???????? while i<10 loop
?????? ??? i:=i+1;
?????? ??? dbms_output.put_line('i的當(dāng)前值為i='||i);
?????? ?end loop;
?????? End;
?
?????? Declare
???????? i int :=0;
?????? Begin
???????? for i in 1..10 loop
?????? ??? dbms_output.put_line('i的當(dāng)前值為i='||i);
?????? ?end loop;
?????? End;
?
?? 四、光標(biāo)
????? 查詢結(jié)果一對多(多條記錄)的情況,需要光標(biāo)。
????? Declare
???????? v1 number(4);
?????? ?v2 varchar2(20);
?????? ?v3 number(7,2);
?????? ?cursor c is select empno,ename,sal from emp;
????? Begin
???????? open c;
?????? ?loop
?????? ?? fetch c into v1,v2,v3;
?????? ?? if v3<3000 then
?????? ???? Begin
?????? ?????? update emp set sal=sal+100 where empno=v1;
?????? ?????? dbms_output.put_line(v2||'工資已經(jīng)增加!');
?????? ???? End;
?????? ?? end if;
?????? ?exit when c%notfound;
?????? ?end loop;
?????? ?close c;
?????? End;
?????? ???
?????? 題目:
?????????? 請將EMP表中工資最高的前幾名員工姓名及工資值查詢出來,存儲到基表topsal中。要求:
?????? ?? 1、基表topsal結(jié)構(gòu)如下
?????? ????? sql>Create table topsal(
?????? ????????? name varchar2(20),
????????????? ? sal number(7,2));
?????????? 2、員工個數(shù)由鍵盤輸入確定。
?
?????? ?? Declare
?????? ???? i? int:=&i;
?????? ???? j? int :=0;
?????? ???? v2 varchar2(20);
?????? ???? v3 number(7,2);
?????? ???? cursor c is select ename,sal from emp order by nvl(sal,0) DESC;
?????? ?? Begin
?????? ???? open c;
?????? ???? loop
?????? ?????? j:=j+1;
?????? ?????? fetch c into v2,v3;
?????? ?????? insert into topsal
?????? ?????? values (v2,v3);
?????? ?????? exit when (i=j or c%notfound);
?????????? ??end loop;
?????? ???? close c;
?????? ??? End;
?????? ??? 空值永遠(yuǎn)在前面排。
???????????
?????? ??? 光標(biāo)變量使用方法:
?????? ??? Declare
?????? ????? type row_cursor is ref cursor;
?????? ????? c row_cursor;
?????? ????? r emp%rowtype;
?????? ??? Begin
?????? ????? open c for select * from emp;
????????????? loop
?????? ??????? fetch c into r;
??????????????? dbms_output.put_line(r.ename||'--'||r.sal||'--'||r.hiredate);
????????????? if c%notfound then exit;
????????????? end if;
?????? ?????? end loop;
?????? ????? close c;
?????? ???? End;
?????? ???
?????? ??? 用于光標(biāo)的for循環(huán)語句:
?????? ??? Declare
?????? ????? cursor c is select empno,ename,sal from emp;
?????? ??? Begin
?????? ????? for i in c loop
?????? ??????? dbms_output.put_line(i.empno||'--'||i.ename||'--'||i.sal);
?????? ????? end loop;
?????? ??? End;
?????? ??????
?????? ??? Declare
?????? ????? cursor c is select ename,sal from emp
?????? ????? for update of sal nowait; --鎖住
?????? ??? Begin
?????? ????? for i IN c loop
??????????????? if i.sal<2000 then
????????????? ?? Begin
????????????? ???? update emp set sal=i.sal +100
????????????? ???? where current of c;? --光標(biāo)當(dāng)前行
????????????? ???? dbms_output.put_line('員工:'||i.ename||'已經(jīng)增加工資!');
????????????? ?? End;
????????????? ?end if;
?????? ????? end loop;
?????? ??? End;
?
??????????? Declare
?????? ????? v_empno emp.empno%type=:=&empno;
?????? ??? Begin
?????? ????? delete from emp
?????? ????? where empno=v_empno;
?????? ????? if sql%NOTFOUND then
?????? ???????? dbms_output.put_line('刪除失敗,無編碼為'||v_empno||'的員工!');
?????? ????? else
?????? ???????? dbms_output.putline('成功刪除編碼為'||v_empno||'的員工!');
?????? ????? end if;
?????? ??? End;
?
2006-4-8
?? 五、例外處理(Exception)
????????????? Exception
?????? ??????? when NO_DATA_FOUND then
????????????? ?? dbms_output.put_line('無這樣的員工');
????????????? when TOO_MANY_ROWS then
????????????? ?? dbms_output.put_line('查詢結(jié)果出現(xiàn)多行,請定義光標(biāo)!');
?????? ??????? when VALUE_ERRORS then
????????????? ?? dbms_output.put_line('員工編碼為4位數(shù)!');
????????????? when INVALID_NUMBER then
????????????? ?? dbms_output.put_line('員工位數(shù)!');
????????????? when OTHERS then
????????????? ?? dbms_output.put_line('程序運(yùn)行出錯!');
?
????????????? ?? 有例外時,附值要在Begin后。
?????? ?
??????? Declare
?????????? v1 emp.empno%type;
?????? ?? v2 number(7,2);
?????? ?? v3 varchar2(20);
??????? Begin
?????? ?? v1:=&empno;
?????? ?? select sal,ename into v2,v3 from emp
?????? ?? where empno=v1;
?????? ?? if v2<2000 then
?????? ???? Begin
?????? ?????? update emp set sal=sal+100
?????? ??????? where empno=v1;
????????????? dbms_output.put_line('員工:'||v3||' 工資已經(jīng)增加!');
?????? ???? End;
?????? ?? else dbms_output.put_line('員工:'||v3||' 工資已經(jīng)超過規(guī)定值,不增加!');
?????? ?? end if;
?????? ?? Exception
?????? ??????? when NO_DATA_FOUND then
????????????? ?? dbms_output.put_line('無這樣的員工');
????????????? when TOO_MANY_ROWS then
????????????? ?? dbms_output.put_line('查詢結(jié)果出現(xiàn)多行,請定義光標(biāo)!');
?????? ??????? when VALUE_ERROR then
????????????? ?? dbms_output.put_line('員工編碼為4位數(shù)!');
????????????? when INVALID_NUMBER then
????????????? ?? dbms_output.put_line('NUMBER型非法!');
????????????? when OTHERS then
????????????? ?? dbms_output.put_line('程序運(yùn)行出錯!');
??????? End;
?
?????? 用戶自定義例外的使用方法:
?????? A、定義例外
?????? B、使用raise引起
?????? C、作為Exception統(tǒng)一使用
?
?????? 試指出以下語句是否有錯誤?
?????? 錯誤用法:
?????? Declare
?????? ? v1 emp.empno%type :=&empno;
?????? Begin
?????? ? delete from emp where empno=v1;
?????? ? Exception
?????? ??? when SQL%NOTFOUND then? --SQL%NOTFOUND是屬性,不能作為例外,但開始使用用戶自定義例外。
?????? ?????? dbms_output.put_line('為無編碼'||v1||'的員工!');
?????? End;
??????? 正確用法:
??????? Declare
?????? ? v1 emp.empno%type;
?????? ??if_delete exception;
?????? Begin
?????? ? v1:=&empno;
?????? ? delete from emp where empno=v1;
?????? ? if SQL%NOTFOUND then raise if_delete;
?????? ? end if;
?????? ? Exception??
?????? ??? when if_delete then?
?????? ?????? dbms_output.put_line('為無編碼'||v1||'的員工!');
?????? ??? when VALUE_ERROR then
????????????? ?? dbms_output.put_line('員工編碼為4位數(shù)!');
??????????? when others then
?????? ?????? dbms_output.put_line('為無編碼'||v1||'的員工!');
?????? End;
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
第七章? 存儲過程與函數(shù)??????????????????????? 回目錄
?? 一、什么存儲過程(函數(shù))
?????? 存儲過程(函數(shù))就是把一個PL/SQL程序塊存到數(shù)據(jù)庫中,作為一個數(shù)據(jù)庫實(shí)體,可以在其他PL/SQL程序、過程中調(diào)用。
?? 二、存儲過程的調(diào)用:
????? 在SQL*PLUS中調(diào)用
??????? sql>exec 過程名;
????? 在PL/SQL及過程中調(diào)用
??????? 過程名;
?? 三、創(chuàng)建存儲過程的語法:
????? IN
????? Create or Replace procedure DelEmp(v1 number) is
????? Begin
??????? delete from emp where empno=v1;
?????? if sql%found then
?????? ?? dbms_output.put_line('編碼:'||v1||'的員工成功刪除!');
??????? else
?????? ?? dbms_output.put_line('無編碼為:'||v1||'的員工!');
?????? end if;
?????? End DelEmp;
?
?????? 在SQL*PLUS中調(diào)用存儲過程:
?????? sql>exec EdlEmp(7788);
?????? 在PL/SQL中調(diào)用:
?????? Begin
???????? DelEmp(7923);
?????? ?DelEmp(7369);
?????? End;
?
????? 查詢存儲過程:
????? sql>select text from user_source
????????? where name='DELEMP';
?
????? OUT
????? Create or Replace Procedure QueryEmp(v1 number,v2 out varchar2,v3 out number)
????? is
?????? Begin
???????? select ename,sal into v2,v3 from emp
?????? ?where empno=v1;
???????? Exception
?????????? when NO_DATA_FOUND then
?????? ???? dbms_output.put_line('無編碼為:'||v1||'的員工!');
?????? End QueryEmp;
?
?????? 調(diào)用方法:
???????? Declare
?????? ?? v1 varchar2(20);
?????? ?? v2? number;
???????? Begin
?????? ?? QueryEmp(7369,v1,v2);
?????????? dbms_output.put_line(v1||'=='||v2);
?????? ?End;
?
??????? IN OUT
?????? Create or Replace Procedure ChangeName(
?????? ?? v_name in out varchar2) is
?????? Begin
?????? ?? v_name:=SubStr(v_name,1,3);
?????? End ChangeName;
?????? 調(diào)用方法:
?????? Declare
?????? ? v_name varchar2(20);
?????? Begin
?????? ? v_name:='SMITH';
?????? ? ChangeName(v_name);
?????? ? dbms_output.put_line(v_name);
?????? End;
?
??????? 存儲過程權(quán)限管理:
?????? sql>grant execute on DelEmp to 用戶名; ---使用的時候要加用戶名前綴。
?????? 刪除存儲過程:
?????? sql>drop procedure 過程名;
?????? 收回權(quán)限:
?????? sql>revoke execute on DelEmp from 用戶名;
?????? 查詢存儲過程權(quán)限:
??????? sql>select owner,name,type
?????? ??? from all_source
?????? ??? where owner='SCOTT'
?????? ????? AND type='PROCEDURE';
?
?
??? 四、函數(shù)(Function)
?????? Create or Replace Function GetSal(v1 number)
?????? return number
?????? is
???????? v_sal emp.sal%type;
?????? Begin
???????? select sal into v_sal from emp where empno=v1;
?????? ?return v_sal;
?????? End GetSal;
?????
?????? 函數(shù)的調(diào)用:
?????? sql>select GetSal(7369) from dual;
?????? sql>exec dbms_output.put_line(GetSal(7369));
?
?????? 題目: 試編寫一個n!的函數(shù)。
?????? Create or Replace Function fn(n number)
?????? return number
?????? is
???????? v_h number;
?????? ?v_i number;
?????? Begin
???????? v_i:=1;
?????? ?v_h:=1;
???????? for v_i in 1..n loop
?????? ??? v_h:=v_h*v_i;
?????? ?end loop;
???????? return v_h;
?????? End A1;
?
?????? Create or Replace Function fn(n number)
?????? return number
?????? is
?????? Begin
???????? if n=1 then
?????? ?? return 1;
???????? else
?????????? return n*fn(n -1);
???????? end if;
?????? End A2;
?
?
??? 五、包(Package)
????? 1、什么是包
???????? 把一些相關(guān)的存儲過程、函數(shù)、光標(biāo)、例外等組合在一起形成包,包由包的說明及包體組成,定義在包的說明部分的元素
????? 叫做公共元素,定義在包體部分的元素叫私有元素。
????? 2、包的優(yōu)點(diǎn)
???????? 規(guī)范化程序設(shè)計
?????? ?便于過程及函數(shù)的管理:整個包的授權(quán)只需一次完成
?????? ?包作為一個整體一次性調(diào)入內(nèi)存,減少I/O
????? 3、創(chuàng)建包的語法
???????? 刪除包:
?????? ?sql>drop package 包名;
?????? ?sql>drop package body 包體;
?
?
?
?
?
第八章? 動態(tài)SQL?????????????????????? 回目錄
?
??? 一、什么是動態(tài)SQL
?????? 在應(yīng)用程序中動態(tài)創(chuàng)建基表、視圖、索引等
?????? 動態(tài)生成SQL、DML語句
?????? 在PL/SQL中使用DDL語句
?
??? 二、動態(tài)SQL應(yīng)用實(shí)例
?????? 1、動態(tài)創(chuàng)建基表:
????????? Declare
?????? ??? v_table varchar2(20):='&v_table';? --在此加單引號,輸入的時候就不用再加了。
?????? ??? stm varchar2(200);
?????? ? Begin
?????? ??? stm:='Create table '||v_table||'(
?????? ????????? id number(7),
????????????? ? name varchar2(20),
????????????? ? sal number(7,2))';
??????????? execute immediate stm;
?????? ? End;
??????? 2、動態(tài)刪除基表
?????? ?? Declare
?????? ???? v_table varchar2(20):='&v_table';
?????? ???? stm varchar2(200);
?????????? Begin
?????? ??? stm:='Drop table '||v_table;
??????????? execute immediate stm;
?????? ??? dbms_output.put_line('表'||v_table||'已經(jīng)被刪除!');
?????? ?? End;
??????? 3、動態(tài)生成SQL
?????? ?? Create or Replace procedure TableRows(
?????? ?? table_name varchar2,
?????? ?? wherestr varchar2:=null) is
?????? ?? rownums number;
?????? ?? stm varchar2(200);
?????? ?? Begin
?????? ???? stm:='select count(*) from '||table_name||' where '||nvl(wherestr,'2=2');
?????? ???? execute immediate stm into rownums;
????????? ???dbms_output.put_line('表'||table_name||'有'||rownums||'行符合要求');
?????? ?? End TableRows;
??????????
?????? ?? sql>exec TableRows('EMP')
?????? ?? sql>exec TableRows('EMP','sal>1500')
?????? ?? sql>exec TableRows('EMP','deptno=10')
?
?????? ?4、動態(tài)生成QL語句
?????? ??? 多行查詢語句:使用光標(biāo)
?????? ????? Create or Replace procedure DisEmp(
?????? ??????????????? wherestr varchar2:=null)
?????? ????? is
?????? ??????? type c is ref cursor;
????????????? r_c c;
?????? ??????? v_empno emp.empno%type;
?????? ??????? v_ename emp.ename%type;
?????? ??????? v_job emp.job%type;
?????? ??????? stm varchar2(200);
?????? ????? Begin
?????? ??????? stm:='select empno,ename,job from emp where '||nvl(wherestr,'2=2');
????????????? open r_c for stm;
????????????? loop
????????????? ? fetch r_c into v_empno,v_ename,v_job;
????????????? ? exit when r_c%notfound;
????????????? ? dbms_output.put_line(v_empno||'\'||v_ename||'\'||v_job);
????????????? end loop;
????????????? close r_c;
?????????????? End;
?
????????? sql>exec DisEmp('sal>1500')
?????? ? sql>exec DisEmp
????????
????????????? Create or Replace procedure DisEmp(
?????? ??????????????? wherestr varchar2:=null)
?????? ????? is
?????? ??????? type c is ref cursor;
????????????? r_c c;
?????? ??????? r1 emp%rowtype;
?????? ??????? stm varchar2(200);
?????? ????? Begin
?????? ??????? stm:='select * from emp where '||nvl(wherestr,'2=2');
????????????? open r_c for stm;
????????????? loop
????????????? ? fetch r_c into r1;
????????????? ? exit when r_c%notfound;
????????????? ? dbms_output.put_line(r1.empno||'\'||r1.ename||'\'||r1.job);
????????????? ? end loop;
????????????? close r_c;
?????????????? End;
?
?
??????? 4、動態(tài)生成DML語句
?????? ?? Create or Replace Procedure UpdateTable(
?????? ???? table_name in varchar2,
?????? ???? column_name in varchar2,
?????? ???? column_value in varchar2,
?????? ???? wherestr in varchar2:=null) is
?????? ??? stm varchar2(200);
?????? ??? Begin
?????? ????? stm:='update '||table_name||' set '||column_name||'='||column_value||' where '||nvl(wherestr,'1=1');
?????? ????? execute immediate stm;
?????? ????? dbms_output.put_line('更新行數(shù):'||sql%rowcount);
?????? ??? End UpdateTable;
?
?????? ??? sql>exec UpdateTable('EMP','sal',2000,'sal<1000');
?
???????? 觸發(fā)器:補(bǔ)充
?????? ??? 語句級觸發(fā)器:只執(zhí)行一次。 --for each row? --行級
?????? ??? Create or Replace trigger ChangeEmp
?????? ??? before insert or update or delete on emp
?????? ??? Begin
?????? ????? if(to_char(sysdate,'dy') in ('星期六','星期日') or
?????? ??????? to_number(to_char(sysdate,'hh24'))
????????????? not between 8 and 18)
?????? ????? then
?????? ???????? Raise_Application_Error(-20500,'非工作時間,不得更改數(shù)據(jù)!');
?????? ????? end if;
?????? ??? End ChangeEmp;
?????????
?????? ? Oracle錯誤代碼范圍:ORA-20000 至 ORA-2009999;
?
?????? ? 系統(tǒng)事件觸發(fā)器:
?????? ? 系統(tǒng)事件指:after logon,before logoff,after startup,before shutdown
????????? sysdba:
?????? ? sql>Create table log_tab(
?????? ????? user_name varchar2(20),
?????? ????? log date,
?????? ????? log_info varchar2(20));
?????? ? sql>Create or Replace trigger logon_trigger
?????? ????? after logon on database
?????? ????? Begin
?????? ??????? insert into log_tab values(user,sysdate,'成功登陸');
?????? ????? End;
?????? ? sql>Create or Replace trigger logoff_tirgger
?????? ????? before logoff on database
?????? ????? Begin
?????? ???????? insert into log_tab values(user,sysdate,'退出系統(tǒng)');
?????? ????? End;
?
?????? ? DDL觸發(fā)器:
?????? ?? 觸發(fā)事件: Create,Alter,Drop
?????? ?? sql>Create table ddl_tab(
?????? ?????? user_name varchar2(20),
?????? ?????? ddl_date date,
?????? ?????? ddl_info varchar2(20));
?????? ?? sql>Create or Replace trigger Create_trigger
?????? ?????? before create on database
?????? ?????? begin
?????? ???????? insert into ddl_tab values(user,sysdate,'創(chuàng)建基表');
?????? ?????? End;
?????? ?? sql>Create or Replace trigger Alter_trigger
?????? ?????? before alter on database
?????? ?????? begin
?????? ???????? insert into ddl_tab values(user,sysdate,'修改基表結(jié)構(gòu)');
?????? ?????? End;
?????? ??? sql>Create or Replace trigger Drop_trigger
?????? ?????? before Drop on database
?????? ?????? begin
?????? ???????? insert into ddl_tab values(user,sysdate,'刪除基表');
?????? ?????? End;
2006-4-9
??????????? (DBA)(Database Administrator)
?
???????????????? Oracle9i 數(shù)據(jù)庫體系結(jié)構(gòu)
????????????? ?物理結(jié)構(gòu)
????????????? ?邏輯結(jié)構(gòu)
????????????? ?內(nèi)存結(jié)構(gòu)
????????????? ?進(jìn)程結(jié)構(gòu)
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?????????????????? 第二部分? 體系結(jié)構(gòu)/數(shù)據(jù)庫管理?????????????????? 回目錄
第一章 Oracle9i 體系結(jié)構(gòu)與物理結(jié)構(gòu)管理
???
?? 一、數(shù)據(jù)庫物理結(jié)構(gòu)
????? 數(shù)據(jù)庫是由構(gòu)成數(shù)據(jù)庫的操作文件所組成。
????? c:\oracle\oradata\db_name\*.*???????? --windows 安裝時的選擇
????????
????? /u01/app/oracle/oradata/db_name/*.*?? --unix
?
????? 每一個數(shù)據(jù)庫物理結(jié)構(gòu)文件包括:數(shù)據(jù)文件、日志文件、控制文件及參數(shù)文件。
?
??? 二、數(shù)據(jù)文件管理(Data Files)
?????? 存儲數(shù)據(jù)庫數(shù)據(jù)的文件
?????? 查詢數(shù)據(jù)文件信息:
?????? sql>select bytes,blocks,file_name
?????????? from dba_data_files;
?????? 查看臨時文件信息:
?????? sql>select bytes,blocks,file_name
?????????? from dba_temp_files;
?????? Oracle數(shù)據(jù)文件不顯示數(shù)據(jù)大小,只顯示空間大小。
?????? 查看空閑空間大小:
?????? sql>select tablespace_name,sum(bytes)
?????????? from dba_free_space
?????? ?? group by rollup(tablespace_name);
?????? 查看臨時表空間空閑空間
?????? sql>select * from v$temp_space_header;? ---Oracle9i
?
?????? 數(shù)據(jù)塊(Data Blocks):db_block_size=2k~32k
?????????????????????????? (2k,4k,8k,16k,32k)
?????? 默認(rèn)值如下:
???????? Oracle7,Oracle8,Oracle8i 8.1.5?????? ????2k 2048
?????? ???? Oracle8i 8.1.6/8.17????????????????? ???8k 8192
?????? ???? Oracle9i 9.0.1?????????????????????? ??4k 4096
?????? ???? Oracle9i 9.2.0?????????????????????? ??8k 8192
?????? ???? Oracle10g??????????????????????????? 8k 8192
?
?????? 查詢數(shù)據(jù)塊大小:
?????? sql>show parameter db_block_size;
?????? A、修改數(shù)據(jù)文件大小
????????? sql>alter database datafile '數(shù)據(jù)文件名及路徑'
?????? ????? resize xxM;
?????? ? $df -k?? --unix下看空間大小
????????? sql>alter database datafile 'D:\ORACLE\ORADATA\HF\SYSTEM01.DBF'
?????? ????? resize 500M;
?
?????? B、數(shù)據(jù)文件自動擴(kuò)展屬性(Autoextend)
????????? 查詢數(shù)據(jù)文件自動擴(kuò)展屬性
?????? ? sql>select bytes,autoextensible,maxbytes,file_name
?????? ????? from dba_data_files;
?????? ? 修改數(shù)據(jù)庫自動擴(kuò)展屬性
?????? ? sql>alter database datafile '數(shù)據(jù)文件名及路徑'
?????? ????? autoextend on;?? ---打開數(shù)據(jù)文件自動擴(kuò)展屬性。
?????? ? sql>alter database datafile '數(shù)據(jù)文件名及路徑'
?????? ????? autoextend off;? ---關(guān)閉數(shù)據(jù)文件自動擴(kuò)展屬性。
?????? ?
?????? ? 設(shè)置數(shù)據(jù)文件每次擴(kuò)展大小:
?????? ? sql>alter database datafile "數(shù)據(jù)文件名及路徑'
?????? ????? autoextend on next 100M maxsize 2000m;? --unlimited 無限
?????? ? sql>select bytes,increment_by,file_name
?????? ????? from dba_data_files;
?
?????? ? create table sa(
?????? ? s1 char(20)) storage(initial 80m) tablespace users; ---空表初始80m.
???
??? 三、日志文件管理(Redo Log Files)
??????? 存儲數(shù)據(jù)庫修改前后信息。用于數(shù)據(jù)庫恢復(fù)。--必須要有備份和日志。
?????? 每個數(shù)據(jù)庫實(shí)例至少有兩個日志文件組,每組有一個或多個日志成員。
??????? sql>select log_mode from v$database;
?????? sql>archive log list;
??????? 查詢在線日志信息:
?????? sql>select * from v$log;
?????? 查詢?nèi)罩疚募畔?#xff1a;
?????? sql>select * from v$logfile;
?????? 查詢歸檔日志:
?????? sql>select * from v$archived_log; 在日志切換時,寫到磁盤或磁帶上的日志。
?????? 查詢歷史日志:
?????? sql>select * from v$log_history;
?????? sql>alter session set nls_date_format='yyyy.mm.dd h24:mi:ss';
?
?????? 日志文件不宜太小,一般幾十兆即可。
?????? 日志成員最少要有2個,9i里面缺省最多5個。
??????? DBA強(qiáng)制日志切換命令:
?????? sql>alter system switch logfile;
?????? A、增加日志文件組命令:
?????? ?? sql>alter database add logfile group n
?????? ?? ('第一個日志成員文件名及路徑',
?????? ??? '第二個日志成員文件名及路徑',,,) size xxM;
?
?????? ?? sql>alter database add logfile group 4(
?????????????? 'd:\oracle\oradata\hf\redo41.log',
?????????????? 'd:\oracle\oradata\hf\redo42.log')
?????????????? size 10m;
?
?????? B、刪除日志文件組:
?????? ?? sql>alter database drop logfile group n;
?????? ?? 注意:當(dāng)前組Current,Active組不得刪除
?????? ??????? 數(shù)據(jù)庫只有兩個組時,不得刪除
????????????? 先邏輯刪除,后物理刪除
?
??????????? sql>select * from v$logfile;
?
?????? C、增加日志成員文件:
?????? ?? sql>alter database add logfile member
?????? ?????? '日志成員文件名及路徑','日志成員文件名及路徑' to group n;
?
?????? ?? sql>alter database add logfiel group 1
?????? ?????? ('日志成員文件名及路徑') reuse;???? --邏輯文件拿掉了,物理文件未刪除。
??????? D、刪除日志成員文件:
?????? ?? sql>alter database drop logfile member
?????? ?????? '日志成員文件名及路徑';
?????? ?? 注意:Current組中成員不得刪除
?????? ?? ?????組中只有一個成員時,不得刪除成員
????????????? 先邏輯刪除,后物理刪除。
?
???????? 題目:請將系統(tǒng)默認(rèn)安裝產(chǎn)生的3個日志文件的大小全部由100M改為1M。
?
?? 四、參數(shù)文件
????? 在Oracle9i數(shù)據(jù)庫中,可以使用兩個不同類型的參數(shù)文件,一個為文本類型Pfile,一個為服務(wù)器類型Spfile。
????? Spfile默認(rèn)存儲路徑
????? d:\oralce\ora92\database\spfileSID.ora? --windows
????? /u01/app/oracle/product/9.2.0/dbs/spfileSID.ora? ---unix
????? Spfile修改方式:
????? sql>alter system set db_cache_size=8m
????????? comment='Changed By TYC,2006.04.09';
?
????? Pfile默認(rèn)存儲路徑
????? d:\oralce\admin\db_name\pfile\init.ora??????? --windows
????? /u01/app/oracle/admin/db_name/pfile/initSID.ora? --Unix
?
????? 建立連接關(guān)系:
????? 創(chuàng)建initSID.ora
????? d:\oracle\ora92\database\initSID.ora=>
????? 增加下面一行:
????? ifile='d:/oracle/admin/db_name/pfile/init.ora'
?
????? Nuinx:
????? /u01/app/oracle/product/9.2.0/dbs/initSID.ora
????? ln:
????? /u01/app/oracle/admin/db_name/pfile/initSID.ora
?
????? 兩個參數(shù)文件同時存在時,系統(tǒng)啟動時,優(yōu)先使用Spfile,如果未找到Spfile,則以Pfile啟動。
????? 查詢數(shù)據(jù)庫所使用的參數(shù)文件:
????? sql>show parameter spfile;
????? 兩個參數(shù)文件可以相互轉(zhuǎn)換
????? sql>Create spfile from pfile;
????? sql>Create pfile from spfile;
?
????? c:\oradim -startup -sid hf?????? ---在操作系統(tǒng)中啟動數(shù)據(jù)庫
?
?? 五、控制文件(Control Files)
????? 控制文件用于存儲數(shù)據(jù)庫結(jié)構(gòu)
????? 查詢警告文件信息,了解修改數(shù)據(jù)庫結(jié)構(gòu)的操作:
????? D:\oracle\admin\db_name\bdump\alert_SID.log
?
????? 控制文件的主要信息如下:
??????? 數(shù)據(jù)庫名稱
??????? 數(shù)據(jù)庫創(chuàng)建的時間
??????? 數(shù)據(jù)庫中全部日志文件及路徑、全部數(shù)據(jù)文件及路徑
?????? 數(shù)據(jù)庫恢復(fù)時所需同步信息
?????? 查詢數(shù)據(jù)文件同步號:
?????? sql>select file#,checkpoint_change#,name
?????? ??? from v$datafile;
?????? 查詢控制文件同步號:
?????? sql>select checkpoint_change# from v$database;
?
????? 控制文件鏡象方法:
????? A、關(guān)閉數(shù)據(jù)庫
????? B、復(fù)制控制文件
????? C、改參數(shù)文件
????? D、重啟數(shù)據(jù)庫
?
????? 查看控制文件:
? ????select * from v$controlfile;
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
????? 2006-4-10???? 第二章? Oracle9i邏輯結(jié)構(gòu)管理??????????????????????? 回目錄
?? 數(shù)據(jù)庫邏輯結(jié)構(gòu)組成:
????? 表空間(Tablespaces)
????????????????????????? 數(shù)據(jù)段: 存儲基表數(shù)據(jù)的段 --私有
???????????????????????? /????
????? 段(Segments)? ------ 索引段: 存儲索引數(shù)據(jù)的段 --私有
???????????????????????? \
???????????????????? ?? 臨時段:用于排序?? ---公有
???????????????????????? \
???????????????????? ?? 回退段:用于事務(wù)的回退 --- 公有
?
????? 區(qū)(Extents)
????? 數(shù)據(jù)塊(Data Blocks)
?
?? 一、表空間(Tablespaces)
????? 控制數(shù)據(jù)庫數(shù)據(jù)磁盤分配
????? 表空間與數(shù)據(jù)文件相對應(yīng),表空間大小等于構(gòu)成該表空間的所有數(shù)據(jù)文件大小和。??
????? 查詢表空間與數(shù)據(jù)文件對應(yīng)關(guān)系:
????? sql>select tablespace_name,bytes,file_name
????????? from dba_data_files;
????? 表空間屬性:read only,read write,online,offline
????? 修改表空間:
????? sql>alter tablespace users read only;
????? sql>select tablespace_name,status
????????? from dba_tablespaces;
????? 其中:System,Undotbs1,Temp 不得設(shè)置為read only,offilne
????
??? 二、段(Segments)
?????? A、數(shù)據(jù)段:存儲基表數(shù)據(jù)的段。
????????? 查詢表與該表的表空間對應(yīng)關(guān)系:
????????? sql>select table_name,tablespace_name
?????? ????? from user_tables;
?????? ? 將表從一個表空間移動到另外一個表空間:
?????? ? sql>alter table dept move tablespace users;
?
????????? 創(chuàng)建基表時,可以指定表空間:
?????? ? sql>Create table t_data(
?????? ????? tname varchar2(20),
?????? ????? tdate date) tablespace users;
?????? ? sql>select username,default_tablespace
?????? ????? from dba_users;
?
?????? B、索引段:存儲索引數(shù)據(jù)的段。
?????? ? 查詢索引與存儲該索引表空間對應(yīng)關(guān)系:
?????? ? sql>select index_name,table_name,tablespace_name
?????? ????? from user_indexes;
?????? ? 在創(chuàng)建索引時,可以指定表空間:
?????? ? sql>Create index d_dname on dept(dname)
?????? ????? tablespace indx;
?????? ??
?????? C、臨時段:用于排序
?????? ?? Oracle9i 后,只能使用臨時表空間來排序。
?????? ?? 查詢臨時表空間:
?????? ?? sql>select tablespace_name,contents
?????? ?????? from dba_tablespaces;
?????????? 查詢用戶所使用的臨時表空間:
?????? ?? sql>select username,temporary_tablespace
?????? ?????? from dba_users;
?????? ?D、回退段:用于事務(wù)管理
?????? ??? Oracle9i后由Oracle自己管理。
?????? ??? 查詢回退段信息:
?????? ??? sql>select * from dba_rollback_segs;
?????? ??? oralce 的事務(wù)由事務(wù)終結(jié)來分,Transaction -----> 回退段。 如:
??????????? sql>delete from --
?????? ??? sql>update ---
?????? ??? sql>create table ---
?????? ??? sql>insert ---
?????? ??? sql>alter ---
?????? ??? 以上有兩個事務(wù),設(shè)置兩個回退段。
?????? ??? SavePoint 點(diǎn):
?????? ????? ….
?????? ????? …
??????????? SavePoint a;
?????? ???? ?…
?????? ????? …
?????? ??? rollback to a;
?
??????????? 檢控回退段的狀態(tài):
?????? ??? sql>select * from v$rollstat;
?????? ??? sql>select * from v$rollname;
?
??? 三、區(qū)(Extents)
?????? 區(qū)是磁盤空間分配最小單位。一個段包含多個區(qū)。
?????? 查詢段與區(qū)的關(guān)系:
?????? sql>select extent_id,bytes,blocks
?????????? from user_extents
?????? ?? where segment_name='PAYMENT';
?????? 區(qū)大小設(shè)置參數(shù)(磁盤存儲參數(shù)):
?????? Initial:初始區(qū)大小? 8.0(2k) 8i(5*db_block_size) 9i(64k)
?????? Next:增長區(qū)大小???? 8.0(2k) 8i(5*db_block_size) 9i(64k)
?????? Minextents:區(qū)最小個數(shù)? 1
?????? Maxextents:區(qū)最大個數(shù)? 8.0(121) 8i(505) 9i(unlimit)
?????? Pctincrease:區(qū)增長百分比? 8.0(50) 8i(50) 9i(0)
?
?????? sql>Create table sales(
?????????? s1 char(20),
?????? ?? s2 char(20));
?????? 如果磁盤無限大,這張表能存多少數(shù)據(jù)?
?????? 這個表的缺省參數(shù):Storage(initial?????? 64k,
??????????????????????????? next?????????? 64k,
???????????????????? ??? minextents???? 1,
???????????????????? ??? maxextents???? unlimit,
???????????????????? ??? pctincrease??? 0);
?
?????? sql>Create table sales(
?????????? s1 char(20),
?????? ?? s2 char(20))storage(initial 200k next 400k minextents 2 maxextents 20000 prtincrrease 0);
?????? 查詢可以用于以下對象
??????? 表空間
??????? 回退段
??????? 表
??????? 索引
?????? 存儲參數(shù)優(yōu)先原則:實(shí)體級參數(shù)>表空間級參數(shù)
?
??? 四、數(shù)據(jù)塊(Data Blocks)
?????? 數(shù)據(jù)塊是數(shù)據(jù)文件存儲空間單位,是I/O最小單位
??????? 在Oracle9i中,可以使用標(biāo)準(zhǔn)數(shù)據(jù)塊及非標(biāo)準(zhǔn)數(shù)據(jù)塊,其大小為 2k,4k,8k,16k,32k。
?????? 標(biāo)準(zhǔn)塊大小由參數(shù)db_block_size設(shè)置,非標(biāo)準(zhǔn)塊在創(chuàng)建表空間時,由參數(shù)blocksize設(shè)置。
?????? 標(biāo)準(zhǔn)塊用于系統(tǒng)表空間及臨時表空間、非標(biāo)準(zhǔn)塊數(shù)據(jù)塊用于用戶數(shù)據(jù)表空間。
?????? 查詢表空間大小:
?????? sql>select tablespace_name,block_size
?????? ??? from dba_tablespaces;
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
第三章? Oracle9i 內(nèi)存結(jié)構(gòu)管理?????????????????????? 回目錄
??? 一、SGA(System Global Area)系統(tǒng)全局區(qū)
????? 1、數(shù)據(jù)緩沖區(qū)(Database Buffer Cache):存儲由磁盤數(shù)據(jù)文件讀入的數(shù)據(jù),全體用戶共同使用。
???????? 數(shù)據(jù)緩沖區(qū)的參數(shù):
?????? ?db_block_buffers (Oracle8i/8/7)
?????? ?db_cache_size??? (Oracle9i/10g)
?
?????? ?非標(biāo)準(zhǔn)塊緩沖區(qū)設(shè)置參數(shù):db_nk_cache_size(其中:n=2,4,8,16,32)
?????? ?在數(shù)據(jù)庫中,可以同時設(shè)置多個數(shù)據(jù)緩沖區(qū)。例如:
?????? ?db_cache_size=128m
?????? ?db_2k_cache_size=64m
?????? ?db_2k_cache_size=32m
?????? ?試判斷以下設(shè)置是否正確:
?????? ??? ...
??????????? db_block_size=8k????? --標(biāo)準(zhǔn)塊設(shè)置
?????? ??? db_4k_cache_size=8m
?????? ??? db_cache_size=128m
?????? ??? db_8k_cache_size=16m (x) 和標(biāo)準(zhǔn)塊設(shè)置重復(fù)
?????? ??? db_16k_cache_size=64m
?????? ??? ...???
?????? ???
?????? ? oracle資源占用以庫為單位,一個庫要占用資源50-57%,所以一臺機(jī)子最好只裝一個數(shù)據(jù)庫。
????????? 解決方法:同一個數(shù)據(jù)庫,多個表空間。 一個實(shí)例相當(dāng)于一個數(shù)據(jù)庫,oracle的表空間級相當(dāng)于sqlserver的數(shù)據(jù)庫級。
????????? 可以在表空間級定義用戶,密碼。
?
?????? 2、共享池(Shared Pool)
????????? 共享池用于對SQL、PL/SQL程序進(jìn)行語法分析及編譯。
?????? ? 共享池大小設(shè)置參數(shù):shared_pool_size
?
?????? 3、日志緩沖區(qū)(Redo Log Buffer)
????????? 存儲數(shù)據(jù)庫修改信息。
?????? ? 在日志緩沖區(qū)寫滿時,由LGWR(日志寫入程序)寫入磁盤日志文件組。
?????? ? 日志緩沖區(qū)大小設(shè)置:log_buffer
?????? ? 查詢?nèi)罩揪彌_區(qū)大小:
?????? ? sql>show parameter log_buffer
?????? ? 顯示SGA區(qū)大小:
????????? sql>show sga
?????? ? sql>show parameter sga_max_size
?????? ? sql>show parameter db
?????? ? sql>show parameter log
?????? ? sql>show parameter
?????? ? sql>select * from v$sga;
?????? ? sql>select * from v$sgastat;
??? 二、PGA(rogram Global Area)
??????? 程序全局區(qū)用于變量及程序設(shè)計
?????? 程序全局區(qū)設(shè)置參數(shù):pga_aggregate_target
?
??? 三、SORT AREA:排序區(qū):
??????? 用于order by,group by ,distinct等排序
??????? 排序區(qū)大小設(shè)置參數(shù):sort_area_size
?????? 內(nèi)存使用完后,ORALCE使用磁盤臨時空間,會造成臨時空間的增長。
?
??? 四、LARGE POOL大池:
??????? 用于Rman數(shù)據(jù)庫備份工具。
?????? 大池設(shè)置參數(shù):large_pool_size
?
??? 五、JAVA POOL:
?????? 用于Java語言
?????? 設(shè)置參數(shù):java_pool_size
?????
?????
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
第四章? Oralce9i實(shí)例及進(jìn)程結(jié)構(gòu)??????????? 回目錄
??? 一、什么是Oralce實(shí)例
?????? 每一個運(yùn)行的數(shù)據(jù)庫都與一個實(shí)例相聯(lián)系。數(shù)據(jù)庫啟動時將分配SGA并啟動后臺數(shù)據(jù)庫進(jìn)程。該SGA與后臺進(jìn)程合成Instance.
?????
????? Unix 下設(shè)置實(shí)例方法:
????? $CRACLE_SID=HF
????? $export ORACLE_SID
?
????? Windows下設(shè)置實(shí)例方法:
????? c:\>set oracle_sid=hf
?
??? 二、Oracle進(jìn)程結(jié)構(gòu)
?
????????????????????????????? 用戶進(jìn)程
????????????????? ??????????/
????????????????????? 多進(jìn)程 - 服務(wù)器進(jìn)程
??????????????????? /?????? \
????? Oracle 主要進(jìn)程?????????? 后臺進(jìn)程
??????????????????? \
????????????? ????? 單進(jìn)程
????? 查詢后臺進(jìn)程信息:
????? sql>select name,description
????????? from v$bgprocess;
???
??? 三、用戶進(jìn)程和服務(wù)器進(jìn)程
???????????? ????CBO (Cost_Based optionmizer)基于成本的規(guī)劃器
???????????? ??/
?????? 執(zhí)行計劃
???????????? ??\
?????? ???????????? RBO (rule_Based optionmizer)基于規(guī)則的規(guī)劃器
?
?
?????????? Oralce 的優(yōu)化 -- 減少分析和編譯
?????? ?
??? 四、后臺進(jìn)程
?????? A、DBWR 數(shù)據(jù)寫入進(jìn)程(Database Writer)
????????? 將數(shù)據(jù)由數(shù)據(jù)緩沖區(qū)寫入磁盤數(shù)據(jù)文件。
?????? ? 采取LRU(least Recently Used)最近最少使用原則,將最近數(shù)據(jù)保留在內(nèi)存,以減少I/O操作。
?????? ? DBWR進(jìn)程設(shè)置參數(shù):
?????? ? db_writer_processes=1~10 (Oracle8i)
?????? ???????????????????? =1~20 (Oracle9i/10g)
?????? ? dbwr_io_salvers
?????? B、LGWR
?????? C、ARCH:歸檔進(jìn)程。在日志切換時,將日志信息寫入磁盤或磁帶,用于數(shù)據(jù)庫恢復(fù)。
????????? ARCH進(jìn)程設(shè)置參數(shù):log_archive_start=ture|false
?????? ???????????? 默認(rèn):false
?????? ? sql>show log_archive_start
?????? D、CKPT:檢驗(yàn)點(diǎn)進(jìn)程(checkpoint)
????????? DBA強(qiáng)制產(chǎn)生檢驗(yàn)點(diǎn)命令:
?????? ? sql>alter system checkpoint;
?????? ? sql>show parameter log_checkpoint_timeout
????????? sql>show parameter log_checkpoint_interval
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
第五章 Oracle9i表空間管理???????????????????? 回目錄
一、Oracle9i表空間概述
?? 查詢數(shù)據(jù)庫中的碎片:
?? sql>select * from dba_free_space;
二、表空間分類
?? 1、按照表空間中區(qū)的管理方式不同,表空間分為數(shù)據(jù)字典管理類型及本地化管理類型。
????? 查詢表空間中區(qū)的管理方式:
????? sql>select tablespace_name,extent_management,allocation_type
????????? from dba_tablespaces;
?
????? 其中:?? UNIFORM --每個區(qū)的大小一樣。
????????????? SYSTEM? --每個區(qū)有數(shù)據(jù)庫管理,動態(tài)分配。
2006-4-11
?
?? 2、在數(shù)據(jù)字典管理表空間中,區(qū)的大小由參數(shù)initial,next,minextents,maxextents,pctincrease設(shè)置。
????? 在本地化管理表空間中區(qū)的管理分為uniform及autoallocate兩種。
?
?? 3、數(shù)據(jù)字典管理表空間主要用于Oracle8i及之前的數(shù)據(jù)庫;本地化管理表空間主要用于Oracle9i及以后版本數(shù)據(jù)庫中。
?
?三、創(chuàng)建數(shù)據(jù)字典管理表空間(Oracle7/8/8i)
??? sql>Create tablespace s_date datafile
??????? 'd:\oracle\oradata\ora8i\s_data01.dbf' size 100m
?????? default storage (initial 100k
?????? ???????????????? next??? 100k
???????????????????? ?minextents 1
???????????????????? ?maxextens 2000
???????????????????? ?pctincrease 0);
?四、創(chuàng)建本地化管理表空間(Oracle8i/9i/10g)
??? 1、Uniform:區(qū)大小一律相同
?????? sql>Create tablespace product datafile
?????????? 'd:\oracle\oradata\hf\prod01.dbf' size 100m
?????? ?? autoextend on next 100m maxsize 2000m,
?????? ?? 'd:\oracle\oradata\hf\prod02.dbf' size 100m
?????? ?? autoextend on next 100m maxsize 2000m
?????? ?? extent management local uniform size 100k;
?????? 查詢表空間情況:
?????? sql>select tablespace_name,extent_management,allocation_type
???? ??????from dba_tablespaces;
?????? sql>select tablespace_name,initial_extent,next_extent,min_extents,max_extents,pct_increase
?????????? from dba_tablespaces;
?????? 刪除表空間:
?????? sql>drop tablespace product including contents and datafiles;
?????? 如果表是空的可以直接刪除:
?????? sql>drop tablespace product including contents;? (Oracle8i只能這樣寫,并手工刪除文件)
?
??? 2、Autoallocate:表空間中區(qū)的大小由系統(tǒng)自動動態(tài)分配
?????? sql>Create tablespace account datafile
?????????? 'd:\oracle\oradata\hf\acc01.dbf' size 100m,
?????? ?? 'd:\oracle\oradata\hf\acc02.dbf' size 100m
?????? ?? extent management local autoallocate;
?????? sql>select maxbytes,file_name,bytes,tablespace_name
?????????? from dba_data_files;
?
?五、修改表空間:
??? 為表空間增加數(shù)據(jù)文件
??? sql>alter tablespace account add datafile
??????? 'd:\oracle\oradata\hf\acc03.dbf' size 100m
?????? autoextend on next 100m maxsize 2000m;
?
??? 數(shù)據(jù)文件移動:將數(shù)據(jù)文件從一個磁盤移動到另一個磁盤;
??? 移動步驟:
??? A、將表空間設(shè)置為offline
?????? sql>alter tablespace account offline;
??? B、物理移動
??? C、邏輯移動
?????? sql>alter tablespace account rename datafile
??????????? '原數(shù)據(jù)文件名及路徑' to '目標(biāo)數(shù)據(jù)文件名及路徑';
??? D、將表空間設(shè)置為online
?????? sql>alter tablespace account online;
?
?????? sql>startup restrict? --數(shù)據(jù)庫以限制方式運(yùn)行
?????? sql>grant restricted session to 指定用戶;
?
? 注意:System,undotbs1,temp數(shù)據(jù)文件不得移動
?
?六、創(chuàng)建Undo表空間(撤消表空間)
??? sql>Create undo tablespace undotbs datafile
??????? 'd:\oracle\oradata\hf\undo01.dbf' size 100m
?????? autoextend on next 100m maxsize unlimited
?????? extent management local autoallocate;
????
??? 設(shè)置參數(shù):undo_tablespace=undotbs
??? 重新啟動數(shù)據(jù)庫。
?
?
?
?
?
?
第六章?? Oralce9i數(shù)據(jù)庫用戶管理????????????????? 回目錄
?二、創(chuàng)建資源限制文件(Profile)
??? sql>alter system set resource_limit=true;
??? 創(chuàng)建資源限制文件:
??? sql>Create profile acc limit
??????? failed_login_attempts 2
?????? password_lock_time 1
?????? sessions_per_user 2
?????? idle_time 2;
?
三、創(chuàng)建用戶:
?? sql>Create user acc03 identified by acc03
?????? default tablespace users
?????? temporary tablespace temp
?????? profile acc
?????? quota 50m on users;? --如果不限制,這行不要
??? sql>grant connect,resource to acc03;
??? 為用戶解鎖:
??? sql>alter user acc03 account unlock;
?
?四、修改用戶:
??? 修改口令字:
??? sql>alter user acc03 indentified by '123';
??? 修改用戶缺省表空間
??? sql>alter user acc03 default tablespace example;
??? 取消資源限制:
??? sql>alter user acc03 profile default;
??? 取消空間使用限制
??? sql>alter user acc03 quota unlimited on users;
?
?五、刪除用戶
? ??sql>drop user acc03 | cascade;??? --cascade用在此用戶有實(shí)體。
?
?
?
?
?
?
?
?
第七章 Oracle9i數(shù)據(jù)庫備份與恢復(fù)??????????????? 回目錄
?一、數(shù)據(jù)庫歸檔模式
?二、數(shù)據(jù)庫歸檔模式的配置
??? 設(shè)置參數(shù):
??? log_archive_start=true
??? log_archive_dest=目錄名
??? log_archive_format=%t-%s.arc???? --日志存儲格式%t線程號 %s日志號
??? log_archive_duplex_dest=鏡像目錄名
?
??? sql>shutodwn immediate
??? sql>startup mount?????? --mount只打開控制文件,不打開數(shù)據(jù)庫文件。
??? sql>alter database archivelog;
??? sql>alter database open;
?
??? sql>select log_mode from v$database;
??? sql>archive log list
??? sql>alter system switch logfile
?
?三、物理備份
??? 1、完全數(shù)據(jù)庫脫機(jī)備份(ARCHIVELOG/NOARCHIVELOG)
??? 2、聯(lián)機(jī)部分?jǐn)?shù)據(jù)庫備份(ARCHIVELOG)
?????? A、確認(rèn)數(shù)據(jù)庫運(yùn)行在ARCHIVELOG模式
?????? B、設(shè)置要備份的表空間為備份狀態(tài)
????????? sql>alter tablespace 表空間 begin backup;
?????? C、操作系統(tǒng)備份相應(yīng)的數(shù)據(jù)文件
?????? D、設(shè)表空間結(jié)束標(biāo)志:
????????? sql>alter tablespace 表空間名 end backup;
?????? ? begin 和 end 為了下次從哪里恢復(fù)而做的標(biāo)記。期間的數(shù)據(jù)不保存。
?
?????? ? sql>Create tablespace account datafile
?????? ????? 'd:\oracle\oradata\hf\acc01.dbf' size 10m;
?????? ? sql>alter tablespace account begin backup;
?????? ? sql>alter tablespace account end backup;
?????? ? sql>Create table account(
?????? ????? s1 char(20)) tablespace account;
?????? ? sql>insert into account values('計算技術(shù)');
?????? ? sql>insert into account select * from account;
?
?????? ? 數(shù)據(jù)文件恢復(fù)命令:
?????? ? sql>alter database datafile '數(shù)據(jù)文件名及路徑'; --日志自動讀取
?????? ? 或
?????? ? sql>recover datafile '數(shù)據(jù)文件名及路徑';? --交互式,可以輸路徑
?????? ? sql>alter database open;
?
?????? ? 基于時間的數(shù)據(jù)庫恢復(fù):
?????? ? sql>alter database recover database until time '2006-04-11 13:00:00';
?????? ? 將數(shù)據(jù)庫向回倒。
?
?四、邏輯備份
??? c:\>exp scott/tiger tables=(dept,emp) file=d:1? --表方式卸出
??? c:\>exp scott/tiger file=d:\f2? --用戶方式
??? c:\>exp system/manager full=y file =d:\f3? --全部數(shù)據(jù)庫方式
?
??? 其他關(guān)鍵字:
????? C:\>exp scott/tiger direct=y file =d:\f2? --直接路徑卸出,不經(jīng)過內(nèi)存,直接從文件卸出,適合大表。
????? C:\>exp scoot/tiger tables=(emp) query=\"where sal>1500\" file=d:\f2 --條件卸出
????? C:\>exp system/manager full=y filesize=100m? --限制卸出文件大小
????? C:\>exp system/manager full=y inctype=complete? --為增量設(shè)基準(zhǔn)
????? C:\>exp system/manager full=y inctype=incremental?? --增量卸出
?
??? 實(shí)際中以物理備份為好,當(dāng)需要數(shù)據(jù)遷移時,用邏輯備份。邏輯備份不許要日志,但速度較慢。
?
?五、Rman恢復(fù)管理器
?? Unix中創(chuàng)建數(shù)據(jù)庫的圖形界面工具:
?? $dbca (Oracle9i/10g)
?? 1、創(chuàng)建恢復(fù)目錄
????? 創(chuàng)建Rman數(shù)據(jù)庫
????? 創(chuàng)建Rman用戶
????? sql>Create user rman identified by rman
????????? default tablespace users
????????? temporary tablespace temp;
????? sql>grant connect,resource,recovery_catalog_owner? to rman;
?
????? c:\>set oracle_sid=rman? --確定進(jìn)rman庫
????? 因?yàn)閛racle為rman庫默認(rèn)建立的rman用戶,限制太多。
????? 所以刪除默認(rèn)rman用戶,自己建立用戶。
????? sql>drop user rman cascade;
????? 創(chuàng)建用戶,授權(quán)給新用戶。
????? 創(chuàng)建恢復(fù)目錄
????? c:\>set oracle_sid=rman
????? c:\>rman catalog rman/rman
????? RMAN>Create catalog; (刪除命令:drop catalog) 在rman中建表,
????? c:\rman catalog rman/rman
?
??? 2、將Rman連接到目標(biāo)數(shù)據(jù)庫
????? c:\>set oracle_sid=hf
????? c:\>rman target sys/abcd catalog rman/rman@rman --target表示目標(biāo)庫,catalog表示rman庫,@表示連接到rman上
????? 注冊數(shù)據(jù)庫:在Rman恢復(fù)目錄中寫入目標(biāo)庫信息。
????? RMAN>register database;
????????? >report schema;? 應(yīng)該顯示目標(biāo)數(shù)據(jù)庫中的表空間和數(shù)據(jù)文件,Rman配置成功。
?
??? 3、使用Rman備份數(shù)據(jù)庫
?????? COPY:復(fù)制文件(數(shù)據(jù)文件和控制文件)只能copy到磁盤。
?????? RMAN>copy datafile 9 to 'c:\oracle\back\9.cf';
?????? 顯示備份文件信息:
?????? RMAN>list copy of datafile 9;
?????? 備份控制文件:
?????? RMAN>copy current controlfile to 'c:\oracle\back\control.cf';
?????? RMAN>list copy of controlfile;
?????? 刪除備份文件:
?????? RMAN>delete copy of datafile 9;
?????? RMAN>delete copy of controlfile;
?????? 復(fù)制多個文件:
?????? RMAN>run {
???????????????? copy
????????????? ?datafile 9 to 'c:\oralce\back\9.cf,
????????????? ?datafile 8 to 'c:\oracle\back\8.cf,
????????????? ?current controlfile to 'c:\oracle\back\control.cf';
????????????? ?}
?????? RMAN>host "cls";
?
?????? BACKUP:可以備份數(shù)據(jù)庫、表空間、數(shù)據(jù)文件,可以備份到磁盤和磁帶,并對備份文件進(jìn)行壓縮。
?????? RMAN>backup datafile 9,8,7
??????????? format='c:\oracle\back\%T_%U';? %T系統(tǒng)當(dāng)前時間,%U系統(tǒng)唯一型,備份文件不會重復(fù)。
?????? 顯示:
?????? RMAN>list backup of datafile 9;
?????? 刪除:
?????? RMAN>delete backup of datafile 9;
?????? 刪除所有文件:
?????? RMAN>delete backup;
?????? 備份表空間:
?????? RMAN>backup tablespace users,indx,odm
??????????? format='c:\oracle\back\'%T_%U';
?????? 全部備份:
?????? RMAN>backup tablespace database
??????????? format='c:\oracle\back\'%T_%U';
?
??? 4、使用RMAN恢復(fù)數(shù)據(jù)庫
?????? RMAN>restore |datafile,tablespace,database? --向回考,物理性恢復(fù)
?????? RMAN>recover |datafile,tablespace,database? --同步恢復(fù)。
?
??? 5、Rman恢復(fù)實(shí)例
?????? A、恢復(fù)數(shù)據(jù)文件
????????? RMAN>startup mount
?????? ????? >restore datafile 9;
?????? ????? >recover datafile 9;
?????? ????? >alter database open;
?????? B、恢復(fù)表空間
????????? RMAN>startup mount
?????? ???? ?>restore tablespace users,
?????? ????? >recover tablespace users;
?????? ????? >alter database open;
?
其它:
在Windows下清除Oracle8i/9i/10g運(yùn)行環(huán)境
1、刪除Oracle注冊表
??? regedit.exe->Local Machine->Software->ORACLE
?
2、刪除Oracle服務(wù)
? regedit.exe->Local Machine->System->CurrentControlSet->Services->ORA*
?
3、刪除Oracle事件日志
? regedit.exe->Local Machine->System->CurrentControlSet->Services->Eventlog->Application->ORA*
?
4、刪除NT安裝磁盤\Program Files\Oracle目錄
?
5、刪除Oracle環(huán)境變量
?? 控制面板->系統(tǒng)->高級->環(huán)境變量
?
6、刪除Oracle菜單
?1
7、重新啟動操作系統(tǒng)
?
8、刪除Oracle主目錄
?
注意: oracle用于unix時,長時間不重起,重啟一次時間非常長。
?
《Oracle數(shù)據(jù)庫系統(tǒng)管理》(上、下冊)???????????? --清華大學(xué)出版社
《Oracle Developer/2000開發(fā)實(shí)例與應(yīng)用技術(shù)》??? --清華大學(xué)出版社
《Oracle9i數(shù)據(jù)庫系統(tǒng)管理大全》???????????????? --清華大學(xué)出版社
《Oracle10g數(shù)據(jù)庫系統(tǒng)管理》??????????????????? --機(jī)械工業(yè)出版社
?
轉(zhuǎn)載于:https://www.cnblogs.com/we1700/archive/2012/02/21/2361485.html
總結(jié)
以上是生活随笔為你收集整理的oracle 基础1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP上传注意事项
- 下一篇: 安装VS2010 无法打开数据文件'C: