Jump to content

[SOLVED] Explanation for NULL please?


Yesideez

Recommended Posts

I've just read the section in the MySQL manual on NULL and I understand not a word of it. It makes no sense and may as well be DIY instructions converted from Chinese to English by someone who only speaks Russian.

 

Please can anyone supply an example (like a roll play) where NULL would be used and how it would benefit being used?

 

I understand some silly stuff like MSB and LSB from my assembler programming days but something this simple has me completely stumped!

Link to comment
Share on other sites

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

Good golly!

 

Like fenway said, NULL means "no value specified".

If you were a row in his database and had a value of '0' for num_brothers, I'd know you had no brothers.

If you had NULL for num_brothers, you may or may not have brothers. NULL is saying we don't know the answer to that question.

Link to comment
Share on other sites

Have you ever filled out forms that look like this?

 

[] Yes

[] No

[] Not sure

 

The "not sure" is what null was designed for.

 

Looking at how it's used may help to understand it.  For example, if you sort products by price, you typically want products with a NULL price to be ignored or placed last, because NULL means "We don't know the price".

 

Or maybe you want to fetch all products where you don't know the price, so you can fix them by setting the price.  Then you would fetch all products "WHERE price IS NULL".

 

If you wanted all products with a price set, you would fetch all products "WHERE price IS NOT NULL".

 

If you wanted all products which were free, you would fetch all products "WHERE price = 0", which is different from all products which haven't had a price set yet.

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.