---
title: "Unique indexes"
url: "https://docs.yugabyte.com/stable/explore/ycql-language/indexes-constraints/unique-index-ycql/"
---

# Unique indexes

Using Unique indexes in YCQL

Explore unique indexes in YugabyteDB using YCQL

If you need values in some of the columns to be unique, you can specify your index as `UNIQUE`.

When a unique index is applied to two or more columns, the combined values in these columns can't be duplicated in multiple rows. Note that because a `NULL` value is treated as a distinct value, you can have multiple `NULL` values in a column with a unique index.

If a table has a primary key defined, a corresponding unique index is created automatically.

The syntax to create a unique index in YCQL is as follows:

```sql
CREATE UNIQUE INDEX index_name ON table_name(column_list);
```

## Example

Before you start

The examples will run on any YugabyteDB universe.  
To create and connect to a universe, see [Set up YugabyteDB universe](/stable/explore/cluster-setup-local/).

1. Create a keyspace and a table as follows:
   
   ```cql
   ycqlsh> CREATE KEYSPACE yb_demo;
   ycqlsh> USE yb_demo;
   ycqlsh> CREATE TABLE employees(employee_no integer,name text,department text, PRIMARY KEY(employee_no))
           WITH transactions = {'enabled': 'true'};
   ```
2. Create a `UNIQUE` index for the `name` column in the `employees` table to allow only unique names in your table.
   
   ```cql
   CREATE UNIQUE INDEX index_employee_name ON employees(name);
   ```
3. Use the [DESCRIBE INDEX](/stable/api/ycqlsh/#describe "DESCRIBE INDEX") command to verify the index creation.
   
   ```cql
   ycqlsh:yb_demo> DESCRIBE INDEX index_employee_name;
   ```
   
   ```output
   CREATE UNIQUE INDEX index_employee_name ON yb_demo.employees (name) INCLUDE (employee_no)
       WITH transactions = {'enabled': 'true'};
   ```
4. Insert values into the table and verify that no duplicate names are created.
   
   ```cql
   ycqlsh:yb_demo> INSERT INTO employees(employee_no, name, department) VALUES (1, 'John', 'Sales');
   ycqlsh:yb_demo> INSERT INTO employees(employee_no, name, department) VALUES (2, 'Bob', 'Marketing');
   ycqlsh:yb_demo> INSERT INTO employees(employee_no, name, department) VALUES (3, 'Bob', 'Engineering');
   ```
   
   ```output
   InvalidRequest: Error from server: code=2200 [Invalid query] message="Execution Error. Duplicate value disallowed by unique index index_employee_name
   INSERT INTO employees(employee_no, name, department) VALUES (3, 'Bob', 'Engineering');
          ^^^^
    (ql error -300)"
   ```

## Learn more

[CREATE INDEX](/stable/api/ycql/ddl_create_index/ "CREATE INDEX")
