DROP TABLESPACE TableSpaceName [INCLUDING CONTENTS [AND DATAFILES]]
2.刪除使用者
DROP USER User_Name CASCADE
3.刪除表的注意事項
在刪除一個表中的全部資料時,須使用TRUNCATE TABLE 表名;因為用DROP TABLE,DELETE * FROM 表名時,TABLESPACE資料表空間該表的佔用空間並未釋放,反覆幾次DROP,DELETE操作後,該TABLESPACE上百兆的空間就被耗光了。
4.having子句的用法
having子句對group by子句所確定的行組進行控制,having子句條件中只允許涉及常量,聚組函數或group by 子句中的列。
5.外部聯結"+"的用法
外部聯結"+"按其在"="的左邊或右邊分左聯結和右聯結.若不帶"+"運算子的表中的一個行不直接匹配於帶"+"預算符的表中的任何行,則前者的行與後者中的一個空行相匹配並被返回.若二者均不帶’+’,則二者中無法匹配的均被返回.利用外部聯結"+",可以替代效率十分低下的 not in 運算,大大提高運行速度.例如,下面這條命令執行起來很慢
用外聯結提高表串連的查詢速度
在作表串連(常用於視圖)時,常使用以下方法來查詢資料:
SELECT PAY_NO, PROJECT_NAME
FROM A
WHERE A.PAY_NO NOT IN (SELECT PAY_
NO FROM B WHERE VALUE >=120000);
select a.empno from emp a where a.empno not in (select empno from emp1 where job=’SALE’);
倘若利用外部聯結,改寫命令如下:
select a.empno from emp a ,emp1 b
where a.empno=b.empno(+)
and b.empno is null
and b.job=’SALE’;
可以發現,運行速度明顯提高.
8.從已知表建立另一個表:
CREATE TABLE b
AS SELECT * (可以是表a中的幾列)
FROM a
WHERE a.column = ...;
9.尋找、重複資料刪除記錄:
法一: 用Group by語句 此尋找很快的
select count(num), max(name) from student --尋找表中num列重複的,列出重複的記錄數,並列出他的name屬性
group by num
having count(num) >1 --按num分組後找出表中num列重複,即出現次數大於一次
delete from student(上面Select的)
這樣的話就把所有重複的都刪除了。-----謹慎
法二:當表比較大(例如10萬條以上)時,這個方法的效率之差令人無法忍受,需要另想辦法:
---- 執行下面SQL語句後就可以顯示所有DRAWING和DSNO相同且重複的記錄
SELECT * FROM EM5_PIPE_PREFAB
WHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB D --D相當於First,Second
WHERE EM5_PIPE_PREFAB.DRAWING=D.DRAWING AND
EM5_PIPE_PREFAB.DSNO=D.DSNO);
---- 執行下面SQL語句後就可以刪除所有DRAWING和DSNO相同且重複的記錄
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
選出結果後用name排序顯示結果。(先選再排序)
注意:只能用以上符號(<、<=、!=)。
select * from tablename where rownum != 10;返回的是前9條記錄。
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;
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;
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 /
CREATE SEQUENCE checkup_no_seq
NOCYCLE
MAXVALUE 9999999999
START WITH 2;
2、建立觸發器:
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.查看對象的依賴關係(比如視圖與表的引用)
查看視圖:dba_dependencies 記錄了相關的依賴關係
查東西不知道要查看哪個視圖時,可以在DBA_Objects裡看,
select object_name from dba_objects where object_name like '%ROLE%'(假如查看ROLE相關)
然後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') --找出當前月份的周五的日期
and trim(to_char(t.d, 'Day')) = '星期五'