[CONSTRAINT [constraint_name]{unique_constraint |Values in the constrained column cannot be repeated, except in the case of null values. For example:
ColA
1
2
NULL
4
5
NULLValues in a single column primary key column must be unique. Values across multiple constrained columns cannot be repeated, but values within a column can be repeated. Null values are not allowed. For example:
Col A Col B
2 1
3 1
4 2
5 2
6 2defines a link between related tables. See Foreign Key Clause for the syntax.A column defined as a foreign key in one table references a primary key in the related table. Only values that are valid in the primary key are valid in the foreign key. The following example is valid because the foreign key values of the dept id column in the EMP table match those of the id column in the referenced table DEPT:
(Foreign Key) The following example, however, is not valid. The value 4 in the dept id column does not match any value in the referenced id column of the DEPT table.
(Foreign Key) specifies the column to which the constraint is applied. Multiple columns names must be separated by commas.specifies the primary key column to which the constraint is applied. Multiple column names must be separated by commas.specifies the foreign key column to which the constraint is applied. Multiple column names must be separated by commas.specifies the primary key column or columns referenced in the referenced table. Multiple column names must be separated by commas.defines the operation performed when a row in the table referenced by a foreign key constraint is deleted. One of the following operators must be specified in the On Delete clause:
![]()
CASCADE specifies that all rows in the foreign key table that reference the deleted row in the primary key table are also deleted.
![]()
SET DEFAULT specifies that the value of the foreign key column is set to the column default value for all rows in the foreign key table that reference the deleted row in the primary key table.
![]()
SET NULL specifies that the value of the foreign key column is set to NULL for all rows in the foreign key table that reference the deleted row in the primary key table.defines the operation performed when the primary key of a row in the table referenced by a foreign key constraint is updated. One of the following operators must be specified in the On Update clause:
![]()
CASCADE specifies that the value of the foreign key column for all rows in the foreign key table that reference the row in the primary key table that had the primary key updated are updated with the new primary key value.
![]()
SET DEFAULT specifies that the value of the foreign key column is set to the column default value for all rows in the foreign key table that reference the row that had the primary key updated in the primary key table.
![]()
SET NULL specifies that the value of the foreign key column is set to NULL for all rows in the foreign key table that reference the row that had the primary key updated in the primary key table.
![]()
Assuming the current schema is PUBLIC, the emp table is created with the name, empId, and deptId columns. The table contains a foreign key constraint on the deptId column that references the id column in the dept table. In addition, it sets the value of any rows in the deptId column to NULL that point to a deleted row in the referenced dept table.CREATE TABLE emp (name VARCHAR(30), empId INTEGER, deptId INTEGER, FOREIGN KEY(deptId) REFERENCES dept(id) ON DELETE SET NULL)
| © 2012 Progress Software Corporation and/or its subsidiaries or affiliates. |