Empty vs Null

free web hosting
Free Web Hosting > Computers & Tech > Databases

Empty vs Null

suicide
I exported an Access database to csv file, and then imported the csv file to mysql.

Now I'm trying to use COUNT() on a field, but its returning the total number of records, which is not what I want. I know that COUNT() is supposed to count all records that arent Null, so I checked out my table in phpmyadmin and I realized that even though there are no values for some records they are not showing up as Null.

What is the difference between a Null and a field that is just empty? Shouldn't it be the same? Is there a way to count the records, or do I need to change those values to Null?

Reply

daf
I think you're facing with character fields, where you have an empty string that is different from a NULL value of a character field.

A NULL value represents the absence of a value for a record in a field (others softwares call it also a missing value).

An empty value is a "field-formatted" value with no significant data in it.

Why an "empty character string" is different from a null value? Cause you're comparing a string (with no characters inside) with a "cell" with no data type: they differ !

In mySQL an empty string "" isnt' a null value, in Excel a blank (character) cell is filled with an empty string ( "" ).

Reply

jackson_cn
The concept of the NULL value is a common source of confusion for newcomers to SQL, who often think that NULL is the same thing as an empty string "". This is not the case! For example, the following statements are completely different:

mysql> INSERT INTO my_table (phone) VALUES (NULL);
mysql> INSERT INTO my_table (phone) VALUES ("");

Both statements insert a value into the phone column, but the first inserts a NULL value and the second inserts an empty string. The meaning of the first can be regarded as ``phone number is not known'' and the meaning of the second can be regarded as ``she has no phone''.

In SQL, the NULL value is always false in comparison to any other value, even NULL. An expression that contains NULL always produces a NULL value unless otherwise indicated in the documentation for the operators and functions involved in the expression. All columns in the following example return NULL:

mysql> SELECT NULL,1+NULL,CONCAT('Invisible',NULL);

If you want to search for column values that are NULL, you cannot use the =NULL test. The following statement returns no rows, because expr = NULL is FALSE, for any expression:

mysql> SELECT * FROM my_table WHERE phone = NULL;

To look for NULL values, you must use the IS NULL test. The following shows how to find the NULL phone number and the empty phone number:

mysql> SELECT * FROM my_table WHERE phone IS NULL;
mysql> SELECT * FROM my_table WHERE phone = "";

Note that you can only add an index on a column that can have NULL values if you are using MySQL Version 3.23.2 or newer and are using the MyISAM or InnoDB table type. In earlier versions and with other table types, you must declare such columns NOT NULL. This also means you cannot then insert NULL into an indexed column.

When reading data with LOAD DATA INFILE, empty columns are updated with ''. If you want a NULL value in a column, you should use \N in the text file. The literal word 'NULL' may also be used under some circumstances. See section 6.4.9 LOAD DATA INFILE Syntax.

When using ORDER BY, NULL values are presented first. In versions prior to 4.0.2, if you sort in descending order using DESC, NULL values are presented last. When using GROUP BY, all NULL values are regarded as equal.

To help with NULL handling, you can use the IS NULL and IS NOT NULL operators and the IFNULL() function.

For some column types, NULL values are handled specially. If you insert NULL into the first TIMESTAMP column of a table, the current date and time is inserted. If you insert NULL into an AUTO_INCREMENT column, the next number in the sequence is inserted.

 

 

 


Reply

iGuest
Add data to a blank field only
Empty vs Null

Hi

I want to add data to a balnk field only. I have to tables. The 1 table has sum scores while the other has some scores. I want to combine both however when I do, I loose the data of the other

-question by s.Nankoo

Reply

Darasen
This question is a bit unclear nor do I have any idea what the DB actually looks like, but I will try to answer as best I can. If you wish to add data to a "blank" field you are going to need to set up a conditional statement on the applications UI or a where clause in your SQL statement. Dependent upon what exactly it is you are trying to accomplish. Keep in mind the difference between a NULL value and value that is entered as a blank string or the like. If you are creating a new row for the table then INSERT will do the trick.

Based on your description I would drop "the 1 table" altogether. A sum is a calculated value. You never want to include a calculated value in your DB design.

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. insert the null values if data is empty - 4.54 hr back. (1)
  2. excel null value - 5.02 hr back. (1)
  3. null value excel - 9.71 hr back. (1)
  4. null vs. empty string mysql - 12.13 hr back. (1)
  5. is an empty list null? - 14.77 hr back. (1)
  6. is_null vs empty() - 26.71 hr back. (1)
  7. string empty if column is null sql server - 32.10 hr back. (1)
  8. mysql empty set vs null - 38.79 hr back. (1)
  9. how to insert a null in excel - 39.62 hr back. (1)
  10. how to select null as blank mysql - 41.63 hr back. (1)
  11. mysql find empty field - 11.84 hr back. (2)
  12. null vs blank - 43.45 hr back. (2)
  13. sql count blank columns in a row - 44.19 hr back. (1)
  14. cpanel php null handling - 44.45 hr back. (1)
Similar Topics

Keywords : empty, null

  1. New MySQL Server & Blank Fields Problem With Jsp
    empty fields and data trancation error (0)


      Looking for empty, null

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for empty, null
advertisement




Empty vs Null



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE