♦
PRIMARY KEY
This is the same as a unique constraint, except that a
table can have only one primary key constraint. The primary key usually
identifies the best identifier for a row. For example, the customer
number might be the primary key for the customer table.
Columns included in primary keys cannot allow NULL. Each row in the
table has a unique primary key value. A table can have only one
PRIMARY KEY.
The order of the columns in a primary key is the order in which the
columns were created in the table, not the order in which they are listed
when the primary key is created.
$
For information on the WITH HASH clause that can be used with
this constraint, see "CREATE INDEX statement" on page 448.
♦
A foreign key constraint can be implemented using a
Foreign key
REFERENCES column constraint (single column only) or a FOREIGN
KEY table constraint. It restricts the values for a set of columns to match
the values in a primary key or, less commonly, a unique constraint of
another table (the primary table). For example, a foreign key constraint
could be used to ensure that a customer number in an invoice table
corresponds to a customer number in the customer table.
If you specify column name in a REFERENCES column constraint, it
must be a column in the primary table, must be subject to a unique
constraint or primary key constraint, and that constraint must consist of
only that one column. If you do not specify column-name, the foreign
key references the primary key of the primary table.
If you do not explicitly define a foreign key column, it is created with
the same data type as the corresponding column in the primary table.
These automatically-created columns cannot be part of the primary key
of the foreign table. Thus, a column used in both a primary key and
foreign key of the same table must be explicitly created.
If foreign key column names are specified, then primary key column
names must also be specified, and the column names are paired
according to position in the lists. If the primary table column names are
not specified in a FOREIGN KEY table constraint, then the primary key
columns are used. If foreign key column names are not specified then
the foreign key columns are give the same names as the columns in the
primary table.
If at least one value in a multi-column foreign key is NULL, there is no
restriction on the values that can be held in other columns of the key.
A temporary table cannot have a foreign key that references a base table
and a base table cannot have a foreign key that references a temporary
table.
Chapter 9 SQL Statements
473
Need help?
Do you have a question about the Adaptive Server Anywhere and is the answer not in the manual?
Questions and answers