Oracle外部表

1.外部表简介

外部表是Oracle9i之后来使用的。外部表是一类表的定义存在于数据库而数据不存在于数据库的表。

在数据库操作过程中可以对外部表进行select,join,sort操作也可以对外部表创建视图和同义词。但是不能在外部表上进行DML操作和创建索引。

外部表提供两种访问驱动。一种ORACLE_LOADER另一种ORACLE_DATAPUMP。默认驱动是ORACLE_LOADER。

ORACLE_LOADER驱动从外部文件中读数据。ORACLE_LOADER创建外部表的语法和SQL*Loader utility有异曲同工之妙。

ORACLE_DATAPUMP先通过外部表unload出数据然后通过外部表reload表。具体下面小节分析。

另外外部表的统计信息收集支持DBMS_STATS包但不支持ANALYZE。

而且外部表不支持虚拟列。

2.创建外部表

[[email protected] mydir]$ cat samp1.txt

360,Jane,Janus,ST_CLERK,121,17-MAY-2001,3000,0,50,jjanus

361,Mark,Jasper,SA_REP,145,17-MAY-2001,8000,.1,80,mjasper

362,Brenda,Starr,AD_ASST,200,17-MAY-2001,5500,0,10,bstarr

363,Alex,Alda,AC_MGR,145,17-MAY-2001,9000,.15,80,aalda

401,Jesse,Cromwell,HR_REP,203,17-MAY-2001,7000,0,40,jcromwel

402,Abby,Applegate,IT_PROG,103,17-MAY-2001,9000,.2,60,aapplega

403,Carol,Cousins,AD_VP,100,17-MAY-2001,27000,.3,90,ccousins

404,John,Richardson,AC_ACCOUNT,205,17-MAY-2001,5000,0,110,jrichard

SQL> create or replace directory mydir as ‘/home/oracle/mydir‘;

Directory created.

SQL> grant read,write on directory mydir to scott;

Grant succeeded.

SQL> conn scott/tiger

Connected.

CREATE TABLE admin_ext_employees

(employee_id       NUMBER(4),

first_name        VARCHAR2(20),

last_name         VARCHAR2(25),

job_id            VARCHAR2(10),

manager_id        NUMBER(4),

hire_date         DATE,

salary            NUMBER(8,2),

commission_pct    NUMBER(2,2),

department_id     NUMBER(4),

email             VARCHAR2(25)

)

ORGANIZATION EXTERNAL

(

TYPE ORACLE_LOADER

DEFAULT DIRECTORY mydir

ACCESS PARAMETERS

(

records delimited by newline

badfile mydir:‘badxt%a_%p.bad‘

logfile mydir:‘logxt%a_%p.log‘

fields terminated by ‘,‘

missing field values are null

( employee_id, first_name, last_name, job_id, manager_id,

hire_date char date_format date mask "dd-mon-yyyy",

salary, commission_pct, department_id, email

)

)

LOCATION (‘samp1.txt‘)

)

PARALLEL REJECT LIMIT UNLIMITED;

PARALLEL                 --指定查询时的并行度

REJECT LIMIT UNLIMITED  --指定外部表查询错误数量

执行成功看产生的日志。

[[email protected] mydir]$ ls -al

total 20

drwxr-xr-x 2 oracle oinstall 4096 Nov 20 16:12 .

drwx------ 5 oracle oinstall 4096 Nov 20 15:53 ..

-rw-r--r-- 1 oracle oinstall 1413 Nov 20 16:12 logxt000_13688.log

-rw-r--r-- 1 oracle oinstall 1555 Nov 20 16:12 logxt000_16408.log

-rw-r--r-- 1 oracle oinstall  480 Nov 20 15:53 samp1.txt

[[email protected] mydir]$ cat logxt000_13688.log

LOG file opened at 11/20/15 16:12:03

Field Definitions for table ADMIN_EXT_EMPLOYEES

Record format DELIMITED BY NEWLINE

Data in file has same endianness as the platform

Rows with all null fields are accepted

Fields in Data Source:

EMPLOYEE_ID                     CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

FIRST_NAME                      CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

LAST_NAME                       CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

JOB_ID                          CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

MANAGER_ID                      CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

HIRE_DATE                       CHAR (80)

Date datatype DATE, date mask dd-mon-yyyy

Terminated by ","

Trim whitespace same as SQL Loader

SALARY                          CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

COMMISSION_PCT                  CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

DEPARTMENT_ID                   CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

EMAIL                           CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

[[email protected] mydir]$ cat logxt000_16408.log

LOG file opened at 11/20/15 16:12:03

Field Definitions for table ADMIN_EXT_EMPLOYEES

Record format DELIMITED BY NEWLINE

Data in file has same endianness as the platform

Rows with all null fields are accepted

Fields in Data Source:

EMPLOYEE_ID                     CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

FIRST_NAME                      CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

LAST_NAME                       CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

JOB_ID                          CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

MANAGER_ID                      CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

HIRE_DATE                       CHAR (80)

Date datatype DATE, date mask dd-mon-yyyy

Terminated by ","

Trim whitespace same as SQL Loader

SALARY                          CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

COMMISSION_PCT                  CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

DEPARTMENT_ID                   CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

EMAIL                           CHAR (255)

Terminated by ","

Trim whitespace same as SQL Loader

Date Cache Statistics for table ADMIN_EXT_EMPLOYEES

Max Size:      1000

Entries :         1

Hits    :         7

Misses  :         0

logxt000_16408.log日志比logxt000_13688.log尾部多了一些统计信息。

查看外部表

SQL>  select EMPLOYEE_ID,FIRST_NAME from ADMIN_EXT_EMPLOYEES;

EMPLOYEE_ID FIRST_NAME

----------- --------------------

360 Jane

361 Mark

362 Brenda

363 Alex

401 Jesse

402 Abby

403 Carol

404 John

3.外部表修改

能够使用于alter table下面子句来修改外部表访问参数

reject limit:ALTER TABLE admin_ext_employees REJECT LIMIT n;

project column: ALTER TABLE admin_ext_employees PROJECT COLUMN REFERENCED;

ALTER TABLE admin_ext_employees PROJECT COLUMN ALL;

default dectory:ALTER TABLE admin_ext_employees DEFAULT DIRECTORY admin_dat2_dir;

查看修改的属性

SQL> select OWNER,TABLE_NAME,REJECT_LIMIT,DEFAULT_DIRECTORY_NAME,PROPERTY from dba_external_tables;

OWNER   TABLE_NAME  REJECT_LIM DEFAULT_DIRECTORY_NAME    PROPERTY

---------- ------------------------------ ---------- ------------------------------ ----------

SCOTT   ADMIN_EXT_EMPLOYEES  UNLIMITED  MYDIR    ALL

4.预处理外部表

SQL> create or replace directory mydir as ‘/home/oracle/mydir‘;

Directory created.

SQL> grant read,write,execute on directory mydir to scott;

Grant succeeded.

在/home/oracle/mydir下面创建一个文件

[[email protected] mydir]$ cat uncompress

/bin/gzip -cd $1

[[email protected] mydir]$ chmod +x uncompress

SQL> conn scott/tiger

Connected.

CREATE TABLE admin_ext_employees

(employee_id       NUMBER(4),

first_name        VARCHAR2(20),

last_name         VARCHAR2(25),

job_id            VARCHAR2(10),

manager_id        NUMBER(4),

hire_date         DATE,

salary            NUMBER(8,2),

commission_pct    NUMBER(2,2),

department_id     NUMBER(4),

email             VARCHAR2(25)

)

ORGANIZATION EXTERNAL

(

TYPE ORACLE_LOADER

DEFAULT DIRECTORY mydir

ACCESS PARAMETERS

(

records delimited by newline

PREPROCESSOR exec_file_dir:‘uncompress‘

badfile mydir:‘badxt%a_%p.bad‘

logfile mydir:‘logxt%a_%p.log‘

fields terminated by ‘,‘

missing field values are null

( employee_id, first_name, last_name, job_id, manager_id,

hire_date char date_format date mask "dd-mon-yyyy",

salary, commission_pct, department_id, email

)

)

LOCATION (‘samp1.txt.gz‘)

)

PARALLEL

REJECT LIMIT UNLIMITED;

5.外部表的删除使用DROP TABLE语句这条语句仅仅删除数据库中的介质,对实际数据没有影响。

SQL> drop table admin_ext_employees;

Table dropped.

6.有关外部表的系统权限和对象。

外部表的系统权限

CREATE ANY TABLE

ALTER ANY TABLE

DROP ANY TABLE

SELECT ANY TABLE

外部表的对象权限

ALTER

SELECT

时间: 2024-10-29 04:48:47

Oracle外部表的相关文章

如何利用Oracle外部表导入文本文件的数据

同事最近在忙数据一致性比对工作,需要对不同文本文件中的数据进行比对,有的文件较大,记录较多,如果用普通的文本编辑器打开的话,很显然,会很卡,甚至打不开. 基于此,可将该文本文件的数据导入到数据库中,在集合的层面进行比对. 那么如何将文本文件的数据导入到数据库中呢?在这里,主要利用了Oracle的外部表特性. Oracle外部表支持两种类型的驱动:一种是ORACLE_LOADER,外部表的数据必须来源于文件文件,另一种则是ORACLE_DATAPUMP,外部表的数据必须是二进制dump文件,该du

ORACLE外部表总结

外部表介绍 ORACLE外部表用来存取数据库以外的文本文件(Text File)或ORACLE专属格式文 件.因此,建立外部表时不会产生段.区.数据块等存储结构,只有与表相关的定义放在数据字典中.外部表,顾名思义,存储在数据库外面的表.当存取时才能从 ORACLE专属格式文件中取得数据,外部表仅供查询,不能对外部表的内容进行修改(INSERT.UPDATE.DELETE操作).不能对外部表建立 索引.因为创建索引就意味着要存在对应的索引记录.而外部表其实在没有存储在数据库中.故在外部是无法建立索

Oracle外部表详解(转载)

(外部表创建主要注意创建目录访问权限问题.目录路径格式无空格等不相关字符,即必须是当前表访问用户可以访问:关于表中行数的限制问题,如果不加限制注意添加reject limit unlimited:表中数据格式与创建表时access parameters中的定义需保持同步,适当用skip=1) 外部表概述 外部表只能在Oracle 9i之后来使用.简单地说,外部表,是指不存在于数据库中的表.通过向Oracle提供描述外部表的元数据,我们可以把一个操作系统文件当成一个只读的数据库表,就像这些数据存储

oracle 外部表

语法: create table 表名( 列名1,列名2,...... ) organization external  ###说明创建外部表 ( type 访问类型            ###一般是 ORACLE_LOADER ,ORACLE_DATAPUMP default directory   路径名   ####指定默认目录对象 access parameter (        ###数据源文件与表中行之间的映射关系 records delimited by 设置分隔符    ##

oracle 外部表查alter日志

--创建文件夹,路径是alter日志的路径 create or replace directory data_dir as '/u01/app/oracle/diag/rdbms/orcl/orcl/trace'; --创建外部表 create table alert_log( text_line varchar2(225) ) organization external ( TYPE ORACLE_LOADER DEFAULT DIRECTORY DATA_DIR ACCESS PARAMET

使用ORACLE外部表装载复杂数据

原文:http://www.oracle.com/technetwork/issue-archive/2013/13-jan/o13asktom-1886639.html I am using SQL Loader to load data into tables from a flat file. Some sample data in the file might be: 我打算使用SQL Loader装载来自平面文件数据.样例数据如下: 12,smith,[email protected]

Oracle外部表的管理和应用

外部表作为oracle的一种表类型,虽然不能像普通库表那么应用方便,但有时在数据迁移或数据加载时,也会带来极大的方便,有时比用sql*loader加载数据来的更为方便,下面就将建立和应用外部表的命令和操作记录如下: --创建存放文件的directory对象 sqlplus username/[email protected] create directory ex_data as '/home/ex_data/'; --创建外部表 CREATE TABLE tab1_ex ( C1 VARCHA

oracle 外部表之 sqlldr 生成

oracle的导入工具,速度挺快,这里记一下外部表的使用,通常外部表要记的语法太多例如: CREATE TABLE PROD_MASTER ( "EMPNO" NUMBER, "ENAME" VARCHAR2(50), "HIREDATE" DATE, "DEPTNO" NUMBER ) ORGANIZATION external ( TYPE oracle_loader DEFAULT DIRECTORY ext_table

Oracle 创建外部表

Oracle 外部表能迅速的将海量的数据导入到数据库里面,外部表的创建使用步骤如下: 1 创建一个Directory:必须用sys用户创建,用户存放外部数据文件. create directory DIR_DATA as '/home/oracle/oradata/IN'; 给用户授权目录的使用权限,否则用户不能访问该目录以及其下的文件 grant write,read on directory DIR_DATA to Test; 2 将要导入的外部数据文件,Test_File.csv 放到目录