InterBase Quick Start: Part II - Primary Keys and Foreign Keys
Go Up to InterBase Quick Start: Part II - Data Definition
The Job table definition includes a primary key and a foreign key.
- A primary key is a column or group of columns that uniquely identify a row. Every table should have a primary key. And a table cannot have more than one primary key. The
PRIMARY KEYcharacteristic can be specified as part of a column definition (as in the first column of theCountrytable), or it can be specified as a separate clause of theCREATE TABLEstatement (as in the statement that creates theJobtable). The primary key in theJobtable is made up of three columns:job_code,job_grade, andjob_country. While a value can appear more than once in any of these columns taken individually, the fact that they are collectively a primary key means that the three values taken together cannot occur in more than one row.
- A foreign key is a column or set of columns in one table whose values must have matching values in the primary key of another (or the same) table. A foreign key is said to reference its primary key. Foreign keys are a mechanism for maintaining data integrity. For example, any country listed in the
job_countrycolumn of theJobtable must also exist in theCountrytable.
- You can declare a constraint such as
UNIQUE,FOREIGN KEY, orPRIMARY KEYeither as part of a column definition, or as a table constraint following column definitions. The syntax varies slightly depending on which approach you choose. See the Language Reference Guide for more details.
- You declare the primary key constraint as part of a column definition in the
CountryandDepartmenttables. For theJobtable, you declare the primary key, foreign key, and check constraints at the table level. Both approaches have the same result.
Contents
Cascading Referential Integrity Constraints
When you create a foreign key, you are saying that the value in the foreign key must also exist in the primary key that it references. What happens if later the value in the referenced primary key changes or is deleted? The cascading referential integrity constraints let you specify what should happen. Your choices are to take no action, to propagate (cascade) the change to the foreign key column, to set the foreign key to its default, or to set it to NULL.
If you are specifying the foreign key as part of the column definition, the syntax is this:
CREATE TABLE table_name
(
column_name data_type FOREIGN KEY
REFERENCES other_table(columns)
[ON UPDATE {NO ACTION | CASCADE | SET DEFAULT | SET NULL}]
[ON DELETE {NO ACTION | CASCADE | SET DEFAULT | SET NULL}],
[, column_definition]
)
If you specify the foreign key as a table-level constraint, the syntax is similar. In this case you have to specify the column for which the constraint is defined:
CREATE TABLE table_name
(
column_definitions,
FOREIGN KEY (column_name) REFERENCES other_table(columns)
[ON UPDATE {NO ACTION | CASCADE | SET DEFAULT | SET NULL}]
[ON DELETE {NO ACTION | CASCADE | SET DEFAULT | SET NULL}],
[, table_constraints]
)
Later on in the tutorial you use ALTER TABLE to add columns and constrains to the Department table, including some cascading referential integrity constraints.
Naming Constraints
When you declare a constraint you may name the constraint using the optional CONSTRAINT keyword, followed by the constraint name. This allows you to use the ALTER TABLE statement to drop that constraint. In the Job table definition, two of the constraints have names (pkjob and fkjob), and the CHECK constraint does not have a name.
Computed Columns
When you create a table, you can define columns whose value is based on the values of one or more other columns in the table. The computation can include any arithmetic operation that is suitable to the data types of the columns.
- To see an example, open the script
TABLES.SQLand take a look at the following column definition for theEmployeetable:
full_name COMPUTED BY (last_name || ', ' || first_name)
The value of the full_name column consists of the value of the last_name column plus a comma plus the value of the first_name column.