How to add create composite primary key in sql server

How to add create composite primary key in sql server

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

Implementation :

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.

How to add create composite primary key in sql server using design view

Now let us discuss how to create or add composite primary key using tsql. Below is the syntax.

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.

Conclusion:

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.

Leave a Reply

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