Updating violation

I have a table with two related columns, a required logical and optional date. There can only be a date when the boolean value 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 wrote this restriction to maintain data integrity:

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

I have to miss something obvious, but the fact is that I cannot run this update request:

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

I get a validation constraint conflict error and the update does not work. What am I doing wrong?


UPDATE

I regret to inform you that I was mistaken in the column name ... Sorry for wasting time. I would like to remove the question: _ (

+3
source share
2 answers

, (- ).

0

script . ?

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
+2

Source: https://habr.com/ru/post/1733191/


All Articles