How to create a unique index in db2?

In DB2, you can create a unique index by following these steps:

  1. Open the DB2 command line window or utilize the DB2 console.
  2. Connect to the database that you want to create an index on. Use the following command to connect to the database:
db2 connect to <database_name>
  1. Create a unique index by using the following command:
db2 CREATE UNIQUE INDEX <index_name> ON <table_name> (<column_name>)

In the command above, replace with the name of the index, with the name of the table to which the index belongs, and with the name of the column for which the index will be created.

For example, to create a unique index for the employee_id column in a table named employees, you can use the following command:

db2 CREATE UNIQUE INDEX emp_id_idx ON employees (employee_id)
  1. After executing the command, DB2 will create a unique index.

Please note that creating a unique index requires having sufficient permissions.

bannerAds