DB2: IMPORTing and EXPORTing |
Here are some quick
and dirty instructions on how to import data that you have in a
file into a DB2 table. Likewise, you can export the data from a
DB2 table into a file. The DB2 commands, surprise, are named import
and export, respectively.
Yes, Virginia, there is a DB2 load command. It offers a way to bulk-load schemas and their data into DB2. However, load requires DBA privileges and so is not for us. The import command is available to all, and is exactly what we want for purposes of the project. |
First, the table
to which you want to add data should exist. So create it first, if you
do not have it already. Prepare a file with the "tuples" that you want
to import. For a file in "DEL" format, each line in the file is a tuple,
and the attribute values are separated (delimited) by commas (","'s).
The file is just an ASCII file.
Of course, some of the inserts may succeed, others may fail. (Why?) Whatever the import utility accomplishes is recorded in the message file import.msg for you perusal. You can replace "insert" in the command above with insert_update. Then any new tuple that clashes with a tuple already in the table will replace ("update") the older tuple. (How can two tuples clash? They have the same primary key values.) To use this option, your table must exist and have a primary key declared. And, of course, you must have update privilege on the table. Likewise, you can replace "insert" with replace. This first wipes all tuples from the table, and then adds the "tuples" from the file. |
Need to know what
the format of you data must be in within file.del? A couple
of pointers:
|
An alternate import
method is as follows. It uses an "ASC" file instead of a "DEL"
file like above. Again, the file is an ASCII file. But instead of the attribute
values being separated by a delimiter character (the comma, in this case),
each attribute entry begins at a specified character column in the file.
|
Conversely, you can
export tuples from a table into a file.
|
Probably you will
not need this, but just to be informative, there is another file format
supported (besides "DEL" and "ASC") called IXF.
It is special in that it also encodes the table's schema into the
file. So it is a useful utility for moving tables from one database to
another or from one system to another.
But is can be useful sometimes for database gurus too, for other purposes. Database folks, like professional programmers, have quite a bag of tricks. Here is one that Don Chamberlin suggests.
|