Jump to content

[SOLVED] NULL


asmith

Recommended Posts

i've seen a lot of times people define their table fields with NOT NULL . 

is that for security ? cause i am working with mysql , i've never define that ( i work locally), and never feel the use of it , what matters if a field stays NULL ?

security reasons ?

Link to comment
Share on other sites

I've talked about the value of NULL a few times over the years... maybe I should sticky a detailed discussion of this...

 

From a performance standpoint, yes, NULL requires an extra bit, affects JOINs, has implications for indexes, etc., so there's no need to use it when it's not necessary.  That being said, the only time you actually need it is when you need to know the difference between blank and NULL. 

 

For example, it's silly to have a NULLable gender column, since everyone has a gender, so blank (empty column) is just as good as male/female.  However, if your storing, say, how many children someone has, you need to be able to distinguish between "0" (no children) and no answer (unknown #).

 

Hope that makes sense.

 

If you ask someone a question like "how many siblings do you have?", zero is a perfectly valid answer.  However, if someone simply doesn't answer the question, the "blank" value would be zero as well.  With a NULL value, you'd have no way to differentiate between those who simply didn't complete this question (and may have 3 siblings -- i.e. an _unknown_ number) vs. those who did answer but were an only child.

 

Ok, it's like this: you need to use NULL whenever you need to be able to tell the difference between "no value specified" and "none/zero/nothing".  For example, if you were storing the answer to the question "How many brothers do you have?", and the question was optional, how would you store these two possible scenarios? You can't simply store the number, because does zero mean the question wasn't answered or the user actually has _no_ brothers?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.