送你51個Oracle常用語句,看了絕不后悔~
1, 連接數據庫;
connect scott/123456;
斷開數據庫
disconn;
2, 查看表結構
describe scott.emp;
3, 查看數據表
select empno,job,mgr,sal;
4,將緩沖區中的 sql 語句保存到文件
save scott_emp_query.sql
5,將文件內容讀入到緩沖區中
get scott_emp_query.sql list
6,使用 start 命令讀取并運行 sql 文件
start scott_emp_query.sql
7, 將緩沖區內容復制到名為 afiedt.buf 文件 (這個是默認文件) 中
edit
8,將輸出結果保存到指定文件中
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,設置變量定義字符
set define '@'(設置以后定義變量的字符就變為 @,& 無效)
14,執行 sql 文件的語句
@ E:\TEMP\temp.sql (此處應當注意,路徑是區分大小寫的)
15,使用 define 查看常量
define;
16, 設置常量
define temp=7850
查看常量
define temp
刪除常量
undefine temp
17, 使用 column 設置輸出格式
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, 設置一頁顯示多少行數據
set pagesize 20
19, 設置一行顯示多少字符
set linesize 20
20, 設置頁眉頁腳
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, 創建***性表空間
create tablespace myspace
datafile '文件路徑'
size 文件大小
autoextend on next 自動增加大小
maxsize 文件***值
語法說明:1,temporary|undo(創建撤銷表空間)2,teblespace_name 3,datafile|tempfile'file_name'4,size5,reuse 若存在,則刪掉并重新創建6,autoextend off|on7,next number 自動擴展的大小8,maxsize unlimited|number 指定數據文件***大小9,mininum extent number 盤區可以分配到的最小尺寸10,blocksize number 設置數據塊大小11,online|offline 12,logging|nologging13,force logging 強制表空間數據庫對象任何操作都產生日志,否定 1214,default storage storage 指定保存在表空間中的數據庫對象默認存儲參數15,compress|nocompress 是否壓縮數據(消除列中的重復值)16,permanent|temporary 指定表空間中數據的保存形式17,extent management dictionary(數據字典形式管理)|local(本地化形式管理)18,autoallocate|uniform size number 右邊為指定表中盤區大小 19,segment space management auto |manual 指定表空間中段的管理方式
24,查看表空間屬性
select * from dba_tablespace wheretablespace_name='表空間名';
25, 修改表空間狀態
alter tablespace 表空間名 表空間狀態;
26,修改表空間名字
alter tablespace 表空間名 1 rename to 表空間名 2;
27,利用數據字典查看空閑空間信息
select * from dba_free_space where tablespace_name='表空間名稱';
28, 利用數據字典查看表空間數據文件信息
select * from dba_data_files wheretablespace_name='表空間名稱';
29, 修改表空間對應的數據文件大小
alter database
datafile '表空間文件路徑名稱'
resize 大小
30, 為表空間增加新的數據文件
alter tablespace myspace
add datafile '數據文件路徑名稱'
size 大小
autoextend on next 自增大小 maxsize ***值;
31, 刪除 myspace 表空間數據文件
alter tablespace myspace
drop datafile '數據文件名稱';
32,修改 myspace 表空間中數據文件的自動擴展性
alter database
datafile '表空間文件路徑名稱'
autoextend off;
33, 設置表空間文件狀態為 offline drop
alter database
datafile '表空間路徑名稱'
offline drop;
34, 移動表空間中的數據文件
(1)alter tablespace 表空間名稱 offline; 先設置表空間狀態為 offline
(2) 手動操作,將磁盤中的表空間數據文件移動到新的路徑
(3) 使用 alter tablespace 語句修改表空間文件路徑名稱
alter tablespace myspace
rename datafile '表空間路徑名稱'
to
'新的表空間路徑名稱';
(4) 修改 myspace 表空間狀態
alter tablespace 表空間名稱 online;
35, 刪除表空間
drop tablespace 表空間名稱
including contents and datafiles;
36, 創建臨時表空間
create temporary tablespace 表空間名稱
tempfile '表空間路徑名稱'
size 大小
autoextend on next 自增大小 maxsize ***大小;
37, 創建臨時表空間,將所在組指定為 group1
create temporary tablespace 表空間名稱
tempfile '表空間路徑名稱'
size 大小
tablespace group group1;
38, 修改臨時表空間組
alter tablespace 臨時表空間名稱 tablespace group group2;
39, 創建大文件表空間
create bigfile tabliespace mybigspace
datafile '表空間路徑名稱'
size 大小;
40, 修改默認表空間
alter database default tablespace 表空間名稱;
41, 創建事物級臨時表
create global temporary table temp_student(
)on commit delete rows(事物級別的意思);
42, 使用事物級別臨時表
select * from temp_student;
commit; 提交事物。
提交后表還在,但數據被清除
43, 創建會話臨時表
create global temporary table temp_book(
)on commit preserve rows(會話級別的意思);
commit;
斷開該用戶連接才會清除數據
44,讀取外部文件
首先要在對應路徑下有文件
然后 create directory external_card as'E:\external';
創建對應路徑
接下來就是創建外部表
create table e_card(
對應的數據
)organization external(
// 里邊這一團是什么東西噢
type oracle_loader// 指定訪問外部數據文件的驅動程序,oracle 默認為 oracle_loader
default directory external_card // 對應上一步路徑
access parameters(
fields terminated by ',')
location ('card.txt')
);
45,reject limit 句子的使用
外部表易出錯,用這個就允許有無數個錯誤
create table e_card(
對應的數據
)organization external(
type oracle_loader
default directory external_card
access parameters(
fields terminated by ',')
location ('card.txt')
)reject limit unlimited;
46, 將錯誤數據存儲到指定文件
create table e_card(
對應的數據
)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(
對應的數據
)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, 創建范圍分區表
create table part_book(
數據庫內容
)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, 創建散列分區表
create table part_book(
數據庫內容
)partition by hash(bid)(
partition part1 tablespace mytemp1,
partition part2 tablespace mytemp2,
)
50, 創建列表分區表
create table part_book(
數據庫內容
)partition by list(bookpress)(
partition part1 values ('清華大學出版社') tablespace mytemp1,
partition part1 values ('嶺南師范出版社') tablespace mytemp2
)
51,創建組合范圍散列分區表
create table part_book(
數據庫內容
)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)
);