acc
(zcaid number,zraid number,accountes INOUTnumber)
is flags number;
begin update accounts set money=money+accountes where aid=zraid returning flag into flags;
ifsql%rowcount=0then raise_application_error(-20002,'目標賬號不存在');
elsif(flags=1)then raise_application_error(-20003,'目標賬號已被鎖定');
endif;
update accounts set money=money-accountes where aid=zcaid returning money into accountes;
if(accountes=10)then raise_application_error(-20004,'你帳戶余額不足十元請取消操作');
endif;
commit;
end;
---
DECLARE v1NUMBER:=300;
BEGIN acc(9,10,v1);
dbms_output.put_line(v1);
END;
包的使用
CREATEORREPLACEPROCEDURE hello(
p_emp INOUT emp.ename%TYPE
)
---
AS BEGIN p_emp:=upper(p_emp);
dbms_output.put_line('hello,'||p_emp);
END;
CREATEORREPLACEPACKAGE bao_1 AS PROCEDURE hello(p_emp emp.ename%TYPE);
FUNCTION add_number(a NUMBER,b NUMBER)RETURNNUMBER;
END;
CREATEORREPLACEPACKAGEBODY bao_1 AS PROCEDURE hello(p_emp emp.ename%TYPE)
AS BEGIN
--p_emp:=upper(p_emp);
dbms_output.put_line('hello,'||p_emp);
END;
FUNCTION add_number(a NUMBER,b NUMBER)RETURNNUMBER AS BEGIN RETURN NVL(a,0)+NVL(b,0);
END;
END;
CALL bao_1.hello('lovo');
SELECT bao_1.add_number(10,NULL)FROM dual;
createorreplaceprocedure acc
(zcaid number,zraid number,accountes INOUTnumber)
is flags number;
begin update accounts set money=money+accountes where aid=zraid returning flag into flags;
ifsql%rowcount=0then raise_application_error(-20002,'目標賬號不存在');
elsif(flags=1)then raise_application_error(-20003,'目的賬號已被鎖定');
endif;
update accounts set money=money-accountes where aid=zcaid returning money into accountes;
if(accountes=10)then raise_application_error(-20004,'你帳戶余額不足十元請撤消操作');
endif;
commit;
end;
---
DECLARE v1NUMBER:=300;
BEGIN acc(9,10,v1);
dbms_output.put_line(v1);
END;
select*from accounts
--特點:無返回值存儲過程
Connection conn=.;
String spName="{call acc(?,?,?)}";
CallableStatement cstmt=conn.prepareCall(spName);
cstmt.setString(1,"值");
…
cstmt.executeUpdate();
--特色:有返回值存儲過程,返回一個值
Connection conn=.;
String spName="{call acc(?,?,?)}";
CallableStatement cstmt=conn.prepareCall(spName);
cstmt.setInt(1,9);
cstmt.setInt(2,10);
cstmt.setInt(2,150);
cstmt.registerOutParameter(3,java.sql.Types.DOUBLE);
cstmt.executeUpdate();
int money=cstmt.getInt(3);
System.out.println("余額:"+money);
1.只讀事務(wù)--只讀事務(wù)
只讀事務(wù)是指只容許執(zhí)行查詢的事務(wù),而不許可執(zhí)行其它任何dml
操作的事務(wù),使用只讀事務(wù)可以確保用戶只能獲得某時光點的數(shù)據(jù),
假定:機票代售票點天天18點開端統(tǒng)計今天的銷售情況,這時可以
應(yīng)用只讀事務(wù),在設(shè)置了只讀事務(wù)后,只管其它會話可能會提交新的事務(wù)
,然而只讀事務(wù)將不會取得新數(shù)據(jù)的變更,從而可以保障取得特定時間
點的數(shù)據(jù)信息。
--在做統(tǒng)計的時候同時也在賣票。不可能在統(tǒng)計的統(tǒng)計的時候不答應(yīng)賣票
--設(shè)置吟詩事務(wù)
settransactionreadonly
--示例:1.以system登陸
再set transactionreadonly
連到scott/tiger登陸,select*from emp;有18行列出來了
再增加一條數(shù)據(jù)
insertinto emp(empno,ename,hiredate)values(111,'zbm',to_date('2000-10-01','yyyy-mm-dd'));
commit;
在scott中能看得到該用戶zbm
在system用戶中不能看到該用戶zbm--select*from scott.emp;
--在個用戶中設(shè)置了只讀事務(wù),之后的操作就不能讀出來了。
--一般主要用在機票代售點,銀行的本錢的統(tǒng)計,職員工資的統(tǒng)計
--字符函數(shù)
lower(char):將字符串轉(zhuǎn)成小寫的格式
upper(char):將字符串轉(zhuǎn)居大寫的格式
length(char):返回字符串的長度
substr(char,m,n):取字符串的子串
replace(char,search_string,repalce_string)
--將姓名按小/大寫顯示
select lower(ename)from emp;
select upper(ename)from emp;
--顯示正好為5個字符的員工姓名
select ename ename from emp where length(ename)=5;
--顯示所有員工的前三個字符
select substr(ename,0,3)from emp;
--以首字母大寫的方式顯示所有的員工
select upper(substr(ename,0,1))||lower(substr(ename,1,len gth(ename)-1))from emp;
--以首字母小寫的方式顯示所有的員工
select lower(substr(ename,0,1))||upper(substr(ename,1,len gth(ename)-1))from emp;
--把名字中的a字母替為我
selectreplace(ename,'A','我')from emp;
--數(shù)字函數(shù)
round(n,[m]):四舍五入,沒m舍到整數(shù),m為正數(shù),舍到小數(shù)后m位,m為負數(shù),舍到數(shù)前m位
trunc(n,[m]):截取數(shù)字,與round一樣,只不外前者是四舍五入,后者是截取
mod(m,n):取模
floor(n):返回小于或等于n的最大整數(shù)
ceil(n):返回大于n或即是n的最小整數(shù)
--對數(shù)字的處理,在財務(wù)系統(tǒng)或在銀行頂用的最多,不同的處理辦法
--對賬務(wù)報表有不同的成果
用四種方法算年薪
用round select(round(sal)+round(comm))*13as年薪from emp where ename='ALLEN';--24700 select(round(sal,2)+round(comm,2))*13as年薪from emp where ename='ALLEN';--24877
用trunc select(trunc(sal)+trunc(comm))*13as年薪from emp where ename='ALLEN';--24700
用floor,用ceil一樣
selectmod(10,3)from dual;--1
--例,數(shù)據(jù):2345.56 45.94
--顯示在一個月為30天的情況下所有員工的日薪金,疏忽余數(shù)
select*from emp forupdate;
--用
oracle第四天
--日期專題
日期函數(shù)用于處置date類型的數(shù)據(jù),默認日期格局是:dd-mon-yy即12-7月-78 1).sysdate:該函數(shù)返回系統(tǒng)時間selectsysdatefrom dual;
2).add_months(d,n)
3).last_day(d):返回指定日期所在月份的最后一天
--題目一:查找已經(jīng)入職8個月多的員工
select*from emp wheresysdate add_months(hiredate,200);
--題目二:顯示滿10年服務(wù)年限的員工的姓名和受雇日期。
select ename,hiredate from emp wheresysdate add_months(hiredate,120);
--題目三:對于每一個員工,顯示其參加公司的天數(shù)。
select ename,sysdate-hiredate as"入職天數(shù)"from emp;
--標題四:找出各月倒數(shù)第3天受雇的所有員工。(在結(jié)賬的時候很有用)
select hiredate,last_day(hiredate),last_day(hiredate)-2from emp;
--轉(zhuǎn)換函數(shù)
1.不必函數(shù)也能轉(zhuǎn)換
insertinto emp(empno,ename)values('001','張三豐');
2.字符串轉(zhuǎn)化函數(shù)
select ename,to_char(hiredate,'yyyy-mm-dd hh24:mi:ss')from emp;
select ename,to_char(sal,'L 99999.999')from emp;--L表示本地的貨幣符號
select ename,to_char(sal,'L99,999.99')as rmb from emp;--L表現(xiàn)本地的貨泉符號
select ename,to_char(sal,'$99,999.99')as rmb from emp;
3.顯示1980年入職的員工
select*from emp where to_char(hiredate,'yyyy')=1981;
4.顯示所有12月份入職的員工
select*from emp where to_char(hiredate,'mm')=12;
1.數(shù)據(jù)庫管理員,管理員有基礎(chǔ)職責
2.數(shù)據(jù)庫/表的邏輯備份與恢復(fù)
3.數(shù)據(jù)字典和動態(tài)機能視圖
4.管理表空間和數(shù)據(jù)文件,數(shù)據(jù)字典,性能視圖
解釋:每個數(shù)據(jù)庫應(yīng)當至少有一名數(shù)據(jù)庫管理員dba
,但我國中小企業(yè)個別不,至公司的dba做什么呢?
--職責
1).安裝和進級oracle數(shù)據(jù)庫
2).建庫,表空間,表,視圖,索引.
3).制訂并實行備份與恢復(fù)打算
4).數(shù)據(jù)庫權(quán)限治理,調(diào)優(yōu),故障消除--重點
5).對高等dba,請求能參加名目開發(fā),會編寫sql語句服務(wù)器數(shù)據(jù)恢復(fù),存儲進程
,觸發(fā)器,規(guī)矩,約束,包
--體系函數(shù)
1.查看當前用戶,及當前數(shù)據(jù)庫--db_name
在sqlplus下show user;
或
select sys_context('userenv','session_user')from dual;
select sys_context('userenv','db_name')from dual;--orcl 2.查看當前所用語言---language select sys_context('userenv','language')from dual;
3.顯示當前會話客戶所對應(yīng)的默認方案名--方案和用戶關(guān)系?
--用戶創(chuàng)立之后,會主動創(chuàng)建一個方案,方案與用戶同名,方案中有各種
--對象,如觸發(fā)器,表,視圖,過程等
select sys_context('userenv','current_schema')from dual;
4.顯示當前主機名
select sys_context('userenv'數(shù)據(jù)恢復(fù),'host')from dual;
sys和system比擬1.存儲數(shù)據(jù)的主要性不同
占有數(shù)據(jù)庫管理員角色dba
--sys用戶:(其方案中有基表,動態(tài)視圖)系統(tǒng)管理員角色sysdba
系統(tǒng)操作員角色sysoper
角色:數(shù)據(jù)庫管理員角色dba
--system用戶:(方案中為次級數(shù)據(jù):)系統(tǒng)管理員角色sysdba 2.權(quán)限不同
1).sys用戶必須以as sysdba或as sysopr情勢登陸,不能以nomal方式登陸數(shù)據(jù)庫
system是畸形登陸,它其實就是一個一般的dba用戶,但作為as sysdba登陸,其結(jié)果
實際上它作為sys用戶登錄的,從登陸信息咱們可以看出。
sqlplus sys/tigertiger assysdba可以登陸
conn sys/tigertiger;--你不再銜接到ORACLE。
conn sys/tigertger assysdba;--可以連接
即:conn system/tigertiger是以普通的dba的身份登陸的(conn system/tigertiger asdba)
3.權(quán)限比較。sysdba sysoper dba sysdba sysoper startup(啟動數(shù)據(jù)庫)startup shutdown shutdown alterdatabaseopen/mount/backup一樣
轉(zhuǎn)變字符集--none createdatabase(創(chuàng)建數(shù)據(jù)庫)--不能
dropdatabase(刪除數(shù)據(jù)庫)--不能
createspfile createspfile alterdatabasearchivelog(歸檔日志)一樣
alterdatabaserecover(恢復(fù)數(shù)據(jù)庫)只能完整恢復(fù)不能不完全恢復(fù)
擁有restricted session會話擁有restricted session權(quán)限
可以讓用戶用為sys用戶連接可以進行一些根本的操作,不能查看用戶數(shù)據(jù)
登陸之后是sys登陸之后是public
--dba的權(quán)限是登陸之后才有的對數(shù)據(jù)庫的操作
4.數(shù)據(jù)庫管理員的工作
1).顯示初始化參數(shù),200多個初始化參數(shù)
show parameter命令,在init.ora文件中去D:\oracle\srvm\admin\init.ora 5.數(shù)據(jù)庫表的邏輯備份與恢復(fù)
邏輯備份是指使用工具export將數(shù)據(jù)對象的結(jié)構(gòu)和數(shù)據(jù)導出到文件的過程,邏輯
恢復(fù)是指當數(shù)據(jù)庫對象被誤操作而破壞后使用工具import應(yīng)用備份的文件把數(shù)據(jù)
對象導入到數(shù)據(jù)庫的過程。物理備份即可在數(shù)據(jù)庫的open的狀態(tài)下進行也可以
在封閉數(shù)據(jù)庫落后行,但是邏輯備份和恢復(fù)在open的狀態(tài)下進行(即數(shù)據(jù)庫翻開時進行)。
--導出分三品種型:
導出表,導出方案,導出數(shù)據(jù)庫三種方式
導出使用exp命令來完成的,該命令常用的選項有:
userid:用于指定執(zhí)行導出操作的用戶名,口令,連接字符串
tables:用于指定履行導出操作的表
owner:用于指定執(zhí)行導出操作的方案
full=y:用于指定執(zhí)行導出操作的數(shù)據(jù)庫
inctype:用于指定執(zhí)行導出操作的增量類型
file:用于指定導出文件名
一.導出表
1.導出本人的表,進到oracle裝置的bin目錄下,D:\oracle\BIN再執(zhí)行以下的命令
exp userid=scott/tigertiger@orcl tables=(emp,dept)file=d:\e1.dmp 2.導出其它方案的表
如果用戶要導出其它方案的表,則需要dba的權(quán)限或是
exp_full_database的權(quán)限,比方system就可以導出scott的表
exe userid=system/tiger@orcl tables=(scott.emp)file=d:\e2.dmp
--scott.emp--哪一個計劃的表
3.導出表結(jié)構(gòu)
exp userid=scott/tiger@orcl tables=(emp)file=d:\e3.dmp rows=n
導出大表的構(gòu)造,數(shù)據(jù)量大的時候exp userid=scott/tiger@orcl tables=(emp)file=d:\e3.dmp direct=y
三.導出方案
導出方案是支使用export工具導出一個方案或是多個方案中的所有對象(表,索引,束縛)
和數(shù)據(jù),并存放到文件中。
1).導出自己的方案
exp scott/tiger@orcl owner=scott file=d:\scott.emp 2).導出其它方案
exp_full_database的權(quán)限,例如system用戶就能夠?qū)С鋈魏畏桨?BR> exp system/tiger@orcl owner=(system,scott)file=d:\system.dmp 4.導出數(shù)據(jù)庫
exp userid=system/tiger@orcl full=y inctype=completefile=d:/aa.dmp
二.導入表
1).導入自己表
imp userid=scott/tiger@orcl tables=(emp)file=d:\x.dmp 2).導入表到其它用戶
要求該用戶具有dba權(quán)限,或是imp_full_database imp userid=system/tiger@orcl tables=(emp)file=d:\x.dmp touser=scott 3).導入表的結(jié)構(gòu)
導入表的結(jié)構(gòu)而不導入數(shù)據(jù)
imp userid=scott/tiger@orcl tables=(emp)file=d:\x.dmp rows=n 4).導入數(shù)據(jù)
表已經(jīng)存在,可以只導入表的數(shù)據(jù)
imp userid=scott/tiger@orcl tables=(emp)file=d:\x.dmp ignore=y
--數(shù)據(jù)字典和動態(tài)性能視圖
基表:數(shù)據(jù)字典是oracle數(shù)據(jù)庫中最重要的組成局部,它供給了數(shù)據(jù)庫的一些系統(tǒng)信息--存放靜態(tài)信息
動態(tài)性能視圖:動態(tài)性能視圖記錄了例程啟動后的相干信息.--存入常常變化的動態(tài)信息
數(shù)據(jù)字典重要用于取得數(shù)據(jù)庫信息,用戶一般不能去操作它
--user_tables:當前用戶領(lǐng)有的所有的表
select table_name from user_tables;
--all_tables當前用戶可以訪問的所有的表,不僅自己的表,別人的表,只有
我能拜訪,也能查出來
select table_name from all_tables;
--dba_tables數(shù)據(jù)庫中所有的表
select table_name from dba_tables;--表或視圖不存在,由于你沒有權(quán)限
以sys/tiger asdba;這個身份可以查看了
--數(shù)據(jù)字典和動態(tài)性能視圖
用戶名,權(quán)限,角色
在建立用戶時,oracle會把用戶的信息存放到數(shù)據(jù)字典中,當給用戶
受權(quán)或是角色時,oracle會將權(quán)限跟角色的信息放到數(shù)據(jù)字典中
1.通過查詢:dba_users可以顯示所有的數(shù)據(jù)庫用戶具體信息
select username from dba_users;--把所有的用戶都列出來了
select username,passwordfrom dba_users;--把所有的用戶都列出來了
--密碼是加密的
2.通過查詢數(shù)據(jù)字典視圖dba_sys_privs,可以顯示用戶所具有的系統(tǒng)權(quán)限
select*from system_privilege_map orderbyname;
select username from dba_users;--把所有的用戶都列出來了
3.通過查詢數(shù)據(jù)字典視圖dba_tab_privs可以顯示用戶具備的對象權(quán)限
selectdistinctprivilegefrom dba_tab_privs;
4.通過查詢字典dba_col_privs可以顯示用戶具有的列權(quán)限
5.通過查詢數(shù)據(jù)字典視圖dba_role_privs可以顯示用戶所擁有的的角色
select*from dba_role_privs where grantee='SCOTT';
--角色和權(quán)限,一個角色里面有良多的權(quán)限,
6.查問數(shù)據(jù)庫的表空間
select tablespace_name from dba_tablespaces;
7.查詢oracle所有的角色,正常是dba.即:oracle空間有多少種角色
select*from dba_roles;
8.如何查詢一個角色所含的權(quán)限?
實在這里面包含了:一個角包含的系統(tǒng)權(quán)限
select*from dba_sys_privs where grantee='DBA';
select*from dba_sys_privs where grantee='CONNECT';
一個角色包括的對象權(quán)限
select*from dba_tab_privs where grantee='DBA';
--表空間
表空間是數(shù)據(jù)庫的邏輯組成部門,從物理上講,數(shù)據(jù)庫存放在
數(shù)據(jù)文件中,從邏輯上講,數(shù)據(jù)庫則是寄存在表空間中的,表空間
由一個或是多個數(shù)據(jù)文件組成.
表空間,數(shù)據(jù)文件,表的關(guān)聯(lián)?
表相當于人,表空間相當于成都這個地方,數(shù)據(jù)文件相當于人的棲身地
--表空間,段,區(qū),塊(由大到小)
畫個方格(表空間),豎分三段(段),每段橫分三段(區(qū)),再把最小段分三段(塊)
國度,省,市,縣,鎮(zhèn),鄉(xiāng),社。
Oracle中的表空間是沒有限度的.--表空間
表空間是數(shù)據(jù)庫的邏輯組成部分,從物理上講,數(shù)據(jù)庫存放在
數(shù)據(jù)文件中,從邏輯上講,數(shù)據(jù)庫則是存放在表空間中的,表空間
由一個或是多個數(shù)據(jù)文件組成.
表空間,數(shù)據(jù)文件,表的關(guān)系?
表相稱于人,表空間相當于成都這個處所,數(shù)據(jù)文件相稱于人的寓居地
--表空間,段,區(qū),塊(由大到小)
表空間的作用:
1).把持數(shù)據(jù)庫占用的磁盤空間
2).dba可以將不同的數(shù)據(jù)類型安排到不同的地位,這樣有利于
進步i/o性能,同時利于備份和恢復(fù)等管理操作。
--建立表空間
建立表空間是使用create tablespace命令來實現(xiàn)的,須要留神的是
,普通情形下,建立表空間是特權(quán)或是dba來執(zhí)行的,假如用其它用戶來創(chuàng)建
表空間,則用戶必需有create tablespace的系統(tǒng)權(quán)限
--建立數(shù)據(jù)庫表空間
在建立數(shù)據(jù)庫后,為便于管理表,最好建立自已的表空間
createtablespace data01datafile'd:\test01.dbf'
size 20m uniformsize 128k
--說明:執(zhí)行完上述命令后,會建揚名稱為data01的表空間,
并為該表空間建立名稱為data01.dbf的數(shù)據(jù)庫文件,區(qū)的大小為
128k,
使用數(shù)據(jù)表空間
createtable mypart(
deptno number(5),
dname varchar2(10),
loc varchar2(15)
)tablespace data01;--當初就使用表空間了
select*from mypart;
insertinto mypart values(10,'hello','成都');
改變表空間的狀態(tài)
當建破表空間時,表空間處于聯(lián)機(online)狀況,此時該表空間
是可以訪問的,并且該表空間是可以讀寫的,即可以查詢該表空間的數(shù)據(jù)
,而且還可以在表空間執(zhí)行各種語句。但是在進行系統(tǒng)維護或是
數(shù)據(jù)保護時,可能需要改變表空間的狀態(tài)。一般情況下,由特權(quán)
用戶或是dba來操作
1).使表空間脫機
altertablespace表空間名offline 2).使表空間聯(lián)機
altertablespace表空間名online 3).只讀表空間
當建立表空間時,表空間可以讀寫,如果不盼望該表空間上
執(zhí)行update,delete,insert操作,那么可以將表空間修正為
只讀:
altertablespace表空間名readonly altertablespace data01readonly--這個表中的數(shù)據(jù)不能修了
4).顯示表空間中所有的表
select*from all_tables where tablespace_name='DATA01';
結(jié)果:1SCOTT MYPART DATA01 5).曉得表名,查看該表屬于哪個表空間
select tablespace_name美國留學,table_name from user_tables where table_name='EMP';--結(jié)果:表空間:users,表名:EMP select tablespace_name,table_name from user_tables where table_name='MYPART';
6).刪除表空間
一般情況下,由特權(quán)用戶或是dba來操作,如果是其它用戶操作
那么要求用戶存在drop tablespace系統(tǒng)權(quán)限
droptablespace'表空間'includingcontentsanddatafiles;
說明:including contents表示刪除表空間時,刪除該空間的所有的
數(shù)據(jù)庫對象,而datafiles表示將數(shù)據(jù)庫文件也刪除.
7).擴展表空間
表空間是由數(shù)據(jù)文件組成的,表空間的大小實際上就是數(shù)據(jù)文件相加
后的大小,那么我們可以設(shè)想,假設(shè)表employee存放到data01表空間上
,初始大小就是2m,當數(shù)據(jù)滿2m空間后,如果在向employee表插入
數(shù)據(jù),這樣就會顯示空間不足的過錯.
案例闡明:
一,樹立一個表空間sp001 createtablespace demo01datafile'd:\demo01.dbf'
size 10m uniformsize--dba才行
二,在該表空間上建立一個普通表mydment其結(jié)構(gòu)和dept一樣
createtable demoemp asselect*from emp where 1=2;
select*from demoemp;
三,向該表中加入一數(shù)據(jù)
insertinto demoemp(select*from emp);
select*from demoemp;--這樣持續(xù)插入,確定會把數(shù)據(jù)庫整爆
四,當必定時候就會呈現(xiàn)無奈擴大的問題,怎么辦?
五,就擴展表空間,為其增添更多的存儲空間,有三種方式:
1).增加數(shù)據(jù)文件
altertablespace sp01adddatafile'd:\test\sp01.dbf'size20m 2).增長數(shù)據(jù)文件的大小
alterdatabasedatafile'd:\test\sp01.dbf'resize20m;
--這里需要注意的是數(shù)據(jù)文件的大小不要超過500m 3).設(shè)置文件的自動增加
alterdatabasedatafile'd:\test\sp01.dbf'autoextend onnext10m maxsize500m;
關(guān)鍵詞:服務(wù)器數(shù)據(jù)