Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create primary key by using two columns in SQL Server

I need to create table with two columns. These columns have to be created with primary key. For example Column 1 name ID and column 2 name Name. ID is auto incremented. When data is inserted into the table, all names has to be different. How can I do that? Please help me.

like image 303
Mehmet Yetkin Avatar asked Oct 19 '25 18:10

Mehmet Yetkin


1 Answers

To create the primary key:

ALTER TABLE dbo.YourTableNameHere
ADD CONSTRAINT PK_YourTable
PRIMARY KEY(ID, Name)

and to make sure the Name is unique:

ALTER TABLE dbo.YourTableNameHere
ADD CONSTRAINT UNIQUE_Name UNIQUE(Name)
like image 84
marc_s Avatar answered Oct 23 '25 13:10

marc_s