In this tutorial i am going to explain about how to add create composite primary key in sql server. In my previous tutorial i have explained about
Now we will see the implementation of how to add create composite primary key in sql server.
First we will check how to add composite primary key in table design. Open the design view of the table and choose the columns that you want to add composite primary key, right click and select Set Primary Key option. Then the primary key will be created on that column. You can see the key against that column as shown in the figure.
Now let us discuss how to create or add composite primary key using tsql. Below is the syntax.
ALTER TABLE [TableName]
ADD CONSTRAINT [ConstraintName]
PRIMARY KEY (Column1,Column2,Column3)
Now let’s see the practical example. For example i have a testtable with column Id and Name column and would like to create composite primary key on Id and Name column. It can be achieved using the below query.
ALTER TABLE TestTable
ADD CONSTRAINT PK_TestCompositeKey
PRIMARY KEY (ID, NAME)
Hope you learned how to add create composite primary key in sql server. Do you like this tutorial? Help us to improve. Please post your comments and feedback below.