Java Reference
In-Depth Information
given names. Such names typically use an identifier, such as PK for primary key
and FK for foreign key, along with the table name and one or more possible field
names. Table 11-6 lists some examples of constraints and their respective SQL
keywords.
Table 11-6
SQL Constraints
TYPE
LEVEL
EXAMPLE SQL KEYWORDS
Integrity
Table
PRIMARY KEY
Integrity
Column
FOREIGN KEY
Value
Table
UNIQUE
Value
Column
NOT NULL
Lines 68 through 104, as shown in Figure 11-17 on page 710, create the
Stocks and Users tables. Individual field names, types, sizes, and named con-
straints are listed within field declarations, which are separated by commas. The
tables are created by calling the executeUpdate() method, each within a separate
try block, allowing execution to continue even if any one table creation throws an
exception. Such an exception would occur if a table already exists. The following
steps enter code to create the Stocks and Users tables and primary key indexes.
To Create Tables with Primary Keys
1. Enter lines 68 through 86 as shown in Figure 11-17.
The SQL statement, CREATE TABLE, is used to create the Stocks table
(Figure 11-24). Following the table name is a comma-delimited list of fields,
types, sizes, and constraints within parentheses. The field, symbol, of type
TEXT with a maximum length of 8, has a value constraint of NOT NULL,
meaning it cannot have a null, or empty, value. It also is used as the pri-
mary key. The field, name, is of type TEXT with a maximum length of 50.
SQL statement
CREATE TABLE
table
name
fields,
types, sizes,
constraints
comma separates
field declarations
FIGURE 11-24
 
Search WWH ::




Custom Search