1.在操作系统创建3个目录
[oracle@hxy ~]$ cd ext
[oracle@hxy ext]$ mkdir data
[oracle@hxy ext]$ mkdir log
[oracle@hxy ext]$ mkdir bad
2.在/home/oracle/ext/data下创建两个数据源文件
[oracle@hxy ext]$ cd data/
[oracle@hxy data]$ touch empxt1.dat
[oracle@hxy data]$ touch empxt2.dat
添加数据:
[oracle@hxy data]$ vi empxt1.dat
001,haoxiaoyu,clerk,7902,17-DEC-80,100,0,20
002,haoxiaoyu1,clerk,7902,17-DEC-80,100,0,20
003,haoxiaoyu2,clerk,7902,17-DEC-80,100,0,20
004,haoxiaoyu3,clerk,7902,17-DEC-80,100,0,20
[oracle@hxy data]$ vi empxt2.dat
7564,zhangzhongshuai,salesman,7892,28-SEp-81,1250,0,30
7564,ngzhongshuai1,salesman,7892,28-SEp-81,1250,0,30
1243,zhangzhongshuai2,salesman,7892,28-SEp-81,1250,0,30
3.以sys登陆创建3个目录对象
SQL> conn / as sysdba
Connected.
SQL> create or replace directory datadir as '/home/oracle/ext/data';
Directory created.
SQL> create or replace directory logdir as '/home/oracle/ext/log';
Directory created.
SQL> create or replace directory baddir as '/home/oracle/ext/bad';
Directory created.
SQL> grant read on directory datadir to scott;
Grant succeeded.
SQL> grant write on directory logdir to scott;
Grant succeeded.
SQL> grant write on directory baddir to scott;
Grant succeeded.
SQL>
4.以scott登陆,创建外部表
create table ext_employee
(
emp_id number(4),
ename varchar2(100),
job varchar2(10),
mgr_id number(4),
hiredate date,
salary number(8,2),
comm number(8,2),
dept_id number(2)
)
organization external
(
type oracle_loader
default directory datadir
access parameters
(
records delimited by newline
badfile baddir:'empxt%a_%p.bad'
logfile logdir:'empxt%a_%p.log'
fields terminated by ','
missing field values are null
(
emp_id,ename,job,mgr_id,
hiredate char date_format date mask "dd-mon-yyyy",
salary,comm,dept_id
)
)
location('empxt1.dat','empxt2.dat')
)
parallel
reject limit unlimited;
~
5。查询
SQL> select * from ext_employee;
EMP_ID
----------
ENAME
--------------------------------------------------------------------------------
JOB MGR_ID HIREDATE SALARY COMM DEPT_ID
---------- ---------- --------- ---------- ---------- ----------
7564
zhangzhongshuai
salesman 7892 28-SEP-81 1250 0 30
7564
ngzhongshuai1
salesman 7892 28-SEP-81 1250 0 30
EMP_ID
----------
ENAME
--------------------------------------------------------------------------------
JOB MGR_ID HIREDATE SALARY COMM DEPT_ID
---------- ---------- --------- ---------- ---------- ----------
1243
zhangzhongshuai2
salesman 7892 28-SEP-81 1250 0 30
1
haoxiaoyu
EMP_ID
----------
ENAME
--------------------------------------------------------------------------------
JOB MGR_ID HIREDATE SALARY COMM DEPT_ID
---------- ---------- --------- ---------- ---------- ----------
clerk 7902 17-DEC-80 100 0 20
2
haoxiaoyu1
clerk 7902 17-DEC-80 100 0 20
3
EMP_ID
----------
ENAME
--------------------------------------------------------------------------------
JOB MGR_ID HIREDATE SALARY COMM DEPT_ID
---------- ---------- --------- ---------- ---------- ----------
haoxiaoyu2
clerk 7902 17-DEC-80 100 0 20
4
haoxiaoyu3
clerk 7902 17-DEC-80 100 0 20
7 rows selected.
记住:创建外部表查不到数据一定是外部表创建语法有错误!语法正确一定能创建成功,语法错误也可能创建成功!