Java Reference
In-Depth Information
FirstName
LastName
ISBN
FirstName
LastName
ISBN
Harvey
Deitel
0133807800
Paul
Deitel
0133406954
Harvey
Deitel
0132151006
Paul
Deitel
0132990601
Paul
Deitel
0132121360
Paul
Deitel
013299044X
Paul
Deitel
0133570924
Paul
Deitel
0132575655
Paul
Deitel
0133764036
Paul
Deitel
0133807800
Paul
Deitel
0133378713
Paul
Deitel
0132151006
Paul
Deitel
0136151574
Michael
Morgano
0132121360
Paul
Deitel
0133379337
Dan
Quirk
0136151574
Fig. 24.19
|
Sampling of authors and ISBNs for the topics they have written in ascending
order by
LastName
and
FirstName
. (Part 2 of 2.)
The
INSERT
statement inserts a row into a table. The basic form of this statement is
INSERT INTO
tableName
(
columnName1
,
columnName2
,
…
,
columnNameN
)
VALUES
(
value1
,
value2
,
…
,
valueN
)
where
tableName
is the table in which to insert the row. The
tableName
is followed by a
comma-separated list of column names in parentheses (this list is not required if the
IN-
SERT
operation specifies a value for every column of the table in the correct order). The list
of column names is followed by the SQL keyword
VALUES
and a comma-separated list of
values in parentheses. The values specified here must match the columns specified after the
table name in both order and type (e.g., if
columnName1
is supposed to be the
FirstName
column, then
value1
should be a string in single quotes representing the first name). Al-
ways explicitly list the columns when inserting rows. If the table's column order changes
or a new column is added, using only
VALUES
may cause an error. The
INSERT
statement
INSERT INTO
Authors (FirstName, LastName)
VALUES
(
'Sue'
,
'Red'
)
inserts a row into the
Authors
table. The statement indicates that values are provided for
the
FirstName
and
LastName
columns. The corresponding values are
'Sue'
and
'Smith'
.
We do not specify an
AuthorID
in this example because
AuthorID
is an autoincremented
column in the
Authors
table. For every row added to this table, the DBMS assigns a
unique
AuthorID
value that is the next value in the autoincremented sequence (i.e., 1, 2,
3 and so on). In this case, Sue Red would be assigned
AuthorID
number 6. Figure 24.20
shows the
Authors
table after the
INSERT
operation. [
Note:
Not every database manage-
ment system supports autoincremented columns. Check the documentation for your
DBMS for alternatives to autoincremented columns.]
Common Programming Error 24.3
SQL delimits strings with single quotes (
'
). A string containing a single quote (e.g.,
O'Malley) must have two single quotes in the position where the single quote appears (e.g.,
'O''Malley'
). The first acts as an escape character for the second. Not escaping single-
quote characters in a string that's part of a SQL statement is a SQL syntax error.