Anda di halaman 1dari 6

Overview

Creating the Control File


Creating the Data File
Loading Your Data
Loading Without a Separate Data File
Loading DATE Data
Loading Long Strings
Entering NULL Values
Overview
To use the Orale !ul" loader# $ou need a control file# whih spei%ies how data should !e
loaded into the data!ase& and a data file# whih spei%ies what data should !e loaded'
You will learn how to reate these %iles in turn'
Creating the Control File
A si(ple ontrol %ile has the %ollowing %or()
LOAD DATA
INFILE <dataFile>
APPEND INTO TABLE <tableName>
FIELDS TERMINATED BY '<separator>'
<list o! all attrib"te #ames to load>$
<dataFile> is the na(e o% the data %ile' *% $ou did not give a %ile na(e e+tension
%or <dataFile># Orale will assu(e the de%ault e+tension ,%dat,' There%ore# it is a
good idea to na(e ever$ data %ile with an e+tension# and spei%$ the o(plete %ile
na(e with the e+tension'
<tableName> is the na(e o% the ta!le to whih data will !e loaded' O% ourse# it
should have !een reated alread$ !e%ore the !ul" load operation'
The optional "e$word APPEND sa$s that data will !e appended to <tableName>'
*% APPEND is o(itted# the ta!le (ust !e e(pt$ !e%ore the !ul" load operation or
else an error will our'
<separator> spei%ies the %ield separator %or $our data %ile' This an !e an$
string' *t is a good idea to use a string that $ou "now will never appear in the data#
so the separator will not !e on%used with data %ields'
Finall$# list the na(es o% attri!utes o% <tableName> that are set !$ $our data %ile#
separated !$ o((as and enlosed in parentheses' This list need not !e the
o(plete list o% attri!utes in the atual she(a o% the ta!le# nor (ust it !e
arranged in the sa(e order as the attri!utes when the ta!le was reated -- s&lldr
will (ath attri!utes to !$ their na(es in the ta!le she(a' An$ attri!utes
unspei%ied in the list o% attri!utes will !e set to N'LL'
As a onrete e+a(ple# here are the ontents o% a ontrol %ile test%(tl)
LOAD DATA
INFILE test%dat
INTO TABLE test
FIELDS TERMINATED BY ')'
i* s$
Creating the Data File
Eah line in the data %ile spei%ies one tuple to !e loaded into <tableName>' *t lists# in
order# values %or the attri!utes in the list spei%ied in the ontrol %ile# separated !$
<separator>' As a onrete e+a(ple# test%dat (ight loo" li"e)
+)!oo
,)bar
-) ba.
.eall that the attri!ute list o% test spei%ied in test%(tl is i* s$# where i has the t$pe i#t#
and s has the t$pe (/ar+0$' As the result o% loading test%dat# the %ollowing tuples are
inserted into test)
+* '!oo'$
,* 'bar'$
-* ' ba.'$
Some Notes of Warning
Note that the third line o% test%dat has a !lan" a%ter ,),' This !lan" is not ignored
!$ the loader' The value to !e loaded %or attri!ute s is ' ba.'# a %our-harater
string with a leading !lan"' *t is a o((on (ista"e to assu(e that 'ba.'# a three-
harater string with no leading !lan"# will !e loaded instead' This an lead to
so(e ver$ %rustrating pro!le(s that $ou will not notie until $ou tr$ to /uer$ $our
loaded data# !eause ' ba.' and 'ba.' are di%%erent strings'
Orale literall$ onsiders ever$ single line to !e one tuple# even an e(pt$ line0
When it tries to load data %ro( an e(pt$ line# however# an error would our and
the tuple will !e re1eted' So(e te+t editors love to add (ultiple newlines to the
end o% a %ile& i% $ou see an$ strange errors in $our %lo1 %ile a!out tuples with all
N'LL olu(ns# this (a$ !e the ause' *t shouldn2t a%%et other tuples loaded'
*% $ou are using a 3iroso%t te+t editor# suh as 3SWord# $ou will %ind that 4ill
5ates !elieves in ending lines with the se/uene 6C.7 8arriage return9 6LF7
8line %eed9' The UN*: world uses onl$ 6LF7# so eah 6C.7 !eo(es ;3# the last
harater o% strings in $our load %ile' That (a"es it i(possi!le %or $ou ever to
(ath a stored string in an S<L /uer$' =ere2s how $ou re(ove ;3 s$(!ols %ro(
$our %ile' Let2s sa$ the %ile with ;3 s$(!ols is !ad>($.el'dat' Then the
%ollowing o((and will reate ($.el'dat without ;3 s$(!ols)
(at bad2m3Rel%dat ) tr 4d '50+6' > m3Rel%dat
I! 3o"'re a# ema(s !a#* t3pe i# t/e !ollo7i#1 se&"e#(e to modi!3
3o"r ("rre#t b"8er9
ES:4; repla(e4stri#1 :TRL4& :TRL4m ENTER ENTER
Loading Your Data
The Orale !ul" loader is alled s&lldr' *t is a UN*:-level o((and# i.e.# it should
!e issued diretl$ %ro( $our UN*: shell# rather than within s&lpl"s' A !ul" load
o((and has the %ollowing %or()
s&lldr <3o"rName> (o#trol<<(tlFile> lo1<<lo1File> bad<<badFile>
Ever$thing !ut s&lldr is optional -- $ou will !e pro(pted %or $our userna(e#
password# and ontrol %ile' <(tlFile> is the na(e o% the ontrol %ile' *% no %ile
na(e e+tension is provided# s&lldr will assu(e the de%ault e+tension ,%(tl,' The
na(e o% the data %ile is not needed on the o((and line !eause it is spei%ied
within the ontrol %ile' You (a$ designate <lo1File> as the log %ile' *% no %ile
na(e e+tension is provided# ,%lo1, will !e assu(ed' s&lldr will %ill the log %ile
with relevant in%or(ation a!out the !ul" load operation# suh as the nu(!er o%
tuples loaded# and a desription o% errors that (a$ have ourred' Finall$# $ou
(a$ designate <badFile> as the %ile where !ad tuples 8an$ tuples %or whih an
error ours on an atte(pt to load the(9 are reorded 8i% the$ our9' Again# i% no
%ile e+tension is spei%ied# Orale uses ,%bad,' *% no log %ile or !ad %ile are
spei%ied# s&lldr will use the na(e o% the ontrol %ile with the %lo1 and %bad
e+tensions# respetivel$'
As a (o#(rete e;ample* i! sall3 7is/es to r"# t/e (o#trol =le test%(tl
a#d /a>e t/e lo1 o"tp"t stored i# test%lo1* t/e# s/e s/o"ld t3pe
s&lldr sall3 (o#trol<test%(tl lo1<test%lo1
Reminder: 4e%ore $ou run an$ Orale o((ands suh as s&lldr and s&lpl"s#
(a"e sure $ou have alread$ set up the orret environ(ent !$ souring
?a!s?ir?(lass?(s+@6?all%e#> 8see 5etting Started With Orale9'
Loading Without a Separate Data File
*t is possi!le to use 1ust the ontrol %ile to load data# instead o% using a separate
data %ile' =ere is an e+a(ple)
LOAD DATA
INFILE A
INTO TABLE test
FIELDS TERMINATED BY ')'
i* s$
BEBINDATA
+)!oo
,)bar
-) ba.
The tri" is to spei%$ ,A, as the na(e o% the data %ile# and use BEBINDATA to start
the data setion in the ontrol %ile'
Loading DATE Data
The DATE datat$pe an have its data loaded in a %or(at $ou spei%$ with
onsidera!le %le+i!ilit$' First# suppose that $ou have reated a relation with an
attri!ute o% t$pe DATE)
:REATE TABLE !oo
i i#t*
d date
$C
*n the ontrol %ile# when $ou desri!e the attri!utes o% !oo !eing loaded# $ou
%ollow the attri!ute d !$ its t$pe DATE and a date mask' A date (as" spei%ies the
%or(at $our date data will use' *t is a quoted string with the %ollowing
onventions)
o Se/uenes o% d# m# or 3# denote %ields in $our data that will !e interpreted
as da$s# (onths# $ears# respetivel$' As with al(ost all o% S<L# apitals
are e/uall$ aepta!le# e.g.# MM is a (onth %ield'
o The lengths o% these %ields spei%$ the (a+i(u( lengths %or the
orresponding values in $our data' =owever# the data an !e shorter'
o Other haraters# suh as dash# are treated literall$# and (ust appear in
$our data i% $ou put the( in the (as"'
=ere is an e+a(ple ontrol %ile)
LOAD DATA
INFILE A
INTO TABLE !oo
FIELDS TERMINATED BY ')'
i* d DATE 'dd4mm43333'$
BEBINDATA
+)0+40+4+DD0
,)@4+4+DDE
Notie that# as illustrated !$ the seond tuple a!ove# a %ield an !e shorter than the
orresponding %ield in the date (as"' The puntuation ,4, tells the loader that the
da$ and (onth %ields o% the seond tuple ter(inate earl$'
Loading Long Strings
String %ields that (a$ !e longer than ?@@ haraters# suh as %or data t$pes
C=A.8?AAA9 or VA.C=A.8BAAA9# re/uire a speial :FAR#$ delaration in the
ontrol %ile' For e+a(ple# i% ta!le %oo was reated as
:REATE TABLE !oo ; GAR:FAR@000$$C
T/e# a sample (o#trol =le s/o"ld looH liHe9
LOAD DATA
INFILE <dataFile>
INTO TABLE !oo
FIELDS TERMINATED BY ')'
; :FAR@000$$
Note that the delaration ta"es the %or( :FAR#$ regardless o% whether the %ield
t$pe was delared as :FAR or GAR:FAR'
Entering NLL !alues
You (a$ spei%$ N'LL values si(pl$ !$ entering %ields in $our data %ile without
ontent' For e+a(ple# i% we were entering integer values into a ta!le with she(a
a* b* ($ spei%ied in the %(tl %ile# the %ollowing lines in the data %ile)
-))6
),)@
+))I
))J
would result in inserting the %ollowing tuples in the relation)
-* N'LL* 6$
N'LL* ,* @$
+* N'LL* I$
N'LL* N'LL* J$
Ceep in (ind that an$ pri(ar$ "e$s or other onstraints re/uiring that values !e
non-N'LL will re1et tuples %or whih those attri!utes are unspei%ied'
Note"I! t/e =#al =eld i# a 1i>e# ro7 o! 3o"r data =le 7ill be
"#spe(i=ed N'LL$* 3o" /a>e to i#(l"de t/e li#e TRAILINB N'LL:OLS
a!ter t/e FIELDS TERMINATED BY li#e i# 3o"r (o#trol =le* ot/er7ise
s&lldr 7ill reKe(t t/at t"ple% s&lldr 7ill also reKe(t a t"ple 7/ose
(ol"m#s are all set to N'LL i# t/e data =le%
I! 3o" do #ot 7is/ to e#ter >al"es !or a#3 ro7 o! a 1i>e# (ol"m#*
3o" (a#* as me#tio#ed abo>e* lea>e t/at (ol"m# o"t o! t/e
attrib"te list alto1et/er%

Anda mungkin juga menyukai