外部表

1. 定义:不在Oracle内部存储,数据存储在操作系统(OS)层面,原数据(Metadata)存储在数据库当中,外部表的数据只能读操作,无法进行DML操作,不能建立索引、约束、LOB大对象,但可以建立表、视图及同义词。

2. 创建外部表流程:

  1. 创建DIRECTORY,并授权给用户

  2. 用户在自己当前模式下,根据OS层面文件的内容创建外部表

  3. 查看外部表

3. 实验如下:

假设在OS层面 ” /u01/app/test/ “ 目录下存在一个test.txt文件,文件内容如下,在Scott用户模式下创建外部表,并查看该文件中的内容

[oracle@oracle12c test]$ pwd


/u01/app/test


[oracle@oracle12c test]$ cat test.txt


1,a,10
2,b,20
3,c,30

用SYS登录ERP数据库,创建名为 test_dir  的 DIRECTORY,并授权给SCOTT用户

SQL> create directory test_dir as '/u01/app/test'; 

Directory created.

SQL> grant all on directory test_dir to scott;

在SCOTT用户模式下,创建外部表,创建脚本如下:

[oracle@oracle12c test]$ cat test_dir.sql
create table test_dir_a  ----外部表表名
(id number,name varchar2(10),deptno number)
organization external
(
type oracle_loader
default directory test_dir ----SYS用户创建的DIRECTORY名称
access parameters
(
records delimited by newline
badfile 'bad.txt'
logfile 'log.txt'-----可以自定义名称
fields terminated by ','
missing field values are null
(id,name,deptno)
)
location('test.txt') ---- OS层面文件名
)
PARALLEL
REJECT LIMIT UNLIMITED;

在数据库中查看外部表

SQL> conn scott/tiger@erp
Connected.
SQL> @/u01/app/test/test_dir.sql

Table created.

SQL> select * from test_dir_a;

ID          NAME       DEPTNO
---------- -------------------- ----------
1      a        10
2      b        20
3      c        30

SQL>

原文地址:https://www.cnblogs.com/eniniemand/p/14055663.html