Saturday, 8 December 2018

Creating composite primary key in the existing table SQL SERVER

A composite key is a combination of two or more columns in a table that can be used to uniquely identify each row in the table. As we know if we want to make any changes on the existing table we need to use Alter commend.
Let’s see how to create composite primary key on my existing table.
There is no any key on this table.
   

With the help of alter we can create the key.
ALTER TABLE EMP_SOURCE
ADD CONSTRAINT PK_EMP_SOURCE
PRIMARY KEY (EMP_ID,EMP_NAME,EMP_ADD)
    
PK is created successfully.
  


NOTE: when we are creating composite primary key keep in mind composite column should be not null. If there is any column null in this case it will throw an error as below.
  

Popular Posts