0

I have a table with column BirthDate(date, null).

I tried to add check constraint in designer BirthDate >= 'January 1, 1980' but got error:

Unable to add constraint 'CK_MyTable'.
The ALTER TABLE statement conflicted with the CHECK constraint "CK_MyTable". The conflict occurred in database "MyDb", table "dbo.MyTable", column 'BirthDate'.

When I tried to add the constraint with this T-SQL script:

ALTER TABLE dbo.MyTable 
ADD CONSTRAINT BirthDate CHECK (BirthDate >= 'January 1, 1900');

I got the same error.

What's wrong?

3
  • 3
    Do you have any existing rows in the table that break that constraint? If so; you have to Update or Delete them and try again. Commented Aug 26, 2015 at 8:04
  • @tobypls yeah i have a rows in table. How can i modifity script to add constratint in this case? Commented Aug 26, 2015 at 8:07
  • I posted an answer. Try one of the stated methods. Commented Aug 26, 2015 at 8:14

2 Answers 2

2

You can't have existing rows in the table that violate the constraint. You can solve this by either

  1. Delete them

    DELETE FROM dbo.MyTable WHERE BirthDate < '1980-01-01'
    

    OR

  2. Update the existing rows

    UPDATE dbo.MyTable SET BirthDate = '1980-01-01'/*Your new date*/ 
    WHERE BirthDate < '1980-01-01'
    
Sign up to request clarification or add additional context in comments.

Comments

0

Try like this

ALTER TABLE dbo.MyTable 
ADD CONSTRAINT BirthDate WITH NOCHECK CHECK (BirthDate >= 'January 1, 1900');

We do not recommend doing this, except in rare cases.

Unsure of WITH CHECK clause when adding a constraint

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.