How do you set a primary key in db2 when creating a table?

In DB2, setting a table’s primary key can be achieved by following these steps:

  1. Specify the primary key constraint when defining columns in a table creation, for example:
  2. The table named TableName is created with two columns – Column1 which is an integer and cannot be empty, and Column2 which is a string with a maximum length of 50 characters. The primary key is set to Column1.
  3. You can use the ALTER TABLE statement to add a primary key constraint after creating the table, for example:
  4. Add a primary key constraint to the table TableName on Column1.
  5. To create a composite primary key on multiple columns, you can specify multiple columns as primary keys when defining them, for example:
  6. Create a table called TableName with two columns: Column1 must be an integer and cannot be empty, and Column2 must be a string of up to 50 characters and cannot be empty. Both Column1 and Column2 together make up the primary key of the table.

You can effectively set a table’s primary key either by defining the primary key constraint when creating the table or by using ALTER TABLE after the table is created.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds