To create a unique index in Oracle, you use the CREATE UNIQUE INDEX
statement, which ensures that all values in the specified column(s) are distinct across all rows in a table.
Understanding Unique Indexes in Oracle
A unique index is a powerful tool in Oracle for maintaining data integrity and optimizing query performance.
- What it does: It enforces the uniqueness of values in one or more columns within a table. This means that no two rows can have the same value (for a single-column index) or the same combination of values (for a composite index) in the indexed columns.
- Why use it:
- Data Integrity: Guarantees that specific data points remain unique, preventing duplicate entries that could corrupt your data.
- Performance: Speeds up data retrieval for queries that filter or sort by the indexed columns. It also helps Oracle quickly locate rows during
UPDATE
andDELETE
operations. - Constraint Enforcement: When you define a
PRIMARY KEY
orUNIQUE
constraint on a table, Oracle automatically creates a unique index behind the scenes to enforce that constraint. This is often the preferred method for declaring uniqueness.
Basic Syntax for Creating a Unique Index
The fundamental command for creating a unique index in Oracle is straightforward:
CREATE UNIQUE INDEX index_name
ON table_name (column1, column2, ..., columnN);
Let's break down the components of this statement:
CREATE UNIQUE INDEX
: These keywords signal to Oracle your intention to create a unique index. TheUNIQUE
keyword is crucial as it defines that the indexed columns must contain unique combinations of values.index_name
: This is a user-defined name for your index. It should be descriptive and follow Oracle's naming conventions (e.g.,UX_TableName_ColumnName
).ON table_name
: Specifies the name of the table on which the index will be created.(column1, column2, ..., columnN)
: A comma-separated list of one or more columns fromtable_name
that you want to include in the index.- For a single-column unique index, you list just one column.
- For a composite unique index, you list multiple columns. In this case, the combination of values across all listed columns must be unique for each row.
Example: Single-Column Unique Index
Suppose you have a table Customers
and want to ensure that each customer's CustomerID
is unique.
-
Create a sample table (if needed):
CREATE TABLE Customers ( CustomerID NUMBER(10), FirstName VARCHAR2(50), LastName VARCHAR2(50), Email VARCHAR2(100) );
-
Create the unique index:
CREATE UNIQUE INDEX UX_Customers_CustomerID ON Customers (CustomerID);
This statement creates an index named
UX_Customers_CustomerID
on theCustomerID
column. If you try to insert a customer with anCustomerID
that already exists, Oracle will raise anORA-00001: unique constraint violated
error.
Example: Composite Unique Index
Consider a scenario where you want to ensure that for a ProjectTasks
table, no two tasks for the same ProjectID
can have the same TaskName
. The TaskName
alone might not be unique across all projects, but the combination of ProjectID
and TaskName
must be.
-
Create a sample table:
CREATE TABLE ProjectTasks ( TaskID NUMBER(10) PRIMARY KEY, ProjectID NUMBER(10), TaskName VARCHAR2(100), Status VARCHAR2(20) );
-
Create the composite unique index:
CREATE UNIQUE INDEX UX_ProjectTasks_ProjTask ON ProjectTasks (ProjectID, TaskName);
With this index, a task with
(ProjectID=101, TaskName='Design UI')
is unique. You can also have(ProjectID=102, TaskName='Design UI')
because the combination is different. However, you cannot have another entry(ProjectID=101, TaskName='Design UI')
.
Important Considerations
-
NULL Values: Oracle's unique indexes and unique constraints allow multiple
NULL
values in the indexed column(s). This is becauseNULL
is considered an unknown value, and thus, twoNULL
s are not considered equal. If you need to ensure noNULL
s and all unique values, you must also define aNOT NULL
constraint on the column. -
Unique Constraints vs. Unique Indexes: While you can explicitly create a unique index, it's often better practice to define a
UNIQUE
constraint. When you define aUNIQUE
constraint, Oracle automatically creates an underlying unique index to enforce it. This approach clearly expresses the intent for data integrity.-- Example: Adding a UNIQUE constraint, which implicitly creates a unique index ALTER TABLE Customers ADD CONSTRAINT UQ_Customers_Email UNIQUE (Email);
This adds a constraint
UQ_Customers_Email
to theEmail
column, making sure all email addresses are unique. -
Existing Data: If the table already contains duplicate values in the columns you specify for the unique index, the
CREATE UNIQUE INDEX
statement will fail. You must clean up the duplicate data before the index can be created successfully. -
Performance Overhead: While unique indexes significantly improve read performance, they introduce a slight overhead during
INSERT
,UPDATE
, andDELETE
operations because Oracle must maintain the index structure. This trade-off is typically worthwhile for the data integrity and query performance benefits.
Managing Unique Indexes
- Viewing Unique Indexes: You can query Oracle's data dictionary views to see information about your unique indexes.
SELECT index_name, table_name, uniqueness, status FROM USER_INDEXES WHERE table_name = 'CUSTOMERS';
- Dropping a Unique Index: If an index is no longer needed, you can drop it.
DROP INDEX UX_Customers_CustomerID;
Note: If the unique index was created implicitly by a
UNIQUE
orPRIMARY KEY
constraint, you should drop the constraint instead of the index directly. Dropping the constraint will automatically drop its associated index.ALTER TABLE Customers DROP CONSTRAINT UQ_Customers_Email;
Best Practices
- Prioritize Constraints: For enforcing business rules, use
PRIMARY KEY
orUNIQUE
constraints. They offer better semantic clarity and Oracle handles the underlying index creation. - Descriptive Naming: Adopt a consistent naming convention (e.g.,
UX_TableName_ColumnName
) for your indexes to improve readability and maintenance. - Validate Data: Always check for existing duplicate data before attempting to create a unique index on an existing table to avoid errors.
For more comprehensive details on indexing in Oracle, refer to the official Oracle Database SQL Language Reference.