Column constraints
- Last Updated: July 9, 2026
- 3 minute read
- OpenEdge
- Version 12.2
- Documentation
Specifies a constraint for a column that restricts
the values that the column can store. INSERT, UPDATE,
or DELETE statements that violate the constraint
fail. The database returns a constraint violation error with an
SQLCODE of -20116.
Column constraints are similar to table constraints, but their definitions are associated with a single column rather than the entire table.
Syntax
|
Parameters
- CONSTRAINT constraint_name
-
Allows you to assign a name for the column constraint. This option facilitates making changes to the column definition. If you do not specify a constraint_name
- NOT NULL
-
Restricts values in the column to values that are not null.
- NOT NULL PRIMARY KEY
-
Defines the column as the primary key for the table. There can be at most one primary key for a table. A column with the , the database assigns a name. These names can be long and unwieldy, and you must query system tables to retrieve the name.
NOT NULL PRIMARY KEYconstraint should not contain null or duplicate values.Other tables can name primary keys as foreign keys in their
REFERENCESclauses. If they do, SQL restricts operations on the table containing the primary key in the following ways:-
DROP TABLEstatements that delete the table fail -
DELETEandUPDATEstatements that modify values in the column that match a foreign key's value also fail
Note: In OpenEdge SQL, a primary key is always unique and never null. SQL automatically generates a unique index to enforce the primary key. This index is protected by both SQL and ABL; it cannot be dropped directly withDROP INDEX. To remove the index, you must drop the primary key constraint. -
- NOT NULL UNIQUE
-
Defines the column as a unique key that cannot contain null or duplicate values. Columns with
NOT NULL UNIQUEconstraints defined for them are also called candidate keys.Other tables can name unique keys in theirREFERENCESclauses. If they do, SQL restricts operations on the table containing the unique key.Note: A SQL unique key defined through aUNIQUEconstraint is not the same as a unique index created withCREATE INDEX. SQL automatically generates a protected unique index to enforce eachUNIQUEconstraint. This index cannot be dropped independently; to remove it, you need to drop theUNIQUEconstraint.A SQL primary key is a special kind of unique key. A table can have only one primary key but can have multiple unique keys.
- REFERENCES table_name[ ( column_name ) ]
-
Defines the column as a foreign key and specifies a matching primary or unique key in another table. The
REFERENCESclause names the matching primary or unique key.A foreign key and its matching primary or unique key specify a referential constraint. A value stored in the foreign key must either be null or be equal to some value in the matching unique or primary key.
You can omit the column_name argument if the table specified in the
REFERENCESclause has a primary key and you want the primary key to be the matching key for the constraint. - CHECK ( search_condition )
-
Specifies a column‑level check constraint. SQL restricts the form of the search condition. The search condition must not:
- Refer to any column other than the one with which it is defined
- Contain aggregate functions, subqueries, or parameter references
Example
Creating a primary key
The following example shows the creation of a primary key column on the supplier table:
|
Creating a constraint to define the column as a unique key
The following example creates a NOT NULL UNIQUE constraint to define the
column ss_no as a unique key for the employee table:
|
Defining a foreign key that references the primary key
The following example defines order_item.orditem_order_no as a foreign key
that references the primary key orders.order_no:
|
The second CREATE TABLE statement
in the previous example could have omitted the column name order_no in
the REFERENCES clause, since it refers to the primary
key of table orders.
Creating a check constraint
The following example creates a check constraint:
|
The column constraint syntax requires NOT NULL before PRIMARY
KEY or UNIQUE. Therefore, columns with these column-level
constraints cannot contain null values. If you require a unique constraint that permits
nulls, use a table-level UNIQUE constraint on a column that is not defined
as NOT NULL. In that case, SQL treats each null as distinct, so multiple
rows may contain null for the column without violating the constraint. For more information,
see Table constraints.