Difference between NULL and MySQL empty strings _ MySQL

Source: Internet
Author: User
Difference between NULL and MySQL NULL strings bitsCN.com

Difference between NULL and MySQL NULL strings

We often see MySQL NULL strings and NULL values, but they are not a concept. The following describes the differences between NULL and MySQL NULL strings for your reference.

For SQL beginners, the concept of NULL value is often confusing. They often think that NULL is the same as the MySQL NULL string. This is not the case. For example, the following statements are completely different:

The following is a code snippet:

MySQL> insert into my_table (phone) VALUES (NULL );

Mysql> insert into my_table (phone) VALUES ('');

Both statements insert values into the phone column, but 1st statements insert NULL values and 2nd statements insert NULL strings. The meaning of 1st cases can be interpreted as "the phone number is unknown", and the meaning of 2nd cases can be interpreted as "the person has no phone number, so there is no phone number ".

To perform NULL processing, the is null and is not null operators and IFNULL () functions can be used.

In SQL, comparing NULL values with any other values (even NULL) is never "true ". Expressions that contain NULL always export NULL values, unless otherwise specified in the document on operators and functions of expressions. All columns in the following example return NULL: mysql> select null, 1 + NULL, CONCAT ('invisable', NULL );

If you want to search for a column with a NULL value, you cannot use expr = NULL for testing. The following statement does not return any rows because expr = NULL is never "true" for any expression: mysql> SELECT * FROM my_table WHERE phone = NULL;

To search for a NULL value, the is null test must be used. The following statement describes how to find a NULL phone number and a blank phone number:

The following is a code snippet:

Mysql> SELECT * FROM my_table WHERE phone is null;

Mysql> SELECT * FROM my_table WHERE phone = '';

For more information and examples:

If you are using the MyISAM, InnoDB, BDB, or MEMORY storage engine, you can add one index to a column that may have NULL values. Otherwise, the index column must be declared as not null and NULL cannot be inserted into the column.

When reading DATA using load data infile, null or lost columns are updated. If you want to have a NULL value in the column, use/N in the data file. In some cases, you can also use the literal word "NULL ".

When DISTINCT, group by, or order by is used, all NULL values are considered equal.

When order by is used, the NULL value is first displayed. if DESC is specified in descending ORDER, the NULL value is finally displayed.

For aggregate (accumulative) functions, such as COUNT (), MIN (), and SUM (), NULL values are ignored. The exception is COUNT (*), which counts rows instead of individual column values. For example, the following statement generates two counts. COUNT the number of rows in the table, and COUNT the number of non-NULL values in the age column: mysql> select count (*), COUNT (age) FROM person;

For some column types, MySQL will perform special processing on the NULL value. If NULL is inserted into the TIMESTAMP column, the current date and time are inserted. If NULL is inserted into an integer column with the AUTO_INCREMENT attribute, the next number in the sequence is inserted.

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.