X-Received: by 10.182.38.164 with SMTP id h4mr27175179obk.40.1439322404483; Tue, 11 Aug 2015 12:46:44 -0700 (PDT) X-Received: by 10.140.105.102 with SMTP id b93mr275752qgf.26.1439322404360; Tue, 11 Aug 2015 12:46:44 -0700 (PDT) Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!se8no2125154igc.0!news-out.google.com!b31ni6136qge.0!nntp.google.com!69no5083264qgl.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: microsoft.public.sqlserver.programming Date: Tue, 11 Aug 2015 12:46:44 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.61.135.70; posting-account=SOVadwoAAAB3h7W1MLW9kMYtEc2JW2L8 NNTP-Posting-Host: 173.61.135.70 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <6516db0e-cb01-4b62-99fa-c304a5ee9787@googlegroups.com> Subject: Re: Unique Constraint Based on Dual GUID From: rpresser Injection-Date: Tue, 11 Aug 2015 19:46:44 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: csiph.com microsoft.public.sqlserver.programming:31269 On Tuesday, August 11, 2015 at 12:08:02 AM UTC-4, Michael Cole wrote: > 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? Rather than an XOR, which as Erland points out could cause false positives, why not use a calculated field that appends the two GUIDs together with the one that is "less" coming first? CREATE FUNCTION OneWayOnly ( @guid1 UNIQUEIDENTIFIER , @guid2 UNIQUEIDENTIFIER ) RETURNS VARCHAR(72) AS BEGIN DECLARE @g1 VARCHAR(36) , @g2 VARCHAR(36) , @result VARCHAR(72); SET @g1 = CONVERT(VARCHAR(36), @guid1); SET @g2 = CONVERT(VARCHAR(36), @guid2); IF @g1 < @g2 SET @result = @g1 + @g2; ELSE SET @result = @g2 + @g1; RETURN @result; END;