Alex Rivera | Logout

I set a MySQL column to "NOT NULL" but still I can insert an empty value

Asked 2011-08-31T23:40:43.013
10

In MySQL I have a table with Column1 as NOT NULL:

create table myTable 
(
    Column1 int not null,
    Column2 int not null
)

I can still insert an empty value like this:

INSERT INTO `myTable` ( `Column1` ,  `Column2` )
VALUES ( '66', '' );

How can I make the MySQL column also disallow blankstring?

Edit
Report

2 Answers

9

You're inserting an empty string, not NULL. The constraint is only against NULL values, and it would appear that your database is not coercing empty strings to NULL when it converts them to INT (which raises the additional question of why you're inserting string literals into INT columns...)

answered 2011-08-31T23:45:52.603
3

As Martin mentions, depends on your RDBMS. Oracle treats empty strings as NULLs while others do not. See this SO post.

answered 2011-08-31T23:43:33.060

Your Answer