Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Check Constraint Works on Inserts, but Not on Updates Expand / Collapse
Author
Message
Posted Tuesday, October 30, 2012 11:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 01, 2012 8:11 AM
Points: 3, Visits: 4
I created a UDF to check for more than one instances of a value 1 in a bit column corresponding to a given ID in a table.

CREATE function [dbo].[udfDefault4Scheduling] (@intNPIID int)
returns bit
as
begin

declare @bit bit

set @bit = (select case when count(NPIID) <= 1 then 1 else 0 end from NPIOrgAddressAvailability where NPIID = @intNPIID and Default4Scheduling = 1)

return @bit

end

I added a check constraint on the table in question (NPIOrgAddressAvailability) requiring the function to return a value of 1 for a given ID. In other words, the table can have multiple repeated values for NPIID; but, for a given NPIID, only one row can have a value of 1 for Default4Scheduling.

ALTER TABLE [dbo].[NPIOrgAddressAvailability] WITH CHECK ADD CONSTRAINT [CK_NPIOrgAddressAvailability] CHECK (([dbo].[udfDefault4Scheduling]([NPIID])=(1)))
GO

ALTER TABLE [dbo].[NPIOrgAddressAvailability] CHECK CONSTRAINT [CK_NPIOrgAddressAvailability]
GO

Whenever I try to insert a new record in violation of this constraint, I get an error as expected. However, when I update the table in violation of this constraint, it allows me to do so as if the constraint doesn't even exist.

I checked in SSMS and "Enforce for Inserts and Updates" is set to "Yes".

A Google search suggests several others have had this issue; but, I haven't seen a resolution. Any ideas?

Thanks,

Mike
Post #1378900
Posted Tuesday, October 30, 2012 11:32 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:34 PM
Points: 15,442, Visits: 9,588
How about skipping the check constraint and using a Unique, Filtered index?

CREATE UNIQUE INDEX UFI_NPIOrgAddressAvailability_NPIID_Default4Scheduling ON dbo.NPIOrgAddressAvailability (NPIID)
WHERE Default4Scheduling = 1;

Name the index per your local policy, of course. I just gave it a sort of default name.

Assuming you're using SQL 2008 (as per the forum you posted in), that should be an option. Try it and see if it does what you need.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1378919
Posted Tuesday, October 30, 2012 12:17 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 01, 2012 8:11 AM
Points: 3, Visits: 4
Thanks G^2.

I forgot that I could use filtered indexes in SQL 08. Still. I'm puzzled as to why the constraint behaves this way.

Thanks again,

Mike.
Post #1378949
Posted Tuesday, October 30, 2012 12:29 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:34 PM
Points: 15,442, Visits: 9,588
Probably because, at the time the constraint is fired, the updated value hasn't been committed yet, so there's only one row with the live data, committed, and thus it doesn't violate the constraint.

But I'm kind of guessing on that. I avoid UDFs in constraints if I can help it. I've found they create more complexity than they solve. Plus, if they have to access row data, I've seen them do all kinds of nasty things to lock escalation.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1378954
Posted Wednesday, October 31, 2012 7:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 01, 2012 8:11 AM
Points: 3, Visits: 4
Thanks.

I went down this same road about 8 weeks ago - trying to create a check constraint using a UDF, finding it works for inserts but not updates, then finding that SQL 08 allows for filtered indexes and using that instead. Somehow, I forgot all about that when the same situation arose again.

I'll keep in mind your advise regarding avoidance of UDFs for constraints.

One more thing - do you know if MySQL allows for filtered indexes? I don't think it does based on my searching.

Thanks again.
Post #1379293
Posted Thursday, November 01, 2012 6:13 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:34 PM
Points: 15,442, Visits: 9,588
No clue on MySQL. I installed a copy of it once, in 2002, played around with it for about an hour, and haven't touched it since. Not against it in any way, just haven't had the need.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1379770
Posted Friday, November 02, 2012 5:20 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 4:53 PM
Points: 1,746, Visits: 2,553
mmallkc (10/30/2012)
Whenever I try to insert a new record in violation of this constraint, I get an error as expected. However, when I update the table in violation of this constraint, it allows me to do so as if the constraint doesn't even exist.

I checked in SSMS and "Enforce for Inserts and Updates" is set to "Yes".

A Google search suggests several others have had this issue; but, I haven't seen a resolution. Any ideas?

Mike



Without testing it yet, I agree with GSquared that the CHECK occurs before the row is actually UPDATEd. In fact, I'm not sure why then the INSERT CHECK is catching the dup (is it possibly something else causing the INSERT to fail??).

I'm taking it that NPIID is not a unique id itself right? If it were, the check would always pass, right?


SQL DBA,SQL Server MVP('07, '08, '09)
I'm not fat, I'm gravity challenged.
Post #1380637
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse