Sunday, March 11, 2012

double unique index

Hello.

I have a question. I need to make a double unique index on a table. for example: I have 2 columns, ColumnA and ColumnB. ColumnA can have duplicate values, so is ColumnB, but it should be impossible to have duplicate values on both columns. for example:

Row 1:

ColumnA = 1, ColumnB = 2

Row2:

ColumnA = 1, ColumnB = 2

this shouldn't be possible.

Row1:

ColumnA = 1, ColumnB = 2

Row2:

ColumnA = 1, ColumnB = 3

this should be possible Smile

is there any way I can do this?

thanks in advance Smile

You can create a UNIQUE constraint or a unique INDEX.

ALTER TABLE YourTable ADD CONSTRAINT UK_YourTable_ColumnA_ColumnB UNIQUE (ColumnA,ColumnB)

Or

CREATE UNIQUE INDEX IX_YourTable_ColumnA_ColumnB ON YourTable (ColumnA,ColumnB)

The constraint or index can also be clustered if you do not have a clustered PK/index/constraint yet.

No comments:

Post a Comment