SOLVED

Unusual uniqueness requirement on two fields in a table

%3CLINGO-SUB%20id%3D%22lingo-sub-2934569%22%20slang%3D%22en-US%22%3EUnusual%20uniqueness%20requirement%20on%20two%20fields%20in%20a%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2934569%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20table%20called%20SF_ACCOUNTS%20that%20represents%20the%20chart%20of%20accounts%20in%20a%20bookkeeping%20system.%20Among%20the%20fields%20of%20the%20table%20are%20sName%20and%20sAltName%2C%20the%20name%20of%20the%20account%20and%20an%20alternate%20name.%20I%20need%20the%20entries%20in%20sName%20to%20be%20unique%2C%20and%20I%20need%20the%20entries%20in%20sAltName%20to%20be%20unique%20with%20this%20additional%20requirement%3A%3C%2FP%3E%3CUL%3E%3CLI%3EAny%20sName%20must%20not%20match%20any%20sAltName%20except%20when%20they%20are%20in%20the%20same%20record.%3C%2FLI%3E%3C%2FUL%3E%3CP%3EIs%20there%20a%20way%20of%20setting%20up%20an%20index%20%2F%20constraint%20%2F%20whatever%20for%20this%20table%20to%20fulfill%20these%20requirements%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2940762%22%20slang%3D%22en-US%22%3ERe%3A%20Unusual%20uniqueness%20requirement%20on%20two%20fields%20in%20a%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2940762%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F272636%22%20target%3D%22_blank%22%3E%40robertrackl%3C%2FA%3E%26nbsp%3B--%26nbsp%3B%20You%20should%20be%20able%20to%20create%20a%20few%20unique%20constraints%20to%20address%20your%20requirements.%26nbsp%3B%20Below%20is%20an%20example%20based%20loosely%20on%20the%20information%20you%20provided%20regarding%20your%20table.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20my%20SF_ACCOUNTS%20table%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20TABLE%20SF_ACCOUNTS%20(AccountID%20smallint%20identity(1%2C1)%2CsName%20nvarchar(128)%2C%20sAltName%20nvarchar(128))%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EI'll%20create%20the%20following%20unique%20constraints%3A%3C%2FP%3E%0A%3CP%3E1.%26nbsp%3B%20One%20unique%20constraint%20for%20the%20sName%3C%2FP%3E%0A%3CP%3E2.%26nbsp%3B%20One%20unique%20constraint%20for%20the%20sAltName%3C%2FP%3E%0A%3CP%3E3.%26nbsp%3B%20One%20unique%20constraint%20for%20the%20combination%20of%20AccountID%2C%20sName%2C%20sAltName%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EALTER%20TABLE%20SF_ACCOUNTS%20ADD%20CONSTRAINT%20UniquesName%20UNIQUE(sName)%0AALTER%20TABLE%20SF_ACCOUNTS%20ADD%20CONSTRAINT%20UniquesAltName%20UNIQUE(sAltName)%0AALTER%20TABLE%20SF_ACCOUNTS%20ADD%20CONSTRAINT%20validNames%20UNIQUE%20(AccountId%2CsName%2CsAltName)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EAnd%20test%20using%20somethin%20like%20what%20is%20shown%20below%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3E--%20This%20is%20valid%0AINSERT%20INTO%20SF_ACCOUNTS%20VALUES%20('an%20sname'%2C'an%20saltname')%0A--%20This%20is%20valid%0AINSERT%20INTO%20SF_ACCOUNTS%20VALUES%20('an%20sname%20with%20the%20same%20saltname'%2C'an%20sname%20with%20the%20same%20saltname')%0A--%20This%20is%20not%20valid%0AINSERT%20INTO%20SF_ACCOUNTS%20VALUES%20('an%20sname'%2C'an%20saltname')%0A--%20This%20is%20not%20valid%0AINSERT%20INTO%20SF_ACCOUNTS%20VALUES%20('a%20new%20sname'%2C'an%20saltname')%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EHope%20that%20helps.%26nbsp%3B%20Take%20care.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2947359%22%20slang%3D%22en-US%22%3ERe%3A%20Unusual%20uniqueness%20requirement%20on%20two%20fields%20in%20a%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2947359%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F272636%22%20target%3D%22_blank%22%3E%40robertrackl%3C%2FA%3E%26nbsp%3B%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F305348%22%20target%3D%22_blank%22%3E%40bake13%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20might%20be%20missing%20something%20in%20the%20requirement%2C%20but%20looking%20at%20the%20third%20constraint%20there%2C%20bake%2C%20that's%20not%20quite%20going%20to%20preclude%20an%20sAltName%20featuring%20in%20the%20sName%20column.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20example%2C%20the%20following%20will%20pass%20the%20third%20constraint%20where%20-%20again%2C%20so%20long%20as%20I've%20understood%20the%20requirement%20correctly%20-%20it%20shouldn't%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CTABLE%20border%3D%221%22%20width%3D%22100%25%22%3E%3CTBODY%3E%3CTR%3E%3CTD%3E%3CSTRONG%3EsName%3C%2FSTRONG%3E%3C%2FTD%3E%3CTD%3E%3CFONT%20color%3D%22%23000000%22%3E%3CSTRONG%3EsAltName%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20width%3D%2250%25%22%3Ean%20sname%3C%2FTD%3E%3CTD%20width%3D%2250%25%22%3E%3CFONT%20color%3D%22%23FF9900%22%3Ean%20saltname%3C%2FFONT%3E%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%20width%3D%2250%25%22%3E%3CFONT%20color%3D%22%23FF9900%22%3Ean%20saltname%3C%2FFONT%3E%3C%2FTD%3E%3CTD%20width%3D%2250%25%22%3Esomething%20else%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20first%20two%20constraints%20are%20necessary%20but%20I've%20gone%20with%20a%20function%20to%20satisfy%20the%20robert's%20bullet-pointed%20requirement%20(i.e.%20as%20the%20third%20constraint).%20This%20throws%20the%20expected%20constraint%20violation%20when%20using%20the%20above%20bogus%20data.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELet%20me%20know%20your%20thoughts.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20TABLE%20dbo.test%0A(%0A%20sName%20varchar(max)%20NOT%20NULL%0A%20%2C%20sAltName%20varchar(max)%20NOT%20NULL%0A)%0AGO%0A%0A%0ACREATE%20FUNCTION%20dbo.SNameInAltName%20(%40sName%20AS%20varchar(max))%0ARETURNS%20int%0AAS%0ABEGIN%0A%20IF%20EXISTS%20(SELECT%20sAltName%20FROM%20dbo.test%20AS%20%5Bt%5D%20WITH%20(nolock)%20WHERE%20%5Bt%5D.sAltName%20%3D%20%40sName)%20%0A%20%20return%201%3B%0A%0A%20return%200%3B%0AEND%0AGO%0A%0A%0AALTER%20TABLE%20dbo.test%0A%20ADD%20CONSTRAINT%20sNameNotInAltNames%20CHECK%20(dbo.SNameInAltName(sName)%20%3D%200)%0AGO%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2957220%22%20slang%3D%22en-US%22%3ERe%3A%20Unusual%20uniqueness%20requirement%20on%20two%20fields%20in%20a%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2957220%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F314983%22%20target%3D%22_blank%22%3E%40LainRobertson%3C%2FA%3E%26nbsp%3B%2C%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F305348%22%20target%3D%22_blank%22%3E%40bake13%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBuilding%20on%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F314983%22%20target%3D%22_blank%22%3E%40LainRobertson%3C%2FA%3E%26nbsp%3B's%20ideas%2C%20this%20is%20what%20I%20have%20come%20up%20with%20(it%20prevents%20repeating%20something%20across%20the%20sName%20and%20sAltName%20columns%2C%20but%20it%20also%20prevents%20sName%20and%20sAltName%20being%20the%20same%20in%20the%20same%20record%20which%20I%20am%20trying%20to%20allow)%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20TABLE%20dbo.test%0A(sName%20nvarchar(128)%20NOT%20NULL%2C%20sAltName%20nvarchar(256)%20NOT%20NULL)%0AGO%0A%0ACREATE%20UNIQUE%20NONCLUSTERED%20INDEX%20%5BIX_test_sName%5D%20ON%20%5Bdbo%5D.%5Btest%5D%20(%5BsName%5D%20ASC)%0AGO%0ACREATE%20UNIQUE%20NONCLUSTERED%20INDEX%20%5BIX_test_sAltName%5D%20ON%20%5Bdbo%5D.%5Btest%5D%20(%5BsAltName%5D%20ASC)%0AGO%0A%0ACREATE%20FUNCTION%20dbo.SNameInAltName%20(%40sName%20AS%20nvarchar(128))%0ARETURNS%20bit%0AAS%0ABEGIN%0A%20IF%20EXISTS%20(SELECT%20sAltName%20FROM%20dbo.test%20AS%20%5Bt%5D%20WITH%20(nolock)%20WHERE%20%5Bt%5D.sAltName%20%3D%20%40sName)%20%0A%20%20return%201%3B%0A%20return%200%3B%0AEND%0AGO%0A%0ACREATE%20FUNCTION%20dbo.SAltNameInName%20(%40sAltName%20AS%20nvarchar(256))%0ARETURNS%20bit%0AAS%0ABEGIN%0A%20IF%20EXISTS%20(SELECT%20sName%20FROM%20dbo.test%20AS%20%5Bt%5D%20WITH%20(nolock)%20WHERE%20%5Bt%5D.sName%20%3D%20%40sAltName)%20%0A%20%20return%201%3B%0A%20return%200%3B%0AEND%0AGO%0A%0AALTER%20TABLE%20dbo.test%0A%20ADD%20CONSTRAINT%20sNameNotInAltNames%20CHECK%20(dbo.SNameInAltName(sName)%20%3D%200)%0AGO%0AALTER%20TABLE%20dbo.test%0A%20ADD%20CONSTRAINT%20sAltNameNotInNames%20CHECK%20(dbo.SAltNameInName(sAltName)%20%3D%200)%0AGO%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3ECan%20someone%20help%20with%20the%20last%20requirement%20that%20sName%20%3D%20sAltName%20in%20the%20same%20record%20should%20be%20allowed%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I have a table called SF_ACCOUNTS that represents the chart of accounts in a bookkeeping system. Among the fields of the table are sName and sAltName, the name of the account and an alternate name. I need the entries in sName to be unique, and I need the entries in sAltName to be unique with this additional requirement:

  • Any sName must not match any sAltName except when they are in the same record.

Is there a way of setting up an index / constraint / whatever for this table to fulfill these requirements?

6 Replies

Hi @robertrackl --  You should be able to create a few unique constraints to address your requirements.  Below is an example based loosely on the information you provided regarding your table.

 

Here is my SF_ACCOUNTS table

CREATE TABLE SF_ACCOUNTS (AccountID smallint identity(1,1),sName nvarchar(128), sAltName nvarchar(128))

I'll create the following unique constraints:

1.  One unique constraint for the sName

2.  One unique constraint for the sAltName

3.  One unique constraint for the combination of AccountID, sName, sAltName 

ALTER TABLE SF_ACCOUNTS ADD CONSTRAINT UniquesName UNIQUE(sName)
ALTER TABLE SF_ACCOUNTS ADD CONSTRAINT UniquesAltName UNIQUE(sAltName)
ALTER TABLE SF_ACCOUNTS ADD CONSTRAINT validNames UNIQUE (AccountId,sName,sAltName)

And test using somethin like what is shown below:

-- This is valid
INSERT INTO SF_ACCOUNTS VALUES ('an sname','an saltname')
-- This is valid
INSERT INTO SF_ACCOUNTS VALUES ('an sname with the same saltname','an sname with the same saltname')
-- This is not valid
INSERT INTO SF_ACCOUNTS VALUES ('an sname','an saltname')
-- This is not valid
INSERT INTO SF_ACCOUNTS VALUES ('a new sname','an saltname')

Hope that helps.  Take care.

@robertrackl , @bake13 

 

I might be missing something in the requirement, but looking at the third constraint there, bake, that's not quite going to preclude an sAltName featuring in the sName column.

 

For example, the following will pass the third constraint where - again, so long as I've understood the requirement correctly - it shouldn't:

 

sNamesAltName
an snamean saltname
an saltnamesomething else

 

The first two constraints are necessary but I've gone with a function to satisfy the robert's bullet-pointed requirement (i.e. as the third constraint). This throws the expected constraint violation when using the above bogus data.

 

Let me know your thoughts.

 

 

CREATE TABLE dbo.test
(
	sName varchar(max) NOT NULL
	, sAltName varchar(max) NOT NULL
)
GO


CREATE FUNCTION dbo.SNameInAltName (@sName AS varchar(max))
RETURNS int
AS
BEGIN
	IF EXISTS (SELECT sAltName FROM dbo.test AS [t] WITH (nolock) WHERE [t].sAltName = @sName) 
		return 1;

	return 0;
END
GO


ALTER TABLE dbo.test
	ADD CONSTRAINT sNameNotInAltNames CHECK (dbo.SNameInAltName(sName) = 0)
GO

 

 

@LainRobertson , @bake13 

Building on @LainRobertson 's ideas, this is what I have come up with (it prevents repeating something across the sName and sAltName columns, but it also prevents sName and sAltName being the same in the same record which I am trying to allow):

CREATE TABLE dbo.test
(sName nvarchar(128) NOT NULL, sAltName nvarchar(256) NOT NULL)
GO

CREATE UNIQUE NONCLUSTERED INDEX [IX_test_sName] ON [dbo].[test] ([sName] ASC)
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_test_sAltName] ON [dbo].[test] ([sAltName] ASC)
GO

CREATE FUNCTION dbo.SNameInAltName (@sName AS nvarchar(128))
RETURNS bit
AS
BEGIN
	IF EXISTS (SELECT sAltName FROM dbo.test AS [t] WITH (nolock) WHERE [t].sAltName = @sName) 
		return 1;
	return 0;
END
GO

CREATE FUNCTION dbo.SAltNameInName (@sAltName AS nvarchar(256))
RETURNS bit
AS
BEGIN
	IF EXISTS (SELECT sName FROM dbo.test AS [t] WITH (nolock) WHERE [t].sName = @sAltName) 
		return 1;
	return 0;
END
GO

ALTER TABLE dbo.test
	ADD CONSTRAINT sNameNotInAltNames CHECK (dbo.SNameInAltName(sName) = 0)
GO
ALTER TABLE dbo.test
	ADD CONSTRAINT sAltNameNotInNames CHECK (dbo.SAltNameInName(sAltName) = 0)
GO

Can someone help with the last requirement that sName = sAltName in the same record should be allowed?

 

@robertrackl 

 

Sticking with the original three constraint approach, you can just alter the existing function to accommodate this requirement. A second function isn't necessary to achieve this.

 

Here's an example for dropping the constraint, updating the function and putting it back. Just change things like the tables names to suit your environment.

 

-- Drop the existing constraint so the function can be updated.
ALTER TABLE [dbo].[test] DROP CONSTRAINT [sNameNotInAltNames]
GO

-- Alter the function.
ALTER FUNCTION [dbo].[SNameInAltName] (@sName AS varchar(max))
RETURNS int
AS
BEGIN
	IF EXISTS (SELECT sAltName FROM dbo.test AS [t] WITH (nolock) WHERE [t].sName <> @sName AND [t].sAltName = @sName) 
		return 1;

	return 0;
END
GO

-- Put the constraint back in place.
ALTER TABLE [dbo].[test]  WITH CHECK ADD  CONSTRAINT [sNameNotInAltNames] CHECK  (([dbo].[SNameInAltName]([sName])=(0)))
GO

 

best response confirmed by robertrackl (New Contributor)
Solution

@LainRobertson 

I think I've got it now. Thanks for your inspiration!

CREATE TABLE dbo.test
(sName nvarchar(128) NOT NULL, sAltName nvarchar(256) NOT NULL)
GO

CREATE UNIQUE NONCLUSTERED INDEX [IX_test_sName] ON [dbo].[test] ([sName] ASC)
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_test_sAltName] ON [dbo].[test] ([sAltName] ASC)
GO

CREATE FUNCTION dbo.bNameXAlt (@sName AS nvarchar(128), @sAltName AS nvarchar(256))
RETURNS bit
AS
BEGIN
	IF @sName = @sAltName  RETURN 0
	IF
		EXISTS (SELECT sName FROM dbo.test AS [t] WHERE [t].sName = @sAltName)
		OR
		EXISTS (SELECT sAltName FROM dbo.test AS [t] WHERE [t].sAltName = @sName)
		RETURN 1
	RETURN 0
END
GO

ALTER TABLE dbo.test
	ADD CONSTRAINT sNameNotInAltNames CHECK (dbo.bNameXAlt(sName, sAltName) = 0)
GO

It does what I wanted to achieve.

 

@robertrackl 

 

Glad to hear it, mate!

 

In the interest of efficiency, you might want to consider cutting the two queries inside the function down to one:

 

ALTER FUNCTION [dbo].[SNameInAltName] (@sName AS varchar(max), @sAltName AS varchar(max))
RETURNS int
AS
BEGIN
	IF (@sName <> @sAltName) AND EXISTS (SELECT 1 FROM dbo.test AS [t] WITH (nolock) WHERE [t].sName = @sAltName OR [t].sAltName = @sName)
		return 1;

	return 0;
END