How can a primary key be added to a table in db2?
In DB2, you can add a primary key to a table using the following steps:
Firstly, use the `ALTER TABLE` statement to modify the structure of the table. For example, if you want to add a primary key column named `pk_column` to a table named `table_name`, you can execute the following command:
ALTER TABLE table_name ADD COLUMN pk_column INTEGER NOT NULL;
This will add a new column `pk_column` to the table, which will be used as the primary key.
Next, you will need to use the `ALTER TABLE` statement to define the `pk_column` column as the primary key. Run the following command:
ALTER TABLE table_name ADD CONSTRAINT pk_constraint PRIMARY KEY (pk_column);
This will create a primary key constraint named `pk_constraint` and apply it to the `pk_column` column.
Please note that the data types and column names in the above examples are placeholders, please replace them according to your actual needs.
After performing the above steps, the table will have a new primary key column, which will be defined as the primary key.