送你51個Oracle常用語句,看了絕不后悔~
1, 連接數(shù)據(jù)庫;
connect scott/123456;
斷開數(shù)據(jù)庫
disconn;
2, 查看表結(jié)構(gòu)
describe scott.emp;
3, 查看數(shù)據(jù)表
select empno,job,mgr,sal;
4,將緩沖區(qū)中的 sql 語句保存到文件
save scott_emp_query.sql
5,將文件內(nèi)容讀入到緩沖區(qū)中
get scott_emp_query.sql list
6,使用 start 命令讀取并運行 sql 文件
start scott_emp_query.sql
7, 將緩沖區(qū)內(nèi)容復(fù)制到名為 afiedt.buf 文件 (這個是默認(rèn)文件) 中
edit
8,將輸出結(jié)果保存到指定文件中
spool scott_emp_outcome.txt append
9, 帶變量的語句 &
select empno,ename,mgr,sal,deptno
from scott.emp
where empno >=&temp;
10, 帶兩個變量的語句 (兩次輸入)
select &column_name,deptno
from scott.emp
where &column_name>=7850;
11, 帶兩個變量的語句(一次輸入)
select &&column_name ,deptno
from scott.emp
where &&column_name>=7850;
12, 不顯示 “原值” 和 “新值” 信息
set veryify off;(on 是顯示)
新招: 反斜杠 "/" 再次運行上面的查詢
13,設(shè)置變量定義字符
set define '@'(設(shè)置以后定義變量的字符就變?yōu)?@,& 無效)
14,執(zhí)行 sql 文件的語句
@ E:\TEMP\temp.sql (此處應(yīng)當(dāng)注意,路徑是區(qū)分大小寫的)
15,使用 define 查看常量
define;
16, 設(shè)置常量
define temp=7850
查看常量
define temp
刪除常量
undefine temp
17, 使用 column 設(shè)置輸出格式
column empno heading '員工編號' format 9999column ename heading '員工姓名' format a10column mgr heading '上級編號' format 9999column hiredate heading '受雇日期' justfify centercolumn sal heading '員工工資' format $999,999.99select empno,ename,mgr,hiredate,salfrom scott.emp;
18, 設(shè)置一頁顯示多少行數(shù)據(jù)
set pagesize 20
19, 設(shè)置一行顯示多少字符
set linesize 20
20, 設(shè)置頁眉頁腳
ttitle 頁眉, btitle 頁腳
21,break 命令用來分組,compute 命令用來計算和
break on deptno;
compute sum of sal on deptno;
select empno,ename,mgr,sal,deptno
from scott.emp order by deptno;
22, 清除 column 格式
clear columns
23, 創(chuàng)建***性表空間
create tablespace myspace
datafile '文件路徑'
size 文件大小
autoextend on next 自動增加大小
maxsize 文件***值
語法說明:1,temporary|undo(創(chuàng)建撤銷表空間)2,teblespace_name 3,datafile|tempfile'file_name'4,size5,reuse 若存在,則刪掉并重新創(chuàng)建6,autoextend off|on7,next number 自動擴展的大小8,maxsize unlimited|number 指定數(shù)據(jù)文件***大小9,mininum extent number 盤區(qū)可以分配到的最小尺寸10,blocksize number 設(shè)置數(shù)據(jù)塊大小11,online|offline 12,logging|nologging13,force logging 強制表空間數(shù)據(jù)庫對象任何操作都產(chǎn)生日志,否定 1214,default storage storage 指定保存在表空間中的數(shù)據(jù)庫對象默認(rèn)存儲參數(shù)15,compress|nocompress 是否壓縮數(shù)據(jù)(消除列中的重復(fù)值)16,permanent|temporary 指定表空間中數(shù)據(jù)的保存形式17,extent management dictionary(數(shù)據(jù)字典形式管理)|local(本地化形式管理)18,autoallocate|uniform size number 右邊為指定表中盤區(qū)大小 19,segment space management auto |manual 指定表空間中段的管理方式
24,查看表空間屬性
select * from dba_tablespace wheretablespace_name='表空間名';
25, 修改表空間狀態(tài)
alter tablespace 表空間名 表空間狀態(tài);
26,修改表空間名字
alter tablespace 表空間名 1 rename to 表空間名 2;
27,利用數(shù)據(jù)字典查看空閑空間信息
select * from dba_free_space where tablespace_name='表空間名稱';
28, 利用數(shù)據(jù)字典查看表空間數(shù)據(jù)文件信息
select * from dba_data_files wheretablespace_name='表空間名稱';
29, 修改表空間對應(yīng)的數(shù)據(jù)文件大小
alter database
datafile '表空間文件路徑名稱'
resize 大小
30, 為表空間增加新的數(shù)據(jù)文件
alter tablespace myspace
add datafile '數(shù)據(jù)文件路徑名稱'
size 大小
autoextend on next 自增大小 maxsize ***值;
31, 刪除 myspace 表空間數(shù)據(jù)文件
alter tablespace myspace
drop datafile '數(shù)據(jù)文件名稱';
32,修改 myspace 表空間中數(shù)據(jù)文件的自動擴展性
alter database
datafile '表空間文件路徑名稱'
autoextend off;
33, 設(shè)置表空間文件狀態(tài)為 offline drop
alter database
datafile '表空間路徑名稱'
offline drop;
34, 移動表空間中的數(shù)據(jù)文件
(1)alter tablespace 表空間名稱 offline; 先設(shè)置表空間狀態(tài)為 offline
(2) 手動操作,將磁盤中的表空間數(shù)據(jù)文件移動到新的路徑
(3) 使用 alter tablespace 語句修改表空間文件路徑名稱
alter tablespace myspace
rename datafile '表空間路徑名稱'
to
'新的表空間路徑名稱';
(4) 修改 myspace 表空間狀態(tài)
alter tablespace 表空間名稱 online;
35, 刪除表空間
drop tablespace 表空間名稱
including contents and datafiles;
36, 創(chuàng)建臨時表空間
create temporary tablespace 表空間名稱
tempfile '表空間路徑名稱'
size 大小
autoextend on next 自增大小 maxsize ***大小;
37, 創(chuàng)建臨時表空間,將所在組指定為 group1
create temporary tablespace 表空間名稱
tempfile '表空間路徑名稱'
size 大小
tablespace group group1;
38, 修改臨時表空間組
alter tablespace 臨時表空間名稱 tablespace group group2;
39, 創(chuàng)建大文件表空間
create bigfile tabliespace mybigspace
datafile '表空間路徑名稱'
size 大小;
40, 修改默認(rèn)表空間
alter database default tablespace 表空間名稱;
41, 創(chuàng)建事物級臨時表
create global temporary table temp_student(
)on commit delete rows(事物級別的意思);
42, 使用事物級別臨時表
select * from temp_student;
commit; 提交事物。
提交后表還在,但數(shù)據(jù)被清除
43, 創(chuàng)建會話臨時表
create global temporary table temp_book(
)on commit preserve rows(會話級別的意思);
commit;
斷開該用戶連接才會清除數(shù)據(jù)
44,讀取外部文件
首先要在對應(yīng)路徑下有文件
然后 create directory external_card as'E:\external';
創(chuàng)建對應(yīng)路徑
接下來就是創(chuàng)建外部表
create table e_card(
對應(yīng)的數(shù)據(jù)
)organization external(
// 里邊這一團是什么東西噢
type oracle_loader// 指定訪問外部數(shù)據(jù)文件的驅(qū)動程序,oracle 默認(rèn)為 oracle_loader
default directory external_card // 對應(yīng)上一步路徑
access parameters(
fields terminated by ',')
location ('card.txt')
);
45,reject limit 句子的使用
外部表易出錯,用這個就允許有無數(shù)個錯誤
create table e_card(
對應(yīng)的數(shù)據(jù)
)organization external(
type oracle_loader
default directory external_card
access parameters(
fields terminated by ',')
location ('card.txt')
)reject limit unlimited;
46, 將錯誤數(shù)據(jù)存儲到指定文件
create table e_card(
對應(yīng)的數(shù)據(jù)
)organization external(
type oracle_loader
default directory external_card
access parameters(
records delimited by newline
badfile 'card_bad.txt'
fields terminated by ',')
location ('card.txt')
);
47, 錯誤信息日志文件
create table e_card(
對應(yīng)的數(shù)據(jù)
)organization external(
type oracle_loader
default directory external_card
access parameters(
records delimited by newline
badfile 'card_bad.txt'
logfile 'card_log.txt'
fields terminated by ',')
location ('card.txt')
);
48, 創(chuàng)建范圍分區(qū)表
create table part_book(
數(shù)據(jù)庫內(nèi)容
)partition by range(booktime)(
partition part1 values less than ('01-1 月 - 2008')tablespacemytemp1,
partition part2 values less than ('01-1 月 - 2009')tablespacemytemp2,
partition part1 values less than (maxvalue)tablespacemytemp3
)
49, 創(chuàng)建散列分區(qū)表
create table part_book(
數(shù)據(jù)庫內(nèi)容
)partition by hash(bid)(
partition part1 tablespace mytemp1,
partition part2 tablespace mytemp2,
)
50, 創(chuàng)建列表分區(qū)表
create table part_book(
數(shù)據(jù)庫內(nèi)容
)partition by list(bookpress)(
partition part1 values ('清華大學(xué)出版社') tablespace mytemp1,
partition part1 values ('嶺南師范出版社') tablespace mytemp2
)
51,創(chuàng)建組合范圍散列分區(qū)表
create table part_book(
數(shù)據(jù)庫內(nèi)容
)partition by range(booktime)
subpartition by hash(bid)
subpartitions 2 store in(mytemp1,mytemp2)
(
partition part1 values less than ('01-1 月 - 2008'),
partition part1 values less than ('01-1 月 - 2009'),
partition part1 values less than (maxvalue)
);