7. Oracle数据加载和卸载
程序员文章站
2023-02-18 11:05:23
在日常工作中;经常会遇到这样的需求: Oracle 数据表跟文本或者文件格式进行交互;即将指定文件内容导入对应的 Oracle 数据表中;或者从 Oracle 数据表导出。 其他数据库中的表跟Oracle数据库进行交互。 Oracle 数据表跟文本或者文件格式进行交互;即将指定文件内容导入对应的 O ......
create [public] database link link connect to username identified by password using 'connectstring'
1.2 oracle to mysql
# 创建外部表需要的目录 sql> create or replace directory dump_dir as '/data/ora_ext_lottu'; directory created. # 给用户授予指定目录的操作权限 sql> grant read,write on directory dump_dir to lottu; grant succeeded.
2.2 外部表源文件lottu.txt
10,accounting,new york 20,research,dallas 30,sales,chicago 40,operations,boston
2.3 创建外部表
drop table dept_external purge; create table dept_external ( deptno number(6), dname varchar2(20), loc varchar2(25) ) organization external (type oracle_loader default directory dump_dir access parameters ( records delimited by newline badfile 'lottu.bad' logfile 'lottu.log' fields terminated by "," optionally enclosed by '"' ( deptno integer external(6), dname char(20), loc char(25) ) ) location ('lottu.txt') ) reject limit unlimited;
查看数据
sql> select * from dept_external; deptno dname loc ---------- -------------------- ------------------------- 10 accounting new york 20 research dallas 30 sales chicago 40 operations boston
[oracle@oracle235 ~]$ seq 1000|awk -vofs="," '{print $1,"lottu",systime()-$1}' > lottu.txt [oracle@oracle235 ~]$ sqlplus lottu/li0924 sql*plus: release 11.2.0.4.0 production on mon aug 13 22:58:34 2018 copyright (c) 1982, 2013, oracle. all rights reserved. connected to: oracle database 11g enterprise edition release 11.2.0.4.0 - 64bit production with the partitioning, olap, data mining and real application testing options sql> create table tbl_load_01 (id number,name varchar2(10),accountid number); table created.
3.2 创建控制文件lottu.ctl
load data characterset utf8 infile '/home/oracle/lottu.txt' truncate into table tbl_load_01 fields terminated by ',' trailing nullcols optionally enclosed by ' ' trailing nullcols ( id , name, accountid )
3.3 执行sqlldr
[oracle@oracle235 ~]$ sqlldr 'lottu/"li0924"' control=/home/oracle/lottu.ctl log=/home/oracle/lottu.log bad=/home/oracle/lottu.bad sql*loader: release 11.2.0.4.0 - production on mon aug 13 23:10:12 2018 copyright (c) 1982, 2011, oracle and/or its affiliates. all rights reserved. commit point reached - logical record count 64 commit point reached - logical record count 128 commit point reached - logical record count 192 commit point reached - logical record count 256 commit point reached - logical record count 320 commit point reached - logical record count 384 commit point reached - logical record count 448 commit point reached - logical record count 512 commit point reached - logical record count 576 commit point reached - logical record count 640 commit point reached - logical record count 704 commit point reached - logical record count 768 commit point reached - logical record count 832 commit point reached - logical record count 896 commit point reached - logical record count 960 commit point reached - logical record count 1000
[oracle@oracle235 ~]$ unzip sqluldr2linux64.zip archive: sqluldr2linux64.zip inflating: sqluldr2linux64.bin [oracle@oracle235 ~]$ mv sqluldr2linux64.bin $oracle_home/bin/sqludr
4.2 查看sqludr帮助
[oracle@oracle235 ~]$ sqludr -? sql*unloader: fast oracle text unloader (gzip, parallel), release 4.0.1 (@) copyright lou fangxin (anysql.net) 2004 - 2010, all rights reserved. license: free for non-commercial useage, else 100 usd per server. usage: sqluldr2 keyword=value [,keyword=value,...] valid keywords: user = username/password@tnsname sql = sql file name query = select statement field = separator string between fields record = separator string between records rows = print progress for every given rows (default, 1000000) file = output file name(default: uldrdata.txt) log = log file name, prefix with + to append mode fast = auto tuning the session level parameters(yes) text = output type (mysql, csv, mysqlins, oracleins, form, search). charset = character set name of the target database. ncharset= national character set name of the target database. parfile = read command option from parameter file for field and record, you can use '0x' to specify hex character code, \r=0x0d \n=0x0a |=0x7c ,=0x2c, \t=0x09, :=0x3a, #=0x23, "=0x22 '=0x27
4.3 执行sqludr
[oracle@oracle235 ~]$ sqludr lottu/li0924 query="tbl_load_01" file=lottu01.txt field="," 0 rows exported at 2018-08-13 23:47:55, size 0 mb. 1000 rows exported at 2018-08-13 23:47:55, size 0 mb. output file lottu01.txt closed at 1000 rows, size 0 mb.
推荐阅读
-
浅析Oracle中sys、system和Scott用户下的数据库连接问题
-
在oracle 数据库中查看一个sql语句的执行时间和SP2-0027错误
-
oracle 存储过程和触发器复制数据
-
Oracle数据库 数据完整性和DML语句
-
oracle索引、视图和闪回数据归档讲解
-
关于Oracle中sys、system和Scott用户下的数据库连接问题
-
DBA_Oracle Startup / Shutdown启动和关闭过程详解(概念)(对数据库进行各种维护操作)
-
Python操作Oracle数据库的简单方法和封装类实例
-
编程开发之--Oracle数据库--存储过程和存储函数(2)
-
华麒通信与Oracle合作区块链和大数据应用创新数字化平台