国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > Oracle > 正文

Oracle經(jīng)驗(yàn)技巧集

2024-08-29 13:29:14
字體:
供稿:網(wǎng)友
 oracle經(jīng)驗(yàn)技巧集1.刪除表空間
    drop tablespace tablespacename [including contents [and datafiles]]
2.刪除用戶
    drop user user_name cascade
3.刪除表的注意事項(xiàng)
    在刪除一個(gè)表中的全部數(shù)據(jù)時(shí),須使用truncate table 表名;因?yàn)橛胐rop table,delete * from 表名時(shí),tablespace表空間該表的占用空間并未釋放,反復(fù)幾次drop,delete操作后,該tablespace上百兆的空間就被耗光了。
4.having子句的用法
    having子句對(duì)group by子句所確定的行組進(jìn)行控制,having子句條件中只允許涉及常量,聚組函數(shù)或group by 子句中的列。
5.外部聯(lián)接"+"的用法
    外部聯(lián)接"+"按其在"="的左邊或右邊分左聯(lián)接和右聯(lián)接.若不帶"+"運(yùn)算符的表中的一個(gè)行不直接匹配于帶"+"預(yù)算符的表中的任何行,則前者的行與后者中的一個(gè)空行相匹配并被返回.若二者均不帶’+’,則二者中無法匹配的均被返回.利用外部聯(lián)接"+",可以替代效率十分低下的 not in 運(yùn)算,大大提高運(yùn)行速度.例如,下面這條命令執(zhí)行起來很慢

 

    用外聯(lián)接提高表連接的查詢速度

    在作表連接(常用于視圖)時(shí),常使用以下方法來查詢數(shù)據(jù):

    select pay_no, project_name

    from a

    where a.pay_no not in (select pay_

    no from b where value >=120000);

    ---- 但是若表a有10000條記錄,表b有10000條記錄,則要用掉30分鐘才能查完,主要因?yàn)閚ot in要進(jìn)  行一條一條的比較,共需要10000*10000次比較后,才能得到結(jié)果。該用外聯(lián)接后,可以縮短到1分左右的時(shí)間:

    select pay_no,project_name

    from a,b

    where a.pay_no=b.pay_no(+)

    and b.pay_no is null

    and b.value >=12000;
6.set transaction命令的用法
    在執(zhí)行大事務(wù)時(shí),有時(shí)oracle會(huì)報(bào)出如下的錯(cuò)誤:

    ora-01555:snapshot too old (rollback segment too small)

  這說明oracle給此事務(wù)隨機(jī)分配的回滾段太小了,這時(shí)可以為它指定一個(gè)足夠大的回滾段,以確保這個(gè)事務(wù)的成功執(zhí)行.例如

 

    set transaction use rollback segment roll_abc;

    delete from table_name where ...

    commit;

  回滾段roll_abc被指定給這個(gè)delete事務(wù),commit命令則在事務(wù)結(jié)束之后取消了回滾段的指定. 
7.?dāng)?shù)據(jù)庫重建應(yīng)注意的問題

 

  在利用import進(jìn)行數(shù)據(jù)庫重建過程中,有些視圖可能會(huì)帶來問題,因?yàn)榻Y(jié)構(gòu)輸入的順序可能造成視圖的輸入先于它低層次表的輸入,這樣建立視圖就會(huì)失敗.要解決這一問題,可采取分兩步走的方法:首先輸入結(jié)構(gòu),然后輸入數(shù)據(jù).命令舉例如下 (uesrname:jfcl,password:hfjf,host sting:ora1,數(shù)據(jù)文件:expdata.dmp):

imp jfcl/[email protected] file=empdata.dmp rows=n

imp jfcl/[email protected] file=empdata.dmp full=y buffer=64000

commit=y ignore=y

  第一條命令輸入所有數(shù)據(jù)庫結(jié)構(gòu),但無記錄.第二次輸入結(jié)構(gòu)和數(shù)據(jù),64000字節(jié)提交一次.ignore=y選項(xiàng)保證第二次輸入既使對(duì)象存在的情況下也能成功.

select a.empno from emp a where a.empno not in

 (select empno from emp1 where job=’sale’);

  倘若利用外部聯(lián)接,改寫命令如下:

select a.empno from emp a ,emp1 b

where a.empno=b.empno(+)

and b.empno is null

and b.job=’sale’;

  可以發(fā)現(xiàn),運(yùn)行速度明顯提高.
8.從已知表新建另一個(gè)表:
create table b

as select * (可以是表a中的幾列)

from a

where a.column = ...;
9.查找、刪除重復(fù)記錄:
法一: 用group by語句 此查找很快的

select count(num), max(name) from student --查找表中num列重復(fù)的,列出重復(fù)的記錄數(shù),并列出他的name屬性

group by num

having count(num) >1 --按num分組后找出表中num列重復(fù),即出現(xiàn)次數(shù)大于一次

 

delete from student(上面select的)

這樣的話就把所有重復(fù)的都刪除了。-----慎重

 

法二:當(dāng)表比較大(例如10萬條以上)時(shí),這個(gè)方法的效率之差令人無法忍受,需要另想辦法:

---- 執(zhí)行下面sql語句后就可以顯示所有drawing和dsno相同且重復(fù)的記錄

select * from em5_pipe_prefab

where rowid!=(select max(rowid) from em5_pipe_prefab d --d相當(dāng)于first,second

where em5_pipe_prefab.drawing=d.drawing and

em5_pipe_prefab.dsno=d.dsno);

 

---- 執(zhí)行下面sql語句后就可以刪除所有drawing和dsno相同且重復(fù)的記錄

delete from em5_pipe_prefab

where rowid!=(select max(rowid) from em5_pipe_prefab d

where em5_pipe_prefab.drawing=d.drawing and

em5_pipe_prefab.dsno=d.dsno);

 
10.返回表中[n,m]條記錄:
 

取得某列中第n大的行

select column_name from

(select table_name.*,dense_rank() over (order by column desc) rank from table_name)

where rank = &n;

 

 假如要返回前5條記錄:

  select * from tablename where rownum<6;(或是rownum <= 5 或是rownum != 6)

假如要返回第5-9條記錄:

select * from tablename

where …

and rownum<10

minus

select * from tablename

where …

and rownum<5

order by name

 選出結(jié)果后用name排序顯示結(jié)果。(先選再排序)

 

注意:只能用以上符號(hào)(<、<=、!=)。

select * from tablename where rownum != 10;返回的是前9條記錄。

不能用:>,>=,=,between...and。由于rownum是一個(gè)總是從1開始的偽列,oracle 認(rèn)為這種條件 不成立,查不到記錄.

 

另外,這個(gè)方法更快:

select * from (

select rownum r,a from yourtable

where rownum <= 20

order by name )

where r > 10

這樣取出第11-20條記錄!(先選再排序再選)

要先排序再選則須用select嵌套:內(nèi)層排序外層選。

 

rownum是隨著結(jié)果集生成的,一旦生成,就不會(huì)變化了;同時(shí),生成的結(jié)果是依次遞加的,沒有1就永遠(yuǎn)不會(huì)有2!

rownum 是在 查詢集合產(chǎn)生的過程中產(chǎn)生的偽列,并且如果where條件中存在 rownum 條件的話,則:

1: 假如 判定條件是常量,則:

只能 rownum = 1, <= 大于1 的自然數(shù), = 大于1 的數(shù)是沒有結(jié)果的, 大于一個(gè)數(shù)也是沒有結(jié)果的

即 當(dāng)出現(xiàn)一個(gè) rownum 不滿足條件的時(shí)候則 查詢結(jié)束   this is stop key!

2: 當(dāng)判定值不是常量的時(shí)候

若條件是 = var , 則只有當(dāng) var 為1 的時(shí)候才滿足條件,這個(gè)時(shí)候不存在 stop key ,必須進(jìn)行 full scan ,對(duì)每個(gè)滿足其他where條件的數(shù)據(jù)進(jìn)行判定

選出一行后才能去選rownum=2的行……

 

 
11.快速編譯所有視圖
 

---- 當(dāng)在把數(shù)據(jù)庫倒入到新的服務(wù)器上后(數(shù)據(jù)庫重建),需要將視圖重新編譯一遍,因?yàn)樵摫砜臻g視圖到其它表空間的表的連接會(huì)出現(xiàn)問題,可以利用pl/sql的語言特性,快速編譯。

sql >spool on.sql

sql >select ‘alter view ‘||tname||’

compile;’ from tab;

sql >spool off

然后執(zhí)行on.sql即可。

sql >@on.sql

當(dāng)然,授權(quán)和創(chuàng)建同義詞也可以快速進(jìn)行,如:

sql >select ‘grant select on ’

||tname||’ to username;’ from tab;

sql >select ‘create synonym

‘||tname||’ for username.’||tname||’;’ from tab;

 

 

--------------------

讓你的天空只有甜和美

 

遺忘———該怎么流淚

 

文章選項(xiàng):

 

lunatic

(stranger)

06/13/03 11:33

[精華] re: oracle 常用命令 [re: lunatic]

 

 

 
12.讀寫文本型操作系統(tǒng)文件
---- 在pl/sql 3.3以上的版本中,utl_file包允許用戶通過pl/sql讀寫操作系統(tǒng)文件。如下:

 

decalre

file_handle utl_file.file_type;

begin

file_handle:=utl_file.fopen(

‘c:/’,’test.txt’,’a’);

utl_file.put_line(file_handle,’

hello,it’s a test txt file’);

utl_file.fclose(file_handle);

end;

 

 
13.在數(shù)據(jù)庫觸發(fā)器中使用列的新值與舊值
---- 在數(shù)據(jù)庫觸發(fā)器中幾乎總是要使用觸發(fā)器基表的列值,如果某條語句需要某列修改前的值,使用:old就可以了,使用某列修改后的新值,用:new就可以了。如:old.dept_no,:new.dept_no。

 

 
14.?dāng)?shù)據(jù)庫文件的移動(dòng)方法
當(dāng)想將數(shù)據(jù)庫文件移動(dòng)到另外一個(gè)目錄下時(shí),可以用alter database命令來移動(dòng)(比alter tablespace適用性強(qiáng)):

1. 使用server manager關(guān)閉實(shí)例.

svrmgr > connect internal;

svrmgr > shutdown;

svrmgr >exit;

2. 使用操作系統(tǒng)命令來移動(dòng)數(shù)據(jù)庫文件位置(假設(shè)這里操作系統(tǒng)為solaris 2.6). 在unix中用 mv命令可以把文件移動(dòng)到新的位置,

#mv /ora13/orarun/document.dbf /ora12/orarun

3. 裝載數(shù)據(jù)庫并用alter database命令來改變數(shù)據(jù)庫中的文件名.

svrmgr > connect internal;

svrmgr > startup mount run73;

svrmgr > alter database rename file

> ‘/ ora13/orarun/document.dbf’

> ‘/ ora12/orarun/document.dbf’;

4. 啟動(dòng)實(shí)例.

svrmgr > alter database open;

 

 
15.連接查詢結(jié)果:
表a 列 a1 a2

記錄 1 a

1 b

2 x

2 y

2 z

用select能選成以下結(jié)果:

1 ab

2 xyz

 

下面有兩個(gè)例子:

1.使用pl/sql代碼實(shí)現(xiàn),但要求你組合后的長度不能超出oracle varchar2長度的限制

create or replace type strings_table is table of varchar2(20);

/

create or replace function merge (pv in strings_table) return varchar2

is

ls varchar2(4000);

begin

for i in 1..pv.count loop

ls := ls || pv(i);

end loop;

return ls;

end;

/

create table t (id number,name varchar2(10));

insert into t values(1,'joan');

insert into t values(1,'jack');

insert into t values(1,'tom');

insert into t values(2,'rose');

insert into t values(2,'jenny');

 

column names format a80;

select t0.id,merge(cast(multiset(select name from t where t.id = t0.id) as strings_table)) names

from (select distinct id from t) t0;

 

drop type strings_table;

drop function merge;

drop table t;

 

 

2.純粹用sql:

表dept, emp

要得到如下結(jié)果

deptno, dname, employees

---------------------------------

10, accounting, clark;king;miller

20, research, smith;adams;ford;scott;jones

30, sales, allen;blake;martin;james;turners

每個(gè)dept的employee串起來作為一條記錄返回

 

this example uses a max of 6, and would need more cut n pasting to do more than that:

 

sql> select deptno, dname, emps

2 from (

3 select d.deptno, d.dname, rtrim(e.ename ||', '||

4 lead(e.ename,1) over (partition by d.deptno

5 order by e.ename) ||', '||

6 lead(e.ename,2) over (partition by d.deptno

7 order by e.ename) ||', '||

8 lead(e.ename,3) over (partition by d.deptno

9 order by e.ename) ||', '||

10 lead(e.ename,4) over (partition by d.deptno

11 order by e.ename) ||', '||

12 lead(e.ename,5) over (partition by d.deptno

13 order by e.ename),', ') emps,

14 row_number () over (partition by d.deptno

15 order by e.ename) x

16 from emp e, dept d

17 where d.deptno = e.deptno

18 )

19 where x = 1

20 /

 

deptno dname emps

------- ----------- ------------------------------------------

10 accounting clark, king, miller

20 research adams, ford, jones, rooney, scott, smith

30 sales allen, blake, james, martin, turner, ward

 

 

 
16.在oracle中建一個(gè)編號(hào)會(huì)自動(dòng)增加的字段,以利于查詢
 

1、建立序列:

create sequence checkup_no_seq

nocycle

maxvalue 9999999999

start with 2;

 

2、建立觸發(fā)器:

create or replace trigger set_checkup_no

before insert on checkup_history

for each row

declare

next_checkup_no number;

begin

--get the next checkup number from the sequence

select checkup_no_seq.nextval

into next_checkup_no

from dual;

 

--use the sequence number as the primary key

--for the record being inserted

:new.checkup_no := next_checkup_no;

end;

 

 
17.查看對(duì)象的依賴關(guān)系(比如視圖與表的引用)
 

查看視圖:dba_dependencies 記錄了相關(guān)的依賴關(guān)系

查東西不知道要查看哪個(gè)視圖時(shí),可以在dba_objects里看,

select object_name from dba_objects where object_name like '%role%'(假如查看role相關(guān))

然后desc一下就大體上知道了。

 

 
18.要找到某月中所有周五的具體日期
select to_char(t.d,'yy-mm-dd') from (

select trunc(sysdate, 'mm')+rownum-1 as d

from dba_objects

where rownum < 32) t

where to_char(t.d, 'mm') = to_char(sysdate, 'mm') --找出當(dāng)前月份的周五的日期

and trim(to_char(t.d, 'day')) = '星期五'

--------

03-05-02

03-05-09

03-05-16

03-05-23

03-05-30

 

如果把where to_char(t.d, 'mm') = to_char(sysdate, 'mm')改成sysdate-90,即為查找當(dāng)前

月份的前三個(gè)月中的每周五的日期。
中國最大的web開發(fā)資源網(wǎng)站及技術(shù)社區(qū),
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 张北县| 武安市| 江北区| 龙游县| 永康市| 中宁县| 阳西县| 富裕县| 安新县| 陆川县| 潮安县| 岚皋县| 开封县| 阿拉善右旗| 姚安县| 全州县| 隆回县| 桦甸市| 蒙自县| 开原市| 西乌珠穆沁旗| 东方市| 和平区| 肥西县| 茂名市| 弥渡县| 名山县| 大荔县| 东乡族自治县| 广饶县| 阿城市| 浦北县| 新密市| 枣庄市| 前郭尔| 德保县| 比如县| 肥乡县| 新营市| 绍兴县| 公主岭市|