This Oracle tutorial explains how to create, drop, disable, and enable unique constraints in Oracle with syntax and examples.
A unique constraint is a single field or combination of fields that uniquely defines a record. Some of the fields can contain null values as long as the combination of values is unique.
Primary Key | Unique Constraint |
---|---|
None of the fields that are part of the primary key can contain a null value. | Some of the fields that are part of the unique constraint can contain null values as long as the combination of values is unique. |
Oracle does not permit you to create both a primary key and unique constraint with the same columns.
The syntax for creating a unique constraint using a CREATE TABLE statement in Oracle is:
The name of the table that you wish to create.
The columns that you wish to create in the table.
The name of the unique constraint.
The columns that make up the unique constraint.
Let's look at an example of how to create a unique constraint in Oracle using the CREATE TABLE statement.
In this example, we've created a unique constraint on the supplier table called supplier_unique. It consists of only one field - the supplier_id field.
We could also create a unique constraint with more than one field as in the example below:
The syntax for creating a unique constraint using an ALTER TABLE statement in Oracle is:
The name of the table to modify. This is the table that you wish to add a unique constraint to.
The name of the unique constraint.
The columns that make up the unique constraint.
Let's look at an example of how to add a unique constraint to an existing table in Oracle using the ALTER TABLE statement.
In this example, we've created a unique constraint on the existing supplier table called supplier_unique. It consists of the field called supplier_id.
We could also create a unique constraint with more than one field as in the example below:
The syntax for dropping a unique constraint in Oracle is:
The name of the table to modify. This is the table that you wish to remove the unique constraint from.
The name of the unique constraint to remove.
Let's look at an example of how to remove a unique constraint from a table in Oracle.
In this example, we're dropping a unique constraint on the supplier table called supplier_unique.
The syntax for disabling a unique constraint in Oracle is:
The name of the table to modify. This is the table whose unique constraint you wish to disable.
The name of the unique constraint to disable.
Let's look at an example of how to disable a unique constraint in Oracle.
In this example, we're disabling a unique constraint on the supplier table called supplier_unique.
The syntax for enabling a unique constraint in Oracle is:
The name of the table to modify. This is the table whose unique constraint you wish to enable.
The name of the unique constraint to enable.
Let's look at an example of how to enable a unique constraint in Oracle.
In this example, we're enabling a unique constraint on the supplier table called supplier_unique.