Jump to content


Null? Whats that mean?

  • Please log in to reply
6 replies to this topic

#1 jwk811

  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 25 October 2006 - 08:07 PM

Whats does the null thing do in a database.. everything ive done so far it just needed to be "not null" i was just wondering what the difference was  ;D thanks

#2 wildteen88

  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 October 2006 - 08:10 PM

null means empty/no value/nothing

#3 Perad

  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 25 October 2006 - 08:10 PM

Null means no value. Its used for optional database values. For example your profile contains many optional values which are null till you put something in them. Things like password and e-mail are NOT NULL

#4 jwk811

  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 25 October 2006 - 08:29 PM

ok but i still dont understand why or when that would be used... optional values?

#5 croix

  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 25 October 2006 - 08:33 PM

you would allow null values for fields that the user does not HAVE to fill in...

I.E. when filling out a form, there are often "required" fields, which would not allow a null value, and fields which are optional for the user to fill out, which will allow null values

#6 Psycho

  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 25 October 2006 - 10:40 PM

But, be careful here. With repsect to programming, different languages treat NULL differently.

NULL does not equal no value. In a true sense NULL means that there never was a value.

Here's an example. Suppose you had a form with two fields where a person could enter their first name and last name. Now, let's suppose the user enters a firstname, but not a lastname. If you used this code to enter the data:

$sql = "INSERT INTO 'table' (fname, lname) VALUES(".$_POST[fname].", ".$_POST[lname].")";

Then the value for lastname would NOT be null. It would have a value of an empty string. However if you used this code:

$fields .= "fname";
$values .= $_POST[fname];
if ($_POST[lname] != "") {
  $fields .= ", lname";
  $values .= ", " . $_POST[lname];

if ($_POST[lname]) {}
$sql = "INSERT INTO 'table' (".$fields.") VALUES(".$values.")";

Then there would be NO value inserted into the last name field. It doesn't have a value because it never existed. It's a very subtle, but important difference.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#7 btherl

  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 26 October 2006 - 04:15 AM

A critical difference between php's null and sql's null is that sql's null is not equal to itself.

$a = null; $b = null; if ($a === $b) print "null === null\n";

This will show that php null is equal to php null.  But

SELECT null = null;

in SQL will give you the result "false".  SQL's null is never equal to itself, because it represents "unknown value".

That's why you have to use "IS null" or "IS NOT null" to test for nullness in SQL, instead of using "= null".

However, people very often use null in sql to mean "no value" as well as "unknown value".  So whichever meaning you are using it for, you need to use the appropriate conditions.  Typically you'll use "IS null" when null means "no value", but you'll check for equality when it means "unknown value".

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users