0

I'm completely new to databases and SQL.
I have a column marked NOT NULL.
When I forget to give a value to that column when doing an INSERT, it inserts a 0 value in that column.
Is there anything I can do to make it return an error instead of changing the NULL to 0?

3
  • 1
    Do you have a default on that column? Commented Dec 3, 2010 at 14:04
  • Is it an integer column? Are you not passing a value at all, or you believe the value is empty? Commented Dec 3, 2010 at 14:04
  • @Jason McCreary Yep, it's an int. It's when I forget that column in the INSERT statement so no value gets passed to it. Commented Dec 3, 2010 at 14:07

3 Answers 3

4

You want to have a look at the SQL_MODE configuration. It allows you to define how strict MySQL handles such things. By default it is pretty lenient which is not what I usually want. It also depends on the data type, especially with Dates it is less than optimal by default.

I personally go for STRICT_ALL_TABLES usually.

See the MySQL manual (5.0) here:

http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html

Sign up to request clarification or add additional context in comments.

1 Comment

I did "SET sql_mode = 'STRICT_ALL_TABLES';" and now it returns an error message whenever I forget a NOT NULL column. Muchas Gracias.
1

There are two possible reasons for this:

  • You have a default value defined for that column
  • Your column is INT NOT NULL and casts NULL to INT, which results in 0

The second situation seems to be the case here, obviously. If you want to get an error instead, you could change the SQL_MODE to be strict. An other possibility is to do the input validation in your progam rather than leaving it to SQL.

Comments

0

I guess you have a DEFAULT value for that column

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.