温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Oracle Externale Tables

发布时间:2020-08-09 09:42:13 来源:ITPUB博客 阅读:183 作者:chenoracle 栏目:关系型数据库
Oracle Externale Tables 


Creating External Tables
You create external tables using the CREATE TABLE statement with an ORGANIZATION EXTERNAL clause.  This statement creates only metadata in the data dictionary.
实验:创建外部表并加载数据;
EXAMPLE: Creating an External Table and Loading Data

In this example, the data for the external table resides in the two text files empxt1.dat and empxt2.dat.

The file empxt1.dat contains the following sample data:
数据一:
[oracle@chen test]$ cat empxt1.dat 
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

The file empxt2.dat contains the following sample data:
数据二:
[oracle@chen test]$ cat empxt2.dat 
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

创建外部表语句如下:
[root@chen ~]# mkdir /flatfiles/{data,log,bad} -p
[root@chen ~]# chown oracle.oinstall /flatfiles/ -R

[oracle@chen test]$ cat create_external.sql
CONNECT  /  AS SYSDBA;
-- Set up directories and grant access to hr 
CREATE OR REPLACE DIRECTORY admin_dat_dir
    AS '/flatfiles/data'; 
CREATE OR REPLACE DIRECTORY admin_log_dir 
    AS '/flatfiles/log'; 
CREATE OR REPLACE DIRECTORY admin_bad_dir 
    AS '/flatfiles/bad'; 
GRANT READ ON DIRECTORY admin_dat_dir TO hr; 
GRANT WRITE ON DIRECTORY admin_log_dir TO hr; 
GRANT WRITE ON DIRECTORY admin_bad_dir TO hr;
-- hr connects. Provide the user password (hr) when prompted.
CONNECT hr
-- create the external table
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 admin_dat_dir 
       ACCESS PARAMETERS 
       ( 
         records delimited by newline 
         badfile admin_bad_dir:'empxt%a_%p.bad' 
         logfile admin_log_dir:'empxt%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 ('empxt1.dat', 'empxt2.dat') 
     ) 
     PARALLEL 
     REJECT LIMIT UNLIMITED; 

验证数据
SQL> set linesize 1000
SQL> select * from  admin_ext_employees;
EMPLOYEE_ID FIRST_NAME           LAST_NAME                 JOB_ID     MANAGER_ID HIRE_DATE     SALARY COMMISSION_PCT DEPARTMENT_ID EMAIL
----------- -------------------- ------------------------- ---------- ---------- --------- ---------- -------------- ------------- -------------------------
        401 Jesse                Cromwell                  HR_REP            203 17-MAY-01       7000              0            40 jcromwel
        402 Abby                 Applegate                 IT_PROG           103 17-MAY-01       9000             .2            60 aapplega
        403 Carol                Cousins                   AD_VP             100 17-MAY-01      27000             .3            90 ccousins
        404 John                 Richardson                AC_ACCOUNT        205 17-MAY-01       5000              0           110 jrichard
        360 Jane                 Janus                     ST_CLERK          121 17-MAY-01       3000              0            50 jjanus
        361 Mark                 Jasper                    SA_REP            145 17-MAY-01       8000             .1            80 mjasper
        362 Brenda               Starr                     AD_ASST           200 17-MAY-01       5500              0            10 bstarr
        363 Alex                 Alda                      AC_MGR            145 17-MAY-01       9000            .15            80 aalda

8 rows selected.

外部表不会在数据库里创建对应的表段;
SQL> select * from user_segments;
no rows selected

外部表只读,不能进行DML操作
SQL> delete admin_ext_employees where employee_id=363;
delete admin_ext_employees where employee_id=363
       *
ERROR at line 1:
ORA-30657: operation not supported on external organized table

无法通过analyze收集外部表的统计信息(可以通过dbms_stats收集)
SQL> analyze table admin_ext_employees compute statistics;
analyze table admin_ext_employees compute statistics
              *
ERROR at line 1:
ORA-30657: operation not supported on external organized table

SQL>  exec dbms_stats.gather_schema_stats('HR');
PL/SQL procedure successfully completed.

更详细的信息:

http://docs.oracle.com/cd/E11882_01/server.112/e25494/tables.htm#ADMIN11706



欢迎关注我的微信公众号"IT小Chen",共同学习,共同成长!!!

Oracle Externale Tables

Oracle Externale Tables



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI