Java Reference
In-Depth Information
We now discuss SQL in the context of our
books
database. You'll be able to use the SQL
discussed here in the examples later in the chapter. The next several subsections demon-
strate SQL queries and statements using the SQL keywords in Fig. 24.10. Other SQL key-
words are beyond this text's scope.
SQL keyword
Description
Retrieves data from one or more tables.
SELECT
Tables involved in the query. Required in every
SELECT
.
FROM
Criteria for selection that determine the rows to be retrieved, deleted or
updated. Optional in a SQL query or a SQL statement.
WHERE
Criteria for grouping rows. Optional in a
SELECT
query.
GROUP BY
Criteria for ordering rows. Optional in a
SELECT
query.
ORDER BY
Merge rows from multiple tables.
INNER JOIN
Insert rows into a specified table.
INSERT
Update rows in a specified table.
UPDATE
Delete rows from a specified table.
DELETE
Fig. 24.10
|
SQL query keywords.
Let's consider several SQL queries that extract information from database
books
. A SQL
query “selects” rows and columns from one or more tables in a database. Such selections
are performed by queries with the
SELECT
keyword. The basic form of a
SELECT
query is
SELECT
*
FROM
tableName
in which the
asterisk (
*
)
wildcard character
indicates that all columns from the
tableName
table should be retrieved. For example, to retrieve all the data in the
Authors
table, use
SELECT
*
FROM
Authors
Most programs do not require all the data in a table. To retrieve only specific columns,
replace the
*
with a comma-separated list of column names. For example, to retrieve only
the columns
AuthorID
and
LastName
for all rows in the
Authors
table, use the query
SELECT
AuthorID, LastName
FROM
Authors
This query returns the data listed in Fig. 24.11.
AuthorID
LastName
AuthorID
LastName
1
Deitel
4
Quirk
2
Deitel
5
Morgano
3
Deitel
Fig. 24.11
|
Sample
AuthorID
and
LastName
data from the
Authors
table.