SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Unique Constraint for 2 columns in SQL 2005


Unique Constraint for 2 columns in SQL 2005

Author
Message
msforumpost
msforumpost
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 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
Ramesh Saive
Ramesh Saive
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3466 Visits: 2643
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


Vijaya Kadiyala
Vijaya Kadiyala
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1887 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



GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23391 Visits: 9730
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
Ramesh Saive
Ramesh Saive
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3466 Visits: 2643
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


arr.nagaraj
arr.nagaraj
SSC Eights!
SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)

Group: General Forum Members
Points: 854 Visits: 1590
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

http://Strictlysql.blogspot.com
arr.nagaraj
arr.nagaraj
SSC Eights!
SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)

Group: General Forum Members
Points: 854 Visits: 1590
One more solution..
Copyrights to my collegue sitting in next desk.. Smile

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

http://Strictlysql.blogspot.com
Christian Buettner-167247
Christian Buettner-167247
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3555 Visits: 3889
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
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23391 Visits: 9730
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
arr.nagaraj
arr.nagaraj
SSC Eights!
SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)SSC Eights! (854 reputation)

Group: General Forum Members
Points: 854 Visits: 1590
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

http://Strictlysql.blogspot.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search