Constraint Definition for Local Tables
- Last Updated: May 15, 2020
- 3 minute read
- DataDirect Connectors
- JDBC
- IBM Db2 5.1
- MySQL 5.1
- Progress OpenEdge 5.1
- SAP Sybase 5.1
- Documentation
Purpose
Defines a constraint for a local table.
Syntax
[CONSTRAINT [constraint_name]
{unique_constraint |
primary_key_constraint |
foreign_key_constraint}]
where:
- constraint_name
- specifies a name for the constraint.
- unique_constraint
- specifies a constraint on a single column in the table. See Unique Clause for the syntax.
- primary_key_constraint
- specifies a constraint on one or more columns in the table. See Primary Key Clause for the syntax.
- foreign_key_constraint
- defines a link between related tables. See Foreign Key Clause for the syntax.
Unique Clause
UNIQUE
(column_name [,column_name...]
where:
- column_name
- specifies the column to which the constraint is applied. Multiple columns names must be separated by commas.
Primary Key Clause
PRIMARY KEY (column_name [,column_name...])
where:
- column_name
- specifies the primary key column to which the constraint is applied. Multiple column names must be separated by commas.
Foreign Key Clause
FOREIGN KEY (fcolumn_name [,fcolumn_name...])
REFERENCES ref_table (pcolumn_name [,pcolumn_name...])
[ON {DELETE | UPDATE}
{CASCADE | SET DEFAULT | SET NULL}]
where:
- fcolumn_name
- specifies the foreign key column to which the constraint is applied. Multiple column names must be separated by commas.
- ref_table
- specifies the table to which a foreign key refers.
- pcolumn_name
- specifies the primary key column or columns referenced in the referenced table. Multiple column names must be separated by commas.
- ON DELETE
- 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:
-
CASCADEspecifies that all rows in the foreign key table that reference the deleted row in the primary key table are also deleted. -
SET DEFAULTspecifies 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 NULLspecifies 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.
- ON UPDATE
- 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:
-
CASCADEspecifies 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 DEFAULTspecifies 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 NULLspecifies 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.
Notes
- You must specify at least one constraint.
- Both the
ON DELETEandON UPDATEclauses can be used in a single foreign key definition.
Example
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)