views:

271

answers:

6

I have a table with several columns.

Sometimes some of these column fields may be empty (ie. I won't use them in some cases).

My questions:

  1. Would it be smart to set them to NULL in phpmyadmin?

  2. What does the "NULL" property actually do?

  3. Would I gain anything at all by setting them to NULL?

  4. Is it possible to use a NULL field the same way even though it is set to null?

+1  A: 

1- Would it be smart to set them to NULL in phpmyadmin?

All fields are null by default unless you specify a default value for them or insert some value for them. No need to do this...

2 -What does the "NULL" property actually do?

Null means that you have not assigned any value to it.

3- Would I gain anything at all by setting them to NULL?

As said before, all fields are null by default unless you specify a default value for them or insert some value for them. I don't think you are going to gain anything.

4- Is it possible to use a NULL field the same way even though it is set to null?

What would you gain out of a field having value of NULL? No need for this too.

Sarfraz
??????? any explanation please???
Sarfraz
blind voting down should be banned on SO i believe. There should always be an explanation so that one could be corrected. Everyone should take notice of such voting down. Thanks
Sarfraz
there are bad people around i guess
Sarfraz
Fields are not `NULL` by default, if you tell the field to be `NOT NULL` upon creating the table. If you haven't assigned a default value for this field and you try to insert a record without a value for this field, it will result in an error.
fireeyedboy
@fireeyeboy: thanks for your comment in the first place, i have not said this, but please note that when you create a field you have to tell it explicitly that it should be not null, **otherwise** it will be null by default.
Sarfraz
@Sarfraz: Fair enough. But I think you should adjust your answer to address this, just to be clear. Furthermore, the is **a lot** to be gained from working with `NULL` able fields, because they reflect an unknown value. Take for instance a time period (two datetime columns `start` and `end`). When for instance end is `NULL` this could reflect that the start of the time period is known, but the end is (still) unkown. It wouldn't make sense to have end be `0000-00-00 00:00:00` in this case, as this is an inaccurate representation of reality. End should not represent hour zero in the year zero.
fireeyedboy
RDBMS's haven't implemented `NULL` able fields for nothing you know. ;-)
fireeyedboy
@fireeyeboy: Thanks for your comment, good point though.
Sarfraz
+4  A: 

I recommend you read Problems with NULL Values.

Am
+9  A: 

The concept of the NULL value is a common source of confusion for newcomers to SQL, who often think that NULL is the same as an empty string '', or a value of zero.

This is not the case. Conceptually, NULL means "a missing unknown value" and it is treated somewhat differently from other values. For example, to test for NULL, you cannot use the arithmetic comparison operators such as =, <, or <>.

If you have columns that may contain "a missing unknown value", you have to set them to accept NULLs.

On the other hand, a table with many NULL columns may be indicating that this table needs to be refactored into smaller tables that better describe the entities they represent.

Daniel Vassallo
+2  A: 

Going to try to answer your questions all at once here.

NULL represents something along the lines of "Unknown"/"No value" or "Not applicable". So yes, if there are columns that are unused in certain circumstances, it would be appropriate to set them to NULL when not used (as no other value is appropriate).

It is possible to constrain a column to NOT NULL, meaning that the column must have a value for each row. An example would the "name" column of a "person" record. It doesn't make sense for a name to be NULL, as everybody has a name.

You can "use" a NULL column, just keep in mind you have to be careful when doing comparisons. A NULL field is never equal to another field. Check for "IS NULL" or "IS NOT NULL".

Sapph
+1  A: 

Brief answers to your questions:

  1. Yes, NULL means that the field contains nothing at all. If that's the true state of affairs, that's what the data should say. An example would be the shipped_date for an order which has not yet shipped. In this case, NULL would accurately represent the value until the order ships out, since until it does there isn't a valid time at which it did (and in this case, checking for the NULL value might be quite a valuable tool in determining which orders do still need to be shipped).

  2. NULL means that the field contains nothing. "Nothing" is different from, say, the value 0 or the string "", as these are values. NULL means roughly the same thing as "N/A" or "I decline to answer". What exactly it would mean is context dependent on the column. Of course, some columns should never be NULL, and you can enforce that with your table design.

  3. If most of the fields in a column are NULL, you should rethink exactly how you're using that column. Generally speaking, a large number of NULL values indicates you could design your tables better. As to defaulting, you can always set a nullable value to default to NULL.

  4. The same way as what? NULL is a unique value. It's not equivalent to 0, or "", or anything else like that. In a query, you must check for IS NULL or IS NOT NULL, and if a null is pulled in to a dataset, you must check for it specifically there too. Asking if a column set to NULL is equal to 0, or "", or what have you, will return false.

Todd
A: 

Now sometimes some of these column fields may be empty (ie. I wont use them in some cases).

  1. Would it be smart to set them to NULL in phpmyadmin?

Yes, that's what it's for.

  1. What does the "NULL" property actually do?

It makes the database allow NULL as a value stored in the column. "NOT NULL" means a column must have a value that is not NULL.

  1. Would I gain anything at all by setting them to NULL?

No. If your logic requires that a column never contains NULL as a value, it's better to set it to "NOT NULL". Think of it as an assertion: it is safe to assume the column value will never be NULL, so you don't have to test for it. That database takes care of that assertion.

  1. Is it possible to use a NULL field the same way even though it is set to null?

I'm not sure what you mean by that... Anyway, NULL and NOT NULL columns are identical in every way, except that NULL columns can contain NULL.

And NULL is a strange value. val = NULL is never true, even if val is NULL. For that you have to test with "IsNull()", "IS NULL" or "IS NOT NULL". See Reference Manual: Comparison Functions and Operators.

bart