2

I have a table in SQL Server which has a constraint like Not null. When I enter new entry if I am not filling anything, SQL Server pops error message preventing Null value.

But once I enter one record and later if I edit that record by just deleting the data in this column, SQL Server is accepting the empty string.

3
  • Program logic should handle this, don't allow your program to delete/remove the value and replace with empty if that is not desired. Commented Apr 9, 2017 at 2:00
  • 1
    Your description does not sound correct. If the column has a NOT NULL constraint, then it is not allowed in the column. Perhaps the value is an empty string '' rather than NULL. Commented Apr 9, 2017 at 2:01
  • Optionally investigate Check constraints. CHECK constraints determine the valid values from a logical expression that is not based on data in another column. Commented Apr 9, 2017 at 2:08

2 Answers 2

5

You can use a CHECK CONSTRAINT

ALTER TABLE table
ADD CONSTRAINT chkNotEmpty CHECK (LEN(col) > 0 );
Sign up to request clarification or add additional context in comments.

Comments

1

By UI you can add constraint by following step

  • 1.open the table in Design view
    1. Right Click any column and select "Check Constraints"
    2. Edit"Expression" and set the minimum lenth of any field as len(desiredField)>=1

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.