?? ch08.htm
字號:
</BLOCKQUOTE>
<H2><FONT COLOR="#000077">Importing and Exporting Data from Foreign Sources</FONT></H2>
<P>The <TT>INSERT</TT>, <TT>UPDATE</TT>, and <TT>DELETE</TT> statements are extremely
useful from within a database program. They are used with the <TT>SELECT</TT> statement
to provide the foundation for all other database operations you will perform. However,
SQL as a language does not have a way to import or export of data from foreign data
sources. For instance, your office may have been using a dBASE application for several
years now that has outgrown itself. Now your manager wants to convert this application
to a client/server application using the Oracle RDBMS. Unfortunately for you, these
dBASE files contain thousands of records that must be converted to an Oracle database.
Obviously, the <TT>INSERT</TT>, <TT>UPDATE</TT>, and <TT>DELETE</TT> commands will
help you after your Oracle database has been populated, but you would rather quit
than retype 300,000 records. Fortunately, Oracle and other manufacturers provide
tools that will assist you in this task.</P>
<P>Nearly all database systems allow you to import and export data using ASCII text
file formats. Although the SQL language does not include this feature, SQL will not
do you (or your boss) much good when you have an empty database. We will examine
the import/export tools available in the following products: Microsoft Access, Microsoft
and Sybase SQL Server, and Personal Oracle7.
<H3><FONT COLOR="#000077">Microsoft Access</FONT></H3>
<P>Microsoft Access is a PC-only database product that contains many of the features
of a relational database management system. Access also includes powerful reporting
tools, a macro language similar to Visual Basic, and the capability to import and
export data from various database and text file formats. This section examines this
last feature, particularly the capability to export to delimited text files. Delimited
means that each field is separated, or delimited, by some special character. This
character is often a comma, a quotation mark, or a space.</P>
<P>Access allows you to import and export various database formats, including dBASE,
FoxPro, and SQL Database. The SQL Database option is actually an ODBC data source
connection. (Microsoft ODBC is covered on Day 13, "Advanced SQL Topics.")
For this discussion, you want to select the Export option and then choose the Text
(Fixed Width) option.</P>
<P>After opening an Access database (with the File | Open), select Export. A Destination
dialog box (for Exporting) is displayed. Select the Text (Fixed Width) option. This
option allows you to output your Access tables to text files in which each data type
is a fixed width. For example, a character data field of length 30 will be output
to the file as a field 30 characters long. If the field's data takes up less space
than 30 characters, it will be padded with spaces. Eventually, you will be asked
to set up the export file format. Figure 8.1 shows the Import/Export Setup dialog
box.</P>
<P><A NAME="01"></A><A HREF="01-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/01-1.htm"><B>Figure 8.1.<BR>
</B></A><I>The Import/Export Setup dialog box.</I></P>
<P>Notice that in this dialog box you can select the Text Delimiter and the Field
Separator for your export file. As a final step, save the specification for use later.
This specification is stored internally within the database.
<H3><FONT COLOR="#000077">Microsoft and Sybase SQL Server</FONT></H3>
<P>Microsoft and Sybase have jointly developed a powerful database system that is
very popular in client/server application development. The name of this system is
SQL Server. Microsoft has agreed to develop versions of the RDBMS for some platforms,
and Sybase has developed its version for all the other platforms (usually the larger
ones). Although the arrangement has changed somewhat in recent years, we mention
this agreement here to help you avoid confusion when you begin examining the various
database systems available on the market today.</P>
<P>SQL Server provides file import/export capabilities with the bcp tool. bcp is
short for "bulk copy." The basic concept behind bcp is the same as that
behind Microsoft Access. Unfortunately, the bcp tool requires you to issue commands
from the operating system command prompt, instead of through dialog boxes or windows.</P>
<P>Bcp imports and exports fixed-width text files. It is possible to export a file
using the Microsoft Access method described earlier and then import that same file
directly into an SQL Server table using bcp. bcp uses format files (usually with
an <TT>.FMT</TT> extension) to store the import specification. This specification
tells bcp the column names, field widths, and field delimiters. You can run bcp from
within an SQL database build script to completely import data after the database
has been built.
<H3><FONT COLOR="#000077">Personal Oracle7</FONT></H3>
<P>Personal Oracle7 allows you to import and export data from ASCII text files containing
delimited or fixed-length records. The tool you use is SQL*Loader. This graphical
tool uses a control file (with the <TT>.CTL</TT> extension). This file is similar
to SQL Server's format (FMT) file. The information contained in this file tells SQL*Loader
what it needs to know to load the data from the file.</P>
<P>The SQL*Loader dialog box appears in Figure 8.2.</P>
<P><A NAME="02"></A><A HREF="02-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/02-1.htm"><B>Figure 8.2.<BR>
</B></A><I>The SQL*Loader dialog box.</I></P>
<H2><FONT COLOR="#000077">Summary</FONT></H2>
<P>SQL provides three statements that you can use to manipulate data within a database.</P>
<P>The <TT>INSERT</TT> statement has two variations. The <TT>INSERT...VALUES</TT>
statement inserts a set of values into one record. The <TT>INSERT...SELECT</TT> statement
is used in combination with a <TT>SELECT</TT> statement to insert multiple records
into a table based on the contents of one or more tables. The <TT>SELECT</TT> statement
can join multiple tables, and the results of this join can be added to another table.</P>
<P>The <TT>UPDATE</TT> statement changes the values of one or more columns based
on some condition. This updated value can also be the result of an expression or
calculation.</P>
<P>The <TT>DELETE</TT> statement is the simplest of the three statements. It deletes
all rows from a table based on the result of an optional <TT>WHERE</TT> clause. If
the <TT>WHERE</TT> clause is omitted, all records from the table are deleted.</P>
<P>Modern database systems supply various tools for data manipulation. Some of these
tools enable developers to import or export data from foreign sources. This feature
is particularly useful when a database is upsized or downsized to a different system.
Microsoft Access, Microsoft and Sybase SQL Server, and Personal Oracle7 include many
options that support the migration of data between systems.
<H2><FONT COLOR="#000077">Q&A</FONT></H2>
<DL>
<DD><B>Q Does SQL have a statement for file import/export operations?</B>
<P><B>A</B> No. Import and export are implementation-specific operations. In other
words, the ANSI committee allows individual manufacturers to create whatever features
or enhancements they feel are necessary.</P>
<P><B>Q Can I copy data from a table into itself using the </B>INSERT<B> command?
I would like to make duplicate copies of all the existing records and change the
value of one field.</B></P>
<P><B>A</B> No, you cannot insert data into the same table that you selected from.
However, you can select the original data into a temporary table. (True temporary
tables are discussed on Day 14.) Then modify the data in this temporary table and
select back into the original table. Make sure that you watch out for unique fields
you may have already created. A unique field means that the particular field must
contain a unique value for each row of data that exists in its table.</P>
<P><B>Q You have stressed using caution when issuing </B>INSERT<B>, </B>UPDATE<B>,
and </B>DELETE <B>commands, but simple fixes seem to be available to correct whatever
I did wrong. Is that a fair statement?</B></P>
<P><B>A </B>Yes. For example, a simple way to fix a misspelled name is to issue a
<TT>ROLLBACK</TT> command and redo the insert. Another fix would be to do an update
to fix the name. Or you could delete the row and redo the insert with the corrected
spelling of the name.</P>
<P>But suppose you inserted a million rows into a table and didn't notice that you
had misspelled a name when you issued the <TT>COMMIT</TT> command. A few weeks later,
someone notices some bad data. You have had two weeks' worth of database activity.
You would more than likely have to issue individual updates to make individual corrections,
instead of making any type of global change. In most cases you probably will not
know what to change. You may have to restore the database.
</DL>
<H2><FONT COLOR="#000077">Workshop</FONT></H2>
<P>The Workshop provides quiz questions to help solidify your understanding of the
material covered, as well as exercises to provide you with experience in using what
you have learned. Try to answer the quiz and exercise questions before checking the
answers in Appendix F, "Answers to Quizzes and Exercises."
<H3><FONT COLOR="#000077">Quiz</FONT></H3>
<DL>
<DD><B>1. </B>What is wrong with the following statement?
</DL>
<BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF">DELETE COLLECTION;</FONT></PRE>
</BLOCKQUOTE>
<DL>
<DD><B>2. </B>What is wrong with the following statement?
</DL>
<BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF">INSERT INTO COLLECTION
SELECT * FROM TABLE_2</FONT></PRE>
</BLOCKQUOTE>
<DL>
<DD><B>3. </B>What is wrong with the following statement?
</DL>
<BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF">UPDATE COLLECTION ("HONUS WAGNER CARD",
25000, "FOUND IT");</FONT></PRE>
</BLOCKQUOTE>
<DL>
<DD><B>4.</B> What would happen if you issued the following statement?
</DL>
<BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF">SQL> <B>DELETE * FROM COLLECTION;</B></FONT></PRE>
</BLOCKQUOTE>
<DL>
<P><B>5.</B> What would happen if you issued the following statement?
</DL>
<BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF">SQL> <B>DELETE FROM COLLECTION;</B></FONT></PRE>
</BLOCKQUOTE>
<DL>
<DD><B>6. </B>What would happen if you issued the following statement?
</DL>
<BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF">SQL> <B>UPDATE COLLECTION
SET WORTH = 555
SET REMARKS = 'UP FROM 525';</B></FONT></PRE>
</BLOCKQUOTE>
<DL>
<DD><B>7. </B>Will the following SQL statement work?
</DL>
<BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF">SQL> I<B>NSERT INTO COLLECTION
SET VALUES = 900
WHERE ITEM = 'STRING';</B></FONT></PRE>
</BLOCKQUOTE>
<DL>
<DD><B>8. </B>Will the following SQL statement work?
</DL>
<BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF">SQL> <B>UPDATE COLLECTION
SET VALUES = 900
WHERE ITEM = 'STRING';</B></FONT></PRE>
</BLOCKQUOTE>
<H3><FONT COLOR="#000077">Exercises</FONT></H3>
<DL>
<DD><B>1.</B> Try inserting values with incorrect data types into a table. Note the
errors and then insert values with correct data types into the same table.
<P><B>2. </B>Using your database system, try exporting a table (or an entire database)
to some other format. Then import the data back into your database. Familiarize yourself
with this capability. Also, export the tables to another database format if your
DBMS supports this feature. Then use the other system to open these files and examine
them.
</DL>
<H1></H1>
<CENTER>
<P>
<HR>
<A HREF="ch07.htm" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/ch07.htm"><IMG SRC="previous.gif" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="ch09.htm" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/ch09.htm"><IMG
SRC="next.gif" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/index-1.htm"><IMG SRC="contents.gif" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR>
<BR>
<BR>
<IMG SRC="corp.gif" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"
BORDER="0"></P>
<P>© <A HREF="copy.htm" tppabs="http://202.113.16.101/%7eeb%7e/Teach%20Yourself%20MS%20SQL%20Server%206.5%20in%2021%20Days/copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>
</BODY>
</HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -