外表(external table)就像普通的表對像一樣
可以select等
只是它是只讀的
數據庫中只保存了表結構的描述
表數據卻沒有存放在數據庫內
而是存放在了文件系統上
當用戶想偶爾使用數據庫外的結構化數據時
用起外表來就非常方便
甚至比sqlldr都要方便的多
在這篇文章裡
我們為大家演示了三步就掌握oracle外表過程
通過這次學習
也許大家就會發展原來學習oracle也是好容易哦
第一步創建目錄並授權 目錄是數據文件的存放目標
數據文件通常要求是文本文件
這個過程在
i以前是需要配置utl_file_dir參數的
sys@TEST>!ls /home/oracle/temp
user
ctl userlist
txt user
log
rudolf@TEST> sys@TEST>conn system/alibaba
Connected
sys@TEST>
sys@TEST>CREATE DIRECTORY
TEMP AS
/home/oracle/temp/
;
Directory created
sys@TEST>grant read
write on directory TEMP to rudolf;
Grant succeeded
第二步創建外表與測試 rudolf@TEST>CREATE TABLE
USERLIST
(
ID NUMBER
USERNAME VARCHAR
(
)
EMAIL VARCHAR
(
)
)
ORGANIZATION external
(
TYPE oracle_loader
DEFAULT DIRECTORY TEMP
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE CHARACTERSET US
ASCII
BADFILE
TEMP
:
userlist
bad
DISCARDFILE
TEMP
:
userlist
dis
LOGFILE
TEMP
:
user
log
READSIZE
FIELDS TERMINATED BY
OPTIONALLY ENCLOSED BY
LDRTRIM
MISSING FIELD VALUES ARE NULL
REJECT ROWS WITH ALL NULL FIELDS
(
ID CHAR(
)
TERMINATED BY
OPTIONALLY ENCLOSED BY
USERNAME CHAR(
)
TERMINATED BY
OPTIONALLY ENCLOSED BY
EMAIL CHAR(
)
TERMINATED BY
OPTIONALLY ENCLOSED BY
)
)
location
(
userlist
txt
)
)REJECT LIMIT UNLIMITED
rudolf@TEST>/
Table created
rudolf@TEST>l
select id
username from userlist where rownum <
* rudolf@TEST>/
ID USERNAME
RudolfLu
tomgu
coug
chao_ping
parrotao
cnoug
FilsDeDragon
Dragon
rows selected
第三步理解外表數據結構與create table organization external語法 大家都用過sqlldr吧?外表的數據文件的結構呢就同sqlldr能讀的數據文件結構一樣了
那麼語法呢?嘿嘿
別急
讓我們先來做個sqlldr的練習吧
[oracle@rac
temp]$ head
userlist
txt
RudolfLu
tomgu
coug
chao_ping
parrotao
cnoug
FilsDeDragon
Dragon
Xavier
[oracle@rac
temp]$ cat user
ctl
LOAD INFILE
/home/oracle/temp/userlist
txt
badfile
/home/oracle/temp/userlist
bad
discardfile
/home/oracle/temp/userlist
dis
APPEND INTO TABLE userlist fields terminated by
optionally enclosed by
trailing nullcols
( id char(
)
username char(
) )
rudolf@TEST>create table userlist
(id number
username varchar
(
)
);
Table created
rudolf@TEST>!
[oracle@rac
temp]$ sqlldr rudolf/nix@test
world control=
/user
ctl external_table=GENERATE_ONLY
注意
我們加了一個external_table的參數
它的作用是告訴sqlldr不用真實load數據
而是生成包含external table 創建腳本的log文件
[oracle@rac
temp]$ ls
user
ctl
userlist
txt
user
log [oracle@rac
temp]$ cat user
log
SQL*Loader: Release
Production on Wed Dec
:
:
Copyright (c)
Oracle Corporation
All rights reserved
Control File:
/user
ctl
Data File:
/home/oracle/temp/userlist
txt
Bad File:
/home/oracle/temp/userlist
bad
Discard File: /home/oracle/temp/userlist
dis
CREATE DIRECTORY statements needed for files
CREATE DIRECTORY SYS_SQLLDR_XT_TMPDIR_
AS
/home/oracle/temp/
CREATE TABLE statement for external table:
CREATE TABLE
SYS_SQLLDR_X_EXT_USERLIST
(
ID NUMBER
USERNAME VARCHAR
(
) )
ORGANIZATION external
(
TYPE oracle_loader
DEFAULT DIRECTORY SYS_SQLLDR_XT_TMPDIR_
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE CHARACTERSET US
ASCII
BADFILE
SYS_SQLLDR_XT_TMPDIR_
:
userlist
bad
DISCARDFILE
SYS_SQLLDR_XT_TMPDIR_
:
userlist
dis
LOGFILE
user
log_xt
READSIZE
FIELDS TERMINATED BY
OPTIONALLY ENCLOSED BY
LDRTRIM
MISSING FIELD VALUES ARE NULL
REJECT ROWS WITH ALL NULL FIELDS
(
ID CHAR(
)
TERMINATED BY
OPTIONALLY ENCLOSED BY
USERNAME CHAR(
)
TERMINATED BY
OPTIONALLY ENCLOSED BY
)
)
location
(
userlist
txt
)
)REJECT LIMIT UNLIMITED
From:http://tw.wingwit.com/Article/program/Oracle/201311/18672.html