Java Reference
In-Depth Information
Chapter 6: Inserting, Updating,and Deleting Data
In This Chapter
The preceding chapter explains how to create a database and how to add, delete, and modify
database tables. This chapter explains how to insert data into a table and, when necessary, modify or
delete data.
Related topics covered in this chapter include a review of transaction control, which is important to
understand when you are inserting and deleting interdependent data items. Transaction control is also
covered at a more theoretical level in Chapter 1 .
The use of the SQL commands is illustrated in the context of a series of Java examples, including a
discussion of the use of the JDBC DatabaseMetaData object to obtain information about a database.
Inserting Data Using SQL INSERT
Once you have created a database and its constituent tables, it is important to know how to add, delete
and modify its contents. SQL provides the three following statements you can use to manipulate data
within a database:
 
INSERT
 
UPDATE
 
DELETE
INSERT
The INSERT statement, in its simplest form, is used to insert data into a table, one row or record at a
time. It can also be used in combination with a SELECT statement to perform bulk inserts of multiple
selected rows from another table or tables. INSERT can only be used to insert entire rows into a table,
not to insert individual fields directly into a row.
UPDATE
The UPDATE command is used to modify the contents of individual columns within a set of rows. The
UPDATE command is normally used with a WHERE clause. As this chapter explains, the WHERE clause
is used to select the rows to be updated. Clearly, it is important to choose the rows you are updating
correctly; otherwise, you may find yourself updating records you have not planned on changing.
DELETE
DELETE is used to delete selected rows from a table. As in the case with the UPDATE command, row
selection is based on the result of an optional WHERE clause. Again, you need to be careful when you
make the selection, or you may delete records you mean to leave intact.
The INSERT Statement
The basic form of the INSERT statement looks like this:
Search WWH ::




Custom Search