0% found this document useful (0 votes)
41 views

FOREIGN KEY Table Constraint: Chapter 1: Creating

1. Always specify a primary key to keep transaction logs small by uniquely identifying rows. Without a primary key, the entire row must be written to the log for every change, inflating the log size. 2. Foreign key constraints have more options than column constraints but require specifying the list of column names in the key. An example shows a three-level hierarchy with primary and foreign keys that increase in size at each level. 3. Constraint names can be specified in two places for foreign keys - as a leading or role name. This makes error messages more meaningful. The foreign key columns must be NOT NULL if specified, regardless of individual column definitions. References must specify the parent table and can optionally list parent key

Uploaded by

Nishki Gejmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

FOREIGN KEY Table Constraint: Chapter 1: Creating

1. Always specify a primary key to keep transaction logs small by uniquely identifying rows. Without a primary key, the entire row must be written to the log for every change, inflating the log size. 2. Foreign key constraints have more options than column constraints but require specifying the list of column names in the key. An example shows a three-level hierarchy with primary and foreign keys that increase in size at each level. 3. Constraint names can be specified in two places for foreign keys - as a leading or role name. This makes error messages more meaningful. The foreign key columns must be NOT NULL if specified, regardless of individual column definitions. References must specify the parent table and can optionally list parent key

Uploaded by

Nishki Gejmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Chapter 1: Creating 29

Tip: Always specify a PRIMARY KEY, or at least a UNIQUE constraint or


non-NULL unique index. This keeps the transaction log small because the row
can be uniquely identified by the key or index entry. Without this identification the
entire row must be written to the transaction log for every change. This inflates
the log file and slows down the server.

1.13.3 FOREIGN KEY Table Constraint


FOREIGN KEY table constraints have more options than the corresponding
column constraint, but they also require more work because you must specify
the list of column names that comprise the foreign key.
<foreign_key_table_constraint> ::= [ <constraint_or_prefix> ]
[ NOT NULL ]
FOREIGN KEY [ <role_name> ]
"(" <column_name_list> ")"
REFERENCES [ <owner_name> "." ] <table_name>
[ "(" <column_name_list> ")" ]
[ <on_action> ]
[ CHECK ON COMMIT ]
[ <clustering> ]
<constraint_or_prefix> ::= CONSTRAINT
| CONSTRAINT <constraint_name>
| <constraint_name>
<role_name> ::= <identifier>
Here is an example of a three-level hierarchy where the primary and foreign
keys grow in size with each level:
CREATE TABLE country (
country_code VARCHAR ( 2 ),
name VARCHAR ( 100 ),
PRIMARY KEY ( country_code ) );

CREATE TABLE office (


country_code VARCHAR ( 2 ),
office_code VARCHAR ( 10 ),
address VARCHAR ( 1000 ),
PRIMARY KEY ( country_code, office_code ),
FOREIGN KEY ( country_code ) REFERENCES country );

CREATE TABLE representative (


country_code VARCHAR ( 2 ),
office_code VARCHAR ( 10 ),
representative_id INTEGER,
name VARCHAR ( 100 ),
PRIMARY KEY ( country_code, office_code, representative_id ),
FOREIGN KEY ( country_code, office_code ) REFERENCES office );
You can specify a constraint name in one of two places: as a leading name (with
or without the CONSTRAINT keyword), or as a role name following the
FOREIGN KEY keywords. Either way, this name will appear in any error mes-
sages so its an opportunity for you to make the messages more meaningful.
The NOT NULL clause is a way of specifying that all the foreign key col-
umns must be NOT NULL regardless of whether the individual columns are
defined as NULL or NOT NULL.
The REFERENCES clause must specify the parent table name. The list of
column names is optional; the default is the parent table primary key columns.
If you want to reference a parent table UNIQUE constraint instead of the

You might also like