views:

61

answers:

2

I have a table with two linked columns, a compulsory boolean and an optional date. There can only be a date when the boolean is FALSE. So I have this structure:

CREATE TABLE FOO (
    FOO_ID INT IDENTITY(1, 1) NOT NULL,
    MY_DATE DATETIME,
    MY_BOOLEAN BIT DEFAULT 0 NOT NULL,

    CONSTRAINT FOO_PK PRIMARY KEY (FOO_ID)
);

And I've written this constraint to keep the data integrity:

ALTER TABLE FOO
ADD CONSTRAINT FOO_CHK CHECK (
    MY_BOOLEAN=0 OR MY_DATE IS NULL
);

I must be missing something obvious but the fact is that I cannot run this update query:

UPDATE FOO
SET 
    MY_BOOLEAN=1,
    MY_DATE=NULL
WHERE FOO_ID=31416

I get a check constraint conflict error and the update fails. What am I doing wrong?


UPDATE

I regret to inform that I had mistyped a column name... Sorry for wasting your time. I wish I could remove the question :_(

+2  A: 

I've tested this with this script and get no errors. Is there any other constraint?

CREATE TABLE FOO (
    FOO_ID INT IDENTITY(1, 1) NOT NULL,
    MY_DATE DATETIME,
    MY_BOOLEAN BIT DEFAULT 0 NOT NULL,

    CONSTRAINT FOO_PK PRIMARY KEY (FOO_ID)
);
ALTER TABLE FOO
ADD CONSTRAINT FOO_CHK CHECK (
    MY_BOOLEAN=0 OR MY_DATE IS NULL
);

set identity_insert foo on
insert into foo(FOO_ID, my_date, MY_BOOLEAN)
select 31416, '20090101', 0
set identity_insert foo off

UPDATE FOO
SET 
    MY_BOOLEAN=1,
    MY_DATE=NULL
WHERE FOO_ID=31416
edosoft
+1, and validate the CHECK works: `UPDATE FOO SET MY_BOOLEAN=1, MY_DATE=getdate() WHERE FOO_ID=31416`
KM
You are right, it works fine with only these two columns... There're some other constraints but none of them affect neither the boolean nor the date, and the error message is very clear: it blames this specific constraint :-?
Álvaro G. Vicario
Please provide entire table creation script with all (check) constraints
edosoft
A: 

Sorry, bogus question (self-reply seems to be the only way to close it).

Álvaro G. Vicario