Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 11th, 2006, 01:41 AM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Location: Melbourne, VIC, Australia.
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default CHECK Constraint Error

Hello everyone,

I noticed a strange (lack of) error with a check constraint I had on a table in my database and wondered if anybody else has seen this.

Create a simple table as:-
Code:
CREATE TABLE t_batch (
    dt_allocated DATETIME NULL,
    dt_created   DATETIME NOT NULL
)
ALTER TABLE t_batch ADD CONSTRAINT ck_bth_allocated CHECK(dt_allocated >= dt_created)
GO
Now, I would expect that if I INSERT a new row with a NULL value for dt_allocated that the check constraint would complain and stop the INSERT.

Try this:-
Code:
INSERT INTO t_batch
    (dt_allocated, dt_created)
    VALUES(NULL, GETDATE())
On my setup it works just fine, but if you run the SELECT below it returns no rows.
Code:
SELECT *
    FROM t_batch
    WHERE dt_allocated >= dt_created
Now I thought this was weird so I check to ensure the check constraint works at all.

Code:
INSERT INTO t_batch
    (dt_allocated, dt_created)
    VALUES(DATEADD(d, -1, GETDATE()), GETDATE())
This INSERT failed with a check constraint violation.

My @@OPTIONS variable returns 5496 (decimal), 1578 (hex).
My @@VERSION variable returns 'Microsoft SQL Server 2000 - 8.00.2040 (Intel X86) May 13 2005 18:33:17 Copyright (c) 1988-2003 Microsoft Corporation Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 2) '

Basically, I've installed service pack 4 with the AWE hotfix.

Has anybody seen this, knows of a patch?

Kep.
__________________
Kep.
Reply With Quote
  #2 (permalink)  
Old April 11th, 2006, 05:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

No patch is necessary - welcome to 3 value logic. :D

A Check Constraint prevents a row from being inserted if the value of the constraint does not evaluate to FALSE. Note that because of 3 value logic, this is not the same as evaluating to TRUE.

Because of the NULL value of one of your operands, your constraint expression evaluates to UNKNOWN. Since this is not FALSE, the insert proceeds.

Modify your constraint to be something like:

CHECK(COALESCE(dt_allocated, '12/31/9999') >= dt_created)

to remove the NULL from the CHECK expression to reduce it to 2 value logic.


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #3 (permalink)  
Old April 11th, 2006, 06:19 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Location: Melbourne, VIC, Australia.
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Interesting. I see what you are saying. I would expect it to be "As long as it evaluates to TRUE" rather than "As long as it doesn't evalute to FALSE".

I looked in BOL and found this...
Quote:
quote:
A CHECK constraint specifies a Boolean (evaluates to TRUE or FALSE) search condition that is applied to all values entered for the column; all values that do not evaluate to TRUE are rejected.
Given the above quote I would suggest this is a bug.
Kep.


Kep.
Reply With Quote
  #4 (permalink)  
Old April 11th, 2006, 06:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

In contrast, the SQL Server 2005 BOL states:

Quote:
quote:
"CHECK constraints reject values that evaluate to FALSE. Because null values evaluate to UNKNOWN, their presence in expressions may override a constraint. For example, suppose you place a constraint on an int column MyColumn specifying that MyColumn can contain only the value 10 (MyColumn = 10). If you insert the value NULL into MyColumn, the SQL Server 2005 Database Engine inserts NULL and does not return an error.

A CHECK constraint returns TRUE when the condition it is checking is not FALSE for any row in the table."
Also, Inside SQL Server 2000 (my SQL Server bible) states:
Quote:
quote:
"CHECK constraints allow you to define an expression for a table that must not evaluate to FALSE for a data modification to succeed. [...] The constraint will allow the row if it evaluates to TRUE or to unknown. The constraint evaluates to unknown when NULL values are present ..."
With all due respect, the only bug here seems to be in the SQL Server BOL quote you gave. The quote indicates that the constraint expression is a boolean which evaluates to (only) TRUE or FALSE. In fact, boolean expressions can evaluate to three values: TRUE, FALSE, or unknown.

This may be why it looks like the SQL Server 2005 BOL clarifies the three value logic a bit more, and apparently corrects the misleading statement that there are only two values for a boolean.

NULLs trip up people all the time in similar situations - that's why it's best if you can avoid them in your database design.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #5 (permalink)  
Old April 11th, 2006, 07:32 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Location: Melbourne, VIC, Australia.
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Fair enough. I did consider that BOL maybe wasn't defining clearly enough.

Can you supply the URL for the quote (maybe I need to update my BOL).

I got my quote from mk:@MSITStore:C:\Program%20Files\Mic...ar_da_0777.htm


Kep.
Reply With Quote
  #6 (permalink)  
Old April 11th, 2006, 08:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

The URL I have for the quote in SQL Server 2005 BOL is:

http://ms-help://MS.SQLCC.v9/MS.SQLS...4178cc09b8.htm

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I have a constraint like this.....? callagga SQL Language 7 August 13th, 2008 01:08 AM
You have an error in your SQL syntax; check the ma JimZippy BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 December 5th, 2007 12:34 PM
Error adding a check constraint SQLScott SQL Server 2005 2 October 3rd, 2007 05:58 AM
check box data insert error mateenmohd Classic ASP Basics 3 May 28th, 2007 05:29 AM
Check box Error surendran BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 July 25th, 2004 11:36 PM



All times are GMT -4. The time now is 12:47 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.