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

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Erland Sommarskog <esquel@sommarskog.se>
Newsgroups microsoft.public.sqlserver.programming
Subject Re: Unique Constraint Based on Dual GUID
Date Tue, 11 Aug 2015 21:35:46 +0200
Organization Erland Sommarskog
Lines 35
Message-ID <XnsA4F3DBB003712Yazorman@127.0.0.1> (permalink)
References <mqbsbp$9va$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding 8bit
Injection-Info mx02.eternal-september.org; posting-host="17bcee2d5ac1a04ffd1127e62f64e159"; logging-data="21614"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WcjDJ1E33JFQGszqSflWT"
User-Agent Xnews/2006.08.24 Mime-proxy/2.1.c.0 (Win32)
Cancel-Lock sha1:wxXzKD74wbfMlQhOU4yzlWfiHSk=
Xref csiph.com microsoft.public.sqlserver.programming:31268

Show key headers only | 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