sqldb2

Set a column unique without being the primary key


I have the following table:

CREATE TABLE product (
id bigint not null,
product_type VARCHAR(50),
product_name VARCHAR(100),
available_from TIMESTAMP,
available_to TIMESTAMP,
PRIMARY KEY (id));

My table's key is the 'id'. When inserting in the table, I want the product_type to be UNIQUE. How can I do that without setting the product_type the PRIMARY KEY for my table.


Solution

  • ALTER TABLE <your table name>
     ADD CONSTRAINT unique_product_type UNIQUE(product_type);
    

    I can't see a table name in your create table SQL.