Oracle UNIQUE Index

Summary: in this tutorial, you’ll learn how to use Oracle unique index to prevent duplicate values in the indexed column or columns of a table.

Introduction to Oracle UNIQUE index #

In Oracle, a unique index is an index that ensures duplicate values in the indexed columns.

To create a unique index, you use the CREATE UNIQUE INDEX statement:

CREATE UNIQUE INDEX index_name 
ON table_name (column1[,column2,...]);Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • First, specify the index name in the CREATE UNIQUE INDEX clause.
  • Second, provide a table and a comma-separated list of columns that you want to include in the unique index.

If the unique index includes one column, the values in the columns cannot be duplicate. However, if the indexed column is nullable, you can insert multiple NULLs into the column.

If the unique index include multiple columns, the combination of values in these columns cannot be duplicate.

Creating an Oracle UNIQUE index on one column #

First, create a members table:

CREATE TABLE members(
    id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    name VARCHAR2(50) NOT NULL,
    email VARCHAR2(100)
);Code language: PHP (php)

Second, create a new unique index for the email column:

CREATE UNIQUE INDEX members_email_unique
ON members(email);

Third, insert a new row into the members table:

INSERT INTO members(name, email)
VALUES('John Doe', '[email protected]');Code language: JavaScript (javascript)

Fourth, attempt to insert a row with the email that already exists in the email column of the members table:

INSERT INTO members(name, email)
VALUES('John Denver', '[email protected]');Code language: JavaScript (javascript)

Oracle issued the following error:

ORA-00001: unique constraint (OT.MEMBERS_EMAIL_UNIQUE) violated on table OT.MEMBERS columns (EMAIL)
ORA-03301: (ORA-00001 details) row with column values (EMAIL:'john.d@oracletutorial.com') already existsCode language: CSS (css)

However, you can insert multiple NULLs into the email columns:

INSERT INTO members(name)
VALUES
   ('John Denver'),
   ('David Bowie');Code language: SQL (Structured Query Language) (sql)

Finally, retrieve all rows from the members table:

SELECT * FROM members;

Output:

Oracle unique index example

Creating a UNIQUE index on two columns #

The following example creates a new table named unq_idx_demo with two columns a and b:

CREATE TABLE unq_idx_demo(
    a INT,
    b INT
);
Code language: SQL (Structured Query Language) (sql)

To create a unique index on the two columns a and b, you use the following statement:

CREATE UNIQUE INDEX unq_idx_demo_ab_i
ON unq_idx_demo(a,b);Code language: SQL (Structured Query Language) (sql)

The following statement inserts a new row into the unq_idx_demo table:

INSERT INTO unq_idx_demo(a,b)
VALUES(1,1);Code language: SQL (Structured Query Language) (sql)

Since we have a unique index on the a and b columns, the combination of values in both columns are used for evaluating duplicate.

The following statement works because the pair (1,2) does not exist:

INSERT INTO unq_idx_demo(a,b)
VALUES(1,2);Code language: SQL (Structured Query Language) (sql)

However, the following statement does not work because (1,1) already exists:

INSERT INTO unq_idx_demo(a,b)
VALUES(1,1);Code language: SQL (Structured Query Language) (sql)

Here is the error message:

SQL Error: ORA-00001: unique constraint (OT.UNQ_IDX_DEMO_AB_I) violatedCode language: SQL (Structured Query Language) (sql)

Oracle UNIQUE index, Primary Key constraint, and Unique constraint #

When you define a PRIMARY KEY or a UNIQUE constraint for a table, Oracle automatically creates a unique index on the primary key or unique key columns to enforce the uniqueness.

The unique index associated with the constraint always has the name of the constraint, unless specify it explicitly otherwise.

The following statement creates a table named t1 with a primary key:

CREATE TABLE t1 (
    pk1 INT PRIMARY KEY,
    c1 INT
);Code language: SQL (Structured Query Language) (sql)

To show the indexes of the t1 table, you use the following statement:

SELECT 
    index_name, 
    index_type, 
    visibility, 
    status 
FROM 
    all_indexes
WHERE 
    table_name = 'T1';Code language: SQL (Structured Query Language) (sql)

Here is the output:

Oracle UNIQUE Index with generated name example

The output indicates that the SYS_C007876 unique index was created automatically with the generated name.

To specify the name for the primary key column, you use the UNIQUE index as shown in the following query:

CREATE TABLE t2 (
    pk2 INT PRIMARY KEY 
        USING INDEX (
            CREATE INDEX t1_pk1_i ON t2 (pk2)
    ),
    c2 INT
);Code language: SQL (Structured Query Language) (sql)

In this example, we explicitly specify the name of the unique index.

SELECT 
    index_name, 
    index_type, 
    visibility, 
    status 
FROM 
    all_indexes
WHERE 
    table_name = 'T2';
Code language: SQL (Structured Query Language) (sql)

The output is:

Oracle UNIQUE Index with explicit name example

Instead of generating the index name, Oracle uses the one that we provided during table creation.

Summary #

  • Use Oracle UNIQUE index to ensure the uniqueness of values in the indexed column or columns.
  • The UNIQUE INDEX treats NULL differently so you can have multiple NULLs on the same index column.

Quiz #

Was this tutorial helpful?