Sub topic 6
Preventing Duplicates with UNIQUE
Assume you know basic SELECT, INSERT, and CREATE TABLE statements, along with the relational idea of a key. This chapter focuses on Oracle UNIQUE constraints and how they differ from primary keys.
What a UNIQUE constraint does
A UNIQUE constraint requires values to be different across all rows in a table. It is useful for business identifiers such as email addresses, usernames, invoice numbers, or combinations such as country and phone number.
A table can have several UNIQUE constraints, but it can have only one primary key. A unique constraint does not automatically make a column the table's main identifier.
CREATE TABLE customer_accounts (
account_id NUMBER GENERATED ALWAYS AS IDENTITY,
email VARCHAR2(255)
CONSTRAINT customer_accounts_email_uk UNIQUE,
country_code VARCHAR2(3) NOT NULL,
phone_number VARCHAR2(30) NOT NULL,
CONSTRAINT customer_accounts_pk PRIMARY KEY (account_id),
CONSTRAINT customer_accounts_phone_uk
UNIQUE (country_code, phone_number)
);The email constraint is defined inline, next to its column. The phone constraint is defined out of line, after the column definitions. Both constraints have explicit names. Naming constraints makes error messages easier to interpret and allows you to refer to the constraints later.
Single-column and composite uniqueness
A single-column constraint checks one column:
INSERT INTO customer_accounts (email, country_code, phone_number)
VALUES ('[email protected]', 'US', '5550100');
-- Fails because the email already exists.
INSERT INTO customer_accounts (email, country_code, phone_number)
VALUES ('[email protected]', 'US', '5550101');A composite unique constraint checks the combination of its columns. The same phone number can appear in different countries, but the same country and phone number cannot appear twice.
| Constraint | Allowed | Rejected |
|---|---|---|
| `UNIQUE (email)` | Different email values | Repeated email value |
| `UNIQUE (country_code, phone_number)` | Same phone in different countries | Same country and phone pair |
NULL values and UNIQUE
Oracle treats NULL as an unknown value, not as an ordinary value equal to another NULL. Therefore, a nullable unique column can contain multiple NULL values. A UNIQUE constraint does not require a value to be present.
If every row must have a distinct email address, combine UNIQUE with NOT NULL:
CREATE TABLE application_users (
user_id NUMBER GENERATED ALWAYS AS IDENTITY,
email VARCHAR2(255) NOT NULL,
CONSTRAINT application_users_pk PRIMARY KEY (user_id),
CONSTRAINT application_users_email_uk UNIQUE (email)
);The NOT NULL rule requires a value. The UNIQUE rule prevents two rows from having the same value. These rules solve different problems.
UNIQUE constraints and indexes
Oracle normally creates a unique index to enforce a unique constraint. Treat the constraint as the data rule and let Oracle manage its supporting index unless you have a specific storage or performance requirement.
A unique constraint can also support a foreign key. For example, another table can reference email if that email column has a primary key or unique constraint. This allows a referenced business identifier to be used as a parent key, although a generated numeric key is often simpler.
Changing a UNIQUE constraint
Add a named unique constraint to an existing table with ALTER TABLE:
ALTER TABLE application_users
ADD CONSTRAINT application_users_email_uk UNIQUE (email);If existing rows contain duplicate non-NULL emails, Oracle cannot enable the constraint until the duplicates are corrected. Remove a constraint by name:
ALTER TABLE application_users
DROP CONSTRAINT application_users_email_uk;Dropping the constraint removes the uniqueness rule. It may also remove its supporting index unless that index is retained explicitly.
Exercise
A nullable column has a UNIQUE constraint. Can several rows contain NULL, and what should you add if every row needs a distinct value?
Show answer
Yes. Oracle permits multiple NULL values. Add NOT NULL as well as UNIQUE when every row must contain a distinct value.