Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 29, 2012 02:22 PM by eralper
Participant
1126 Points
1299 Posts
Nov 29, 2012 01:50 PM|LINK
Hello,
I have a scenario where 4 column values combined should be unique.
Customer ID, ProductID, StartDate, EndDate are the 4 columns in 10 columns table.
I want those 4 columns as Primary(unique) column. Column as Independent should not be unique but the combination of 4 columns should be unique.
How can I achieve this ?
TIA
Star
9337 Points
1447 Posts
Nov 29, 2012 01:59 PM|LINK
ALTER TABLE <table> ADD PRIMARY KEY(CustomerID, ProductID, StartDate, EndDate);
If Primary key already exists then
ALTER TABLE <table> DROP PRIMARY KEY, ADD PRIMARY KEY(CustomerID, ProductID, StartDate, EndDate);
Contributor
6048 Points
971 Posts
Nov 29, 2012 02:22 PM|LINK
Instead of Primary Key a unique Constraint is also possible
alter table TableName add constraint ConstraintName unique (c1,c2,c3,c4)
ramll
Participant
1126 Points
1299 Posts
Set 4 column values to be Unique
Nov 29, 2012 01:50 PM|LINK
Hello,
I have a scenario where 4 column values combined should be unique.
Customer ID, ProductID, StartDate, EndDate are the 4 columns in 10 columns table.
I want those 4 columns as Primary(unique) column. Column as Independent should not be unique but the combination of 4 columns should be unique.
How can I achieve this ?
TIA
me_ritz
Star
9337 Points
1447 Posts
Re: Set 4 column values to be Unique
Nov 29, 2012 01:59 PM|LINK
eralper
Contributor
6048 Points
971 Posts
Re: Set 4 column values to be Unique
Nov 29, 2012 02:22 PM|LINK
Instead of Primary Key a unique Constraint is also possible
SQL Server 2012