|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, September 08, 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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Friday, February 22, 2013 12:03 AM
Points: 2,555,
Visits: 2,587
|
|
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
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Monday, October 25, 2010 6:09 AM
Points: 1,621,
Visits: 409
|
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:09 PM
Points: 15,439,
Visits: 9,569
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Friday, February 22, 2013 12:03 AM
Points: 2,555,
Visits: 2,587
|
|
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
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 12:27 AM
Points: 483,
Visits: 1,191
|
|
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
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 12:27 AM
Points: 483,
Visits: 1,191
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 3:36 AM
Points: 2,522,
Visits: 3,616
|
|
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
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:09 PM
Points: 15,439,
Visits: 9,569
|
|
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
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 12:27 AM
Points: 483,
Visits: 1,191
|
|
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
|
|
|
|