MySQL, better to insert NULL or empty string?

By using NULL you can distinguish between "put no data" and "put empty data.

By using NULL you can distinguish between "put no data" and "put empty data". Some more differences: A LENGTH of NULL is NULL, a LENGTH of an empty string is 0. NULLs are sorted before the empty strings.

COUNT(message) will count empty strings but not NULLs You can search for an empty string using a bound variable but not for a NULL. This query: SELECT * FROM mytable WHERE mytext =? Will never match a NULL in mytext, whatever value you pass from the URL1 match NULLs, you'll have to use other query: SELECT * FROM mytable WHERE mytext IS NULL.

0 or NULL or "" – Atul Dravid Oct 16 '10 at 23:48 @aadravid: no difference. – Quassnoi Oct 17 '10 at 10:20.

One thing to consider, if you ever plan on switching databases, is that Oracle does not support empty strings. They are converted to NULL automatically and you can't query for them using clauses like WHERE somefield = '' .

4 This sounded incredibly fishy to me, even on your link, so I tried it. Null field, set to '', oracle ignores it. Reports length as null rather than 0.

That's just so wrong. There's got to be some way around this. Think I'll post this as another question.

– Steve B. Aug 12 '09 at 19:10 Steve B. : see this question: stackoverflow.Com/questions/1171196/… – Quassnoi Aug 12 '09 at 19:16 Thanks for the reference, though I still don't understand the reasoning.

Posted as stackoverflow. Com/questions/1268177/… – Steve B. Aug 12 '09 at 19:24.

I don't know what best practice would be here, but I would generally err in favor of the null unless you want null to mean something different from empty-string, and the user's input matches your empty-string definition. Note that I'm saying YOU need to define how you want them to be different. Sometimes it makes sense to have them different, sometimes it doesn't.

If not, just pick one and stick with it. Like I said, I tend to favor the NULL most of the time. Oh, and bear in mind that if the column is null, the record is less likely to appear in practically any query that selects (has a where clause, in SQL terms) based off of that column, unless the selection is for a null column of course.

...And now that I see the answer above me, I think it's safe to say that the usual differentiation you would care about is no data versus empty data. :-) – Platinum Azure Aug 12 '09 at 18:54.

One thing to keep in mind is that NULL might make your codepaths much more difficult. In pytohn for example most database adapters / ORMs map NULL to None. So things like: print " %(title)s %(firstname) %(lastname)!" % databaserow might result in " None Joe Doe!

" To avoid it you need something like this code: if databaserow. Title: print " %(title)s %(firstname) %(lastname)!" % databaserow else: print " %(firstname) %(lastname)! " % databaserow Which can make things much more complex.

3 In my opinion abusing your database to "fix" errors in your code or the framework is a (very) bad coding practice. When there is no data you should just insert NULL and be consistent in using that. Otherwise you must use statements like: if(myString == null || myString = "").

When an object is not set or defined in your code you are also using NULL instead of some kind of "placeholder" (which an empty string is in my opinion). – Gertjan Aug 12 '09 at 19:39 1 Depends very much on your Language of choice. In Python "if not myString:" tests for None and "".

Probably mainly a cultural issues. The Java Guys "bad practice" is the dynamic person's elegance. – mdorseif Oct 21 '10 at 8:54.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions