简体   繁体   中英

How to create combined primary key in SQL Server 2008?

I have a table, I created a composite primary key from field word1 and word2, their data type are both int.

Now I want to avoid records like this:

-------- Word1---------Word2--------------
           1             2  
           2             1

I want to treat - 1 2 - and - 2 1 - as one key.

How can I do this?

I think you would need to create a CHECK constraint that calls a UDF that performs this check. As far as I know there is no built in way to do this with SQL Server. Something like this:

ALTER TABLE MyTable
ADD CONSTRAINT SwappableUnique CHECK(dbo.SwappableUnique(Word1, Word2))

You can read up more on CHECK constraints here .

For every new row to be inserted, if word1 and word 2 are different values, Always insert them with lower of the two values in word1, and the higher of the two values in word2.

Insert myTable(word1, word2, etc.)
values (case When @word1 < @word2 then @word1 else @word2 End,
        case When @word1 < @word2 then @word2 else @word1 End,
        etc.)

or use a trigger

Create trigger NoWord1Word2Dupes 
On myTable for Insert, Update
As
  Begin
    If Exists(Select * From MyTable e
                join inserted I    
                   on i.Word1 = e.word2
                      and i.word2 = e.word1)
       Begin
           Rollback Transaction
           Raiserror ('Duplicate exists with swapped word1, word2 values.', 16, 1)
       End
  End   

A check constraint can be used to ensure no "transitive" keys. An example:

CREATE TABLE MyTable
 (
   Word1  int  not null
  ,Word2  int  not null
  ,constraint PK_MyTable
    primary key clustered (Word1, Word2)
  ,constraint CK_MyTable__KeyCheck
    check (Word1 < Word2)
 )

This prevents any rows where Word2 is less than or equal to Word1. (If Word1 = Word2 is alloed, makey it check (Word1 <= Word2)

This does leave a possible indexing problem: to find all rows referencing value N, you have to check in both columns...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM