Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > microsoft.public.sqlserver.programming > #31268

Re: Unique Constraint Based on Dual GUID

From Erland Sommarskog <esquel@sommarskog.se>
Newsgroups microsoft.public.sqlserver.programming
Subject Re: Unique Constraint Based on Dual GUID
Date 2015-08-11 21:35 +0200
Organization Erland Sommarskog
Message-ID <XnsA4F3DBB003712Yazorman@127.0.0.1> (permalink)
References <mqbsbp$9va$1@dont-email.me>

Show all headers | View raw


Michael Cole (invalid@invalid.com) writes:
> I have a table with a GUID as PK (created via NewId())
> 
> I also have a table to indicate links between records in this table, 
> with a dual PK of GUIDS (for the two records that connect to each 
> other)
> 
> Putting a constrain on these two fields will limit it to only one 
> combination of the two fields, i.e., a link of A to B, but I also need 
> to ensure that the link is not duplicated as B to A - the link is 
> non-directional.
> 
> My idea was to include a calculated field of the two GUIDs XORed 
> together, and place a constraint on this calculated field.  Can anyone 
> see any issues with this idea?
> 

There is obviously a risk for false positives:

SELECT CASE WHEN dbo.GUIDXor('DF468CF1-9035-477D-BE8B-3E597509F8E2',	
                             '1B05A03D-B830-43A8-8396-9FE8364F952C') = 
                 dbo.GUIDXor('DF468CF1-9035-43A8-BE8B-3E597509F8E2',	
                             '1B05A03D-B830-477D-8396-9FE8364F952C')
            THEN 1
            ELSE 0
       END

Here I have simply swapped a number of bytes in the GUID. What the
actual probability for this to happen, I don't know though.

You need to create the functon WITH SCHEMABINDING, but you may already
have discovered that.

-- 
Erland Sommarskog, Stockholm, esquel@sommarskog.se

Back to microsoft.public.sqlserver.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Unique Constraint Based on Dual GUID Michael Cole <invalid@invalid.com> - 2015-08-11 14:07 +1000
  Re: Unique Constraint Based on Dual GUID Erland Sommarskog <esquel@sommarskog.se> - 2015-08-11 21:35 +0200
  Re: Unique Constraint Based on Dual GUID rpresser <rpresser@gmail.com> - 2015-08-11 12:46 -0700
  Re: Unique Constraint Based on Dual GUID --CELKO-- <jcelko212@earthlink.net> - 2015-08-11 15:30 -0700
    Re: Unique Constraint Based on Dual GUID rpresser <rpresser@gmail.com> - 2015-08-11 20:13 -0700
  Re: Unique Constraint Based on Dual GUID --CELKO-- <jcelko212@earthlink.net> - 2015-08-12 18:26 -0700
    Re: Unique Constraint Based on Dual GUID Michael Cole <invalid@invalid.com> - 2015-08-13 11:59 +1000

csiph-web