Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Erland Sommarskog 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: References: 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 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