shadow0359 shadow0359 - 16 days ago 4
MySQL Question

Does NOT NULL in mysql accept empty string

I have two tables.
One with int as primary key and other with a varchar primary key.

When an empty string is inserted ( "" ) i get programming error for int but successfully stores for varchar.
How does this work?

Is there a way to make varchar fields not accept empty strings?
I want them to give some database error which i can catch.

Answer

When a empty string is inserted ( "" ) i get programming error for int

Because a string is not an int. And there's no way to automatically convert a non-numeric string to an int.

but successfully stores for varchar

Because a string is a varchar. Columns like CHAR and VARCHAR are for storing character data, which is what a string is.

None of these values are NULL. NULL is the absence of a value. An empty string is still a value.