Administer > Database administration > Data persistence > Keys and indexes in Service Manager > Modifying a Key > Modify keys: Convert the first unique key to a primary key

Modify keys: Convert the first unique key to primary key

You can run the sm command together with system_addconstraint parameter to convert the first unique key of a table to a primary key. When you run this command, Service Manager first modifies the fields of the first unique keys to be NOT NULL (as if you ran system_addconstraint:0 as described in Modify keys: Add Not Null constraints to the first unique key in a table) and then applies a primary constraint to those keys.

To convert the first unique key to a primary key by using the command line, use one of the following methods:

Note: Make sure that Service Manager is offline before you execute this command.

Method 1

Run the following system_addconstraint command with a value of 1. You can use this method with an Oracle or DB2 database. You cannot use this with a SQL Server database.

sm -system_addconstraint:<target>:1

In this example, <target> represents the name of the Service Manager table on which you wish to add the constraint, and 1 indicates that you wish to convert the first unique key of the <target> to a primary key.

Method 2

Caution If Service Manager fails to perform the conversion by using the command in Method 1, or if you have a Microsoft SQL Server database, use the following method. However, you should note the following dangers when using this method:

  • Data loss will occur if you run this method on a unique index that contains null values.
  • This method invokes a full-table-copy operation, which may take a long time.
  • Any database exception error that occurs during the operation can cause unexpected results.

To use this method, follow these steps:

  1. Make sure that you have no null values on any unique index.
  2. Execute the following command: sm -system_addconstraint:<target>:2

Note Regardless of which method you use, an attempt to revert from a primary key to a unique key may cause unpredictable results.