外部表
二,举几个栗子
一,使用sql*loader生成创建外部表语句
二.ORACLE_LOADER驱动方式
三,使用 ORACLE_DATAPUMP 卸载/装载数据
一,How Are External Tables Created?
External tables are created using the SQL CREATE TABLE...ORGANIZATION EXTERNAL statement. When you create an external table, you specify the following attributes:
TYPE- specifies the type of external table. The two available types are theORACLE_LOADERtype and theORACLE_DATAPUMPtype. Each type of external table is supported by its own access driver.The
ORACLE_LOADERaccess driver is the default. It loads data from external tables to internal tables. The data must come from text data files. (TheORACLE_LOADERaccess driver cannot perform unloads; that is, it cannot move data from an internal table to an external table.)The
ORACLE_DATAPUMPaccess driver can perform both loads and unloads. The data must come from binary dump files. Loads to internal tables from external tables are done by fetching from the binary dump files. Unloads from internal tables to external tables are done by populating the binary dump files of the external table. TheORACLE_DATAPUMPaccess driver can write dump files only as part of creating an external table with the SQLCREATE TABLE AS SELECTstatement. Once the dump file is created, it can be read any number of times, but it cannot be modified (that is, no DML operations can be performed).
DEFAULTDIRECTORY- specifies the default directory to use for all input and output files that do not explicitly name a directory object. The location is specified with a directory object, not a directory path. You must create the directory object before you create the external table; otherwise, an error is generated. See “Location of Data Files and Output Files” for more information.ACCESSPARAMETERS- describe the external data source and implements the type of external table that was specified. Each type of external table has its own access driver that provides access parameters unique to that type of external table. Access parameters are optional. See “Access Parameters”.LOCATION- specifies the data files for the external table. The files are named in the formdirectory:file. Thedirectoryportion is optional. If it is missing, then the default directory is used as the directory for the file.
The following example shows the use of each of these attributes (it assumes that the default directory def_dir1 already exists):
SQL> CREATE TABLE emp_load2 (employee_number CHAR(5),3 employee_dob CHAR(20),4 employee_last_name CHAR(20),5 employee_first_name CHAR(15),6 employee_middle_name CHAR(15),7 employee_hire_date DATE)8 ORGANIZATION EXTERNAL9 (TYPE ORACLE_LOADER10 DEFAULT DIRECTORY def_dir111 ACCESS PARAMETERS12 (RECORDS DELIMITED BY NEWLINE13 FIELDS (employee_number CHAR(2),14 employee_dob CHAR(20),15 employee_last_name CHAR(18),16 employee_first_name CHAR(11),17 employee_middle_name CHAR(11),18 employee_hire_date CHAR(10) date_format DATE mask "mm/dd/yyyy"19 )20 )21 LOCATION ('info.dat')22 );Table created.
The information you provide through the access driver ensures that data from the data source is processed so that it matches the definition of the external table. The fields listed after CREATE TABLE emp_load are actually defining the metadata for the data in the info.dat source file.
二,举几个栗子
一,使用sql*loader生成创建外部表语句
1.数据源
t.dat
P,James,31,P,Thomas,22,E,Pat,38,93645,1122,Engineering,P,Bill,19,P,Scott,55,S,Judy,45,27316,English,S,Karen,34,80356,History,E,Karen,61,90056,1323,"Manufa,cturing",S,Pat,29,98625,Spanish,S,Cody,22,99743,Math,P,Ted,43,E,Judy,44,87616,1544,Accounting,E,Bob,50,63421,1314,Shipping,S,Bob,32,67420,Psychology,E,Cody,33,25143,1002,"Hum,an Resources",
**
2.创建控制文件**
t.ctl
load datainfile '/home/oracle/t.dat'badfile '/home/oracle/t.bad'discardfile '/home/oracle/t.dec'appendinto table tfields terminated by ',' optionally enclosed by '"'trailing nullcols(x1,x2,x3,x4,x5,x6)
3.创建表T
create table t(x1 varchar2(20),x2 varchar2(20),x3 varchar2(20),x4 varchar2(20),x5 varchar2(20),x6 varchar2(20));
sqlldr test/test control=/tmp/t.ctl log=/home/oracle/t.log external_table=GENERATE_ONLY
4.修改生成的日志文件,并执行
删除刚才建的表T
CREATE DIRECTORY SYS_SQLLDR_XT_TMPDIR_00000 AS '/tmp/';CREATE TABLE t ---不加引号,如果加引号,查询的时候表名也必须加引号("X1" VARCHAR2(20),"X2" VARCHAR2(20),"X3" VARCHAR2(20),"X4" VARCHAR2(20),"X5" VARCHAR2(20),"X6" VARCHAR2(20))ORGANIZATION external(TYPE oracle_loaderDEFAULT DIRECTORY SYS_SQLLDR_XT_TMPDIR_00000ACCESS PARAMETERS(RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCIIBADFILE 'SYS_SQLLDR_XT_TMPDIR_00000':'t.bad'DISCARDFILE 'SYS_SQLLDR_XT_TMPDIR_00000':'t.dec'READSIZE 1048576FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' LDRTRIMMISSING FIELD VALUES ARE NULLREJECT ROWS WITH ALL NULL FIELDS("X1" CHAR(255)TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',"X2" CHAR(255)TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',"X3" CHAR(255)TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',"X4" CHAR(255)TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',"X5" CHAR(255)TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',"X6" CHAR(255)TERMINATED BY "," OPTIONALLY ENCLOSED BY '"'))location('t.dat'))REJECT LIMIT UNLIMITED;
SQL> set line 150 pages 999SQL> select * from t;X1 X2 X3 X4 X5 X6-------------------- -------------------- -------------------- -------------------- -------------------- --------------------P James 31P Thomas 22E Pat 38 93645 1122 EngineeringP Bill 19P Scott 55S Judy 45 27316 EnglishS Karen 34 80356 HistoryE Karen 61 90056 1323 Manufa,cturingS Pat 29 98625 SpanishS Cody 22 99743 MathP Ted 43E Judy 44 87616 1544 AccountingE Bob 50 63421 1314 ShippingS Bob 32 67420 PsychologyE Cody 33 25143 1002 Hum,an Resources15 rows selected.
二.ORACLE_LOADER驱动方式
通过sqlldr引擎方式加载,访问平面文件
Assume your
.datfile looks as follows:56november, 15, 1980 baker mary alice 09/01/2004 87december, 20, 1970 roper lisa marie 01/01/2002
Execute the following SQL statements to set up a default directory (which contains the data source) and to grant access to it:
CREATE DIRECTORY def_dir1 AS '/usr/apps/datafiles'; GRANT READ ON DIRECTORY ext_tab_dir TO SCOTT;
Create a traditional table named
emp:CREATE TABLE emp (emp_no CHAR(6), last_name CHAR(25), first_name CHAR(20), middle_initial CHAR(1), hire_date DATE, dob DATE);
Create an external table named
emp_load:SQL> CREATE TABLE emp_load 2 (employee_number CHAR(5), 3 employee_dob CHAR(20), 4 employee_last_name CHAR(20), 5 employee_first_name CHAR(15), 6 employee_middle_name CHAR(15), 7 employee_hire_date DATE) 8 ORGANIZATION EXTERNAL 9 (TYPE ORACLE_LOADER 10 DEFAULT DIRECTORY def_dir1 11 ACCESS PARAMETERS 12 (RECORDS DELIMITED BY NEWLINE 13 FIELDS (employee_number CHAR(2), 14 employee_dob CHAR(20), 15 employee_last_name CHAR(18), 16 employee_first_name CHAR(11), 17 employee_middle_name CHAR(11), 18 employee_hire_date CHAR(10) date_format DATE mask "mm/dd/yyyy" 19 ) 20 ) 21 LOCATION ('info.dat') 22 ); Table created.Load the data from the external table
emp_loadinto the tableemp:SQL> INSERT INTO emp (emp_no, 2 first_name, 3 middle_initial, 4 last_name, 5 hire_date, 6 dob) 7 (SELECT employee_number, 8 employee_first_name, 9 substr(employee_middle_name, 1, 1), 10 employee_last_name, 11 employee_hire_date, 12 to_date(employee_dob,'month, dd, yyyy') 13 FROM emp_load); 2 rows created.
Perform the following select operation to verify that the information in the
.datfile was loaded into theemptable:SQL> SELECT * FROM emp; EMP_NO LAST_NAME FIRST_NAME M HIRE_DATE DOB
56 baker mary a 01-SEP-04 15-NOV-80 87 roper lisa m 01-JAN-02 20-DEC-70 2 rows selected.
Notes about this example:
The
employee_numberfield in the data file is converted to a character string for theemployee_numberfield in the external table.The data file contains an
employee_dobfield that is not loaded into any field in the table.The
substrfunction is used on theemployee_middle_namecolumn in the external table to generate the value formiddle_initialin tableemp.The character string for
employee_hire_dateininfo.datis automatically converted into aDATEdatatype at external table access time, using the format mask specified in the external table definiition.Unlike
employee_hire_date, theDATEdatatype conversion foremployee_dobis done atSELECTtime and is not part of the external table definition.
三,使用 ORACLE_DATAPUMP 卸载/装载数据
The following steps use the sample schema, oe, to show an extended example of how you can use the ORACLE_DATAPUMP access driver to unload and load data. (The example assumes that the directory object def_dir1 already exists, and that user oe has read and write access to it.)
An external table will populate a file with data only as part of creating the external table with the
ASSELECTclause. The following example creates an external table namedinventories_xtand populates the dump file for the external table with the data from tableinventoriesin theoeschema.SQL> CREATE TABLE inventories_xt 2 ORGANIZATION EXTERNAL 3 ( 4 TYPE ORACLE_DATAPUMP 5 DEFAULT DIRECTORY def_dir1 6 LOCATION ('inv_xt.dmp') 7 ) 8 AS SELECT * FROM inventories; Table created.Describe both
inventoriesand the new external table, as follows. They should both match.SQL> DESCRIBE inventories Name Null? Type
PRODUCT_ID NOT NULL NUMBER(6) WAREHOUSE_ID NOT NULL NUMBER(3) QUANTITY_ON_HAND NOT NULL NUMBER(8) SQL> DESCRIBE inventories_xt Name Null? Type
PRODUCT_ID NOT NULL NUMBER(6) WAREHOUSE_ID NOT NULL NUMBER(3) QUANTITY_ON_HAND NOT NULL NUMBER(8)Now that the external table is created, it can be queried just like any other table. For example, select the count of records in the external table, as follows:
SQL> SELECT COUNT(*) FROM inventories_xt; COUNT(*)
1112Compare the data in the external table against the data in
inventories. There should be no differences.SQL> SELECT * FROM inventories MINUS SELECT * FROM inventories_xt; no rows selected
After an external table has been created and the dump file populated by the
CREATETABLEASSELECTstatement, no rows may be added, updated, or deleted from the external table. Any attempt to modify the data in the external table will fail with an error.The following example shows an attempt to use data manipulation language (DML) on an existing external table. This will return an error, as shown.
SQL> DELETE FROM inventories_xt WHERE warehouse_id = 5; DELETE FROM inventories_xt WHERE warehouse_id = 5 * ERROR at line 1: ORA-30657: operation not supported on external organized tableThe dump file created for the external table can now be moved and used as the dump file for another external table in the same database or different database. Note that when you create an external table that uses an existing file, there is no
ASSELECTclause for theCREATETABLEstatement.SQL> CREATE TABLE inventories_xt2 2 ( 3 product_id NUMBER(6), 4 warehouse_id NUMBER(3), 5 quantity_on_hand NUMBER(8) 6 ) 7 ORGANIZATION EXTERNAL 8 ( 9 TYPE ORACLE_DATAPUMP 10 DEFAULT DIRECTORY def_dir1 11 LOCATION ('inv_xt.dmp') 12 ); Table created.Compare the data for the new external table against the data in the
inventoriestable. Theproduct_idfield will be converted to a compatible datatype before the comparison is done. There should be no differences.SQL> SELECT * FROM inventories MINUS SELECT * FROM inventories_xt2; no rows selected
Create an external table with three dump files and with a degree of parallelism of three.
SQL> CREATE TABLE inventories_xt3 2 ORGANIZATION EXTERNAL 3 ( 4 TYPE ORACLE_DATAPUMP 5 DEFAULT DIRECTORY def_dir1 6 LOCATION ('inv_xt1.dmp', 'inv_xt2.dmp', 'inv_xt3.dmp') 7 ) 8 PARALLEL 3 9 AS SELECT * FROM inventories; Table created.Compare the data unload against
inventories. There should be no differences.SQL> SELECT * FROM inventories MINUS SELECT * FROM inventories_xt3; no rows selected
Create an external table containing some rows from table
inventories.SQL> CREATE TABLE inv_part_xt 2 ORGANIZATION EXTERNAL 3 ( 4 TYPE ORACLE_DATAPUMP 5 DEFAULT DIRECTORY def_dir1 6 LOCATION ('inv_p1_xt.dmp') 7 ) 8 AS SELECT * FROM inventories WHERE warehouse_id < 5; Table created.Create another external table containing the rest of the rows from
inventories.SQL> drop table inv_part_xt; Table dropped. SQL> SQL> CREATE TABLE inv_part_xt 2 ORGANIZATION EXTERNAL 3 ( 4 TYPE ORACLE_DATAPUMP 5 DEFAULT DIRECTORY def_dir1 6 LOCATION ('inv_p2_xt.dmp') 7 ) 8 AS SELECT * FROM inventories WHERE warehouse_id >= 5; Table created.Create an external table that uses the two dump files created in Steps 10 and 11.
SQL> CREATE TABLE inv_part_all_xt 2 ( 3 product_id NUMBER(6), 4 warehouse_id NUMBER(3), 5 quantity_on_hand NUMBER(8) 6 ) 7 ORGANIZATION EXTERNAL 8 ( 9 TYPE ORACLE_DATAPUMP 10 DEFAULT DIRECTORY def_dir1 11 LOCATION ('inv_p1_xt.dmp','inv_p2_xt.dmp') 12 ); Table created.Compare the new external table to the
inventoriestable. There should be no differences. This is because the two dump files used to create the external table have the same metadata (for example, the same table nameinv_part_xtand the same column information).SQL> SELECT * FROM inventories MINUS SELECT * FROM inv_part_all_xt; no rows selected

浙公网安备 33010602011771号