Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Unique Constraint for 2 columns in SQL 2005 Expand / Collapse
Author
Message
Posted Tuesday, March 31, 2009 5:13 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, September 8, 2010 11:23 AM
Points: 21, Visits: 88
I have 2 columns of datatype int (integer) in a table. Just like creating a unique constraint on a column , in order to get unique values in the table, can I create unique constraint for those 2 columns, so that no 2 values combined in the 2 columns are the same.

One of these 2 columns is a foreign key column



If you observe now the values for rows 3 and 4 are the same.. Now I want to have unique values for both the columns combined. I am currently using SQL 2005. Is there any solution for this in SQL 2005 ? Something like arriving at GUID from 2 columns ....

Thanks in Advance
Post #686879
Posted Tuesday, March 31, 2009 5:43 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 21, 2013 12:32 AM
Points: 2,556, Visits: 2,593
Just as simple as this...

-- Adding constraint to an existing table
ALTER TABLE SomeTable WITH CHECK ADD CONSTRAINT [UK_SomeTable_SomeCompositeKey] UNIQUE( KeyColumn1, KeyColumn2 )

-- Adding constraint when creating new table
CREATE TABLE SomeTable
(
KeyColumn1 INT NOT NULL,
KeyColumn2 INT NOT NULL,
CONSTRAINT [UK_SomeTable_SomeCompositeKey] UNIQUE
(
KeyColumn1,
KeyColumn2
)
)

Edit:
1. The ADDADD thing is the mis-interpretation of SSC, just use single ADD in the query
2. Added an example of adding constraint in a CREATE statement.


--Ramesh

Post #686900
Posted Tuesday, March 31, 2009 11:34 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, October 25, 2010 6:09 AM
Points: 1,621, Visits: 409
Hi

Ramesh, Instead of Unique Constraint why can't we have Unique Index??

Thanks -- Vijaya Kadiyala
www.dotnetvj.com



Thanks -- Vijaya Kadiyala
www.dotnetvj.com
SQL Server Articles For Beginers



Post #687278
Posted Tuesday, March 31, 2009 11:50 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
You can use a unique index. The constraint will actually create a unique index. That's how it works.

I can't tell from the original post if it's included in the sample, but one thing you can run into with a unique constraint/index is that this won't be prevented:

Col1 Col2
1 2
2 1

The only way I know of to prevent that is through code. That means either an insert proc, or a trigger, or something of that sort. Might be other methods, but that's the only one I can think of. (I've tried a few, none worked.)


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #687294
Posted Wednesday, April 1, 2009 2:55 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 21, 2013 12:32 AM
Points: 2,556, Visits: 2,593
Vijaya Kadiyala (3/31/2009)
Hi

Ramesh, Instead of Unique Constraint why can't we have Unique Index??

Thanks -- Vijaya Kadiyala
www.dotnetvj.com



There are no significant differences between creating a UNIQUE constraint and creating a unique index independent of a constraint. Enforcing a UNIQUE constraint will make the business rule/data integrity meaningful and thereby directing the objective of the UNIQUE index it creates.


--Ramesh

Post #687710
Posted Wednesday, April 1, 2009 4:30 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, July 2, 2014 3:48 AM
Points: 488, Visits: 1,328
CREATE TABLE [dbo].[t3] (
[n1] [int] not null,
[n2] [int] not null,
[n3] as n1 + n2 ,
[n4] as n1 * n2
CONSTRAINT [t3_unique_key] unique (n3,n4)

)

Create 2 computed columns n3,n4 as shown below. declare unique key constraint on it.
That shd detect the following scenario.


Regards,
Raj

Strictlysql.blogspot.com
Post #687749
Posted Wednesday, April 1, 2009 4:38 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, July 2, 2014 3:48 AM
Points: 488, Visits: 1,328
One more solution..
Copyrights to my collegue sitting in next desk.. :)

create table t3(

n1 int not null,

n2 int not null,

n3 as case when n1< n2

then cast(n1 as varchar)+','+cast(n2 as varchar) else

cast(n2 as varchar)+','+cast(n1 as varchar) end unique


);



insert into t3(n1,n2) values (10,11);

insert into t3(n2,n1) values (11,10);


Regards,
Raj

Strictlysql.blogspot.com
Post #687753
Posted Wednesday, April 1, 2009 6:00 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 1:45 AM
Points: 2,826, Visits: 3,866
arr.nagaraj (4/1/2009)

insert into t3(n1,n2) values (10,11);

insert into t3(n2,n1) values (11,10);

These two statements are semantically the same.

You probably wanted to write this instead:
insert into t3(n1,n2) values (10,11);
insert into t3(n1,n2) values (11,10);


Another possible solution to this requirement might be ("might" because I don't know the business problem):
CREATE TABLE dbo.t3 (
n1 int NOT NULL
,n2 int NOT NULL
,CONSTRAINT UQ_t3 UNIQUE (n1, n2)
,CONSTRAINT CK_t3_n1_LE_n2 CHECK (n1<=n2)
);



Best Regards,
Chris Büttner
Post #687802
Posted Wednesday, April 1, 2009 7:04 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
arr.nagaraj (4/1/2009)
CREATE TABLE [dbo].[t3] (
[n1] [int] not null,
[n2] [int] not null,
[n3] as n1 + n2 ,
[n4] as n1 * n2
CONSTRAINT [t3_unique_key] unique (n3,n4)

)

Create 2 computed columns n3,n4 as shown below. declare unique key constraint on it.
That shd detect the following scenario.


Clever. I thought of each of those separately, but not combined. Should work.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #687857
Posted Wednesday, April 1, 2009 8:34 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, July 2, 2014 3:48 AM
Points: 488, Visits: 1,328
Chris,

The buisness requirement is
n1,n2 shd be unique even when interchanged.

unique constraint on 2 columns shd work even when values are interchged.

n1 n2
11 10 ->allowed.
10 11 -> fail as 11,10( 10,11's already exists

your example i am afraid will fail as n1 > n2 at first insert itself, however by req it shd be allowed.


Regards,
Raj

Strictlysql.blogspot.com
Post #687984
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse