I believe that many people who have used MySQL for a long time are still not very clear about the concepts of these two field attributes, and generally have the following questions:
With the above questions in mind, let's take a closer look at the differences between null and not null.
For example, if you have a cup, a null value means that the cup is vacuum, and NULL means that the cup is full of air. Although the cups look empty, the difference is huge. After clarifying the concepts of "null value" and "NULL", the problem is basically clear. Let's make an example to test it: CREATE TABLE `test` ( `col1` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , `col2` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ) ENGINE = MYISAM ; Insert data: INSERT INTO `test` VALUES (null,1); mysql error:
One more INSERT INTO `test` VALUES ('',1); Inserted successfully. It can be seen that "NULL" cannot be inserted into the NOT NULL field, only "null value" can be inserted. The answer to question 1 above is also available. Regarding question 2, we have already said above that NULL is not actually a null value, but it takes up space. Therefore, when MySQL makes comparisons, NULL will participate in the field comparison, which has some impact on efficiency. Let's insert some more data into the test table: INSERT INTO `test` VALUES ('', NULL); INSERT INTO `test` VALUES ('1', '2'); Now the data in the table: Now according to the requirements, I want to count all the data in the test table where col1 is not empty. Should I use "<> ''" or "IS NOT NULL"? Let's take a look at the difference in the results. SELECT * FROM `test` WHERE col1 IS NOT NULL SELECT * FROM `test` WHERE col1 <> '' As you can see, the results are quite different, so we must figure out what search criteria to use based on business needs. MYSQL recommends that column attributes be NOT NULL as much as possible Length verification: Note that there is no space between the empty values''. mysql> select length(''),length(null),length(' '); +------------+--------------+--------------+ | length('') | length(null) | length(' ') | +------------+--------------+--------------+ | 0 | NULL | 2 | +------------+--------------+--------------+ Note:
This concludes this article on the differences between MySQL null and not null and between null and empty value ''''. For more information about MySQL null and not null, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Vue implements calling PC camera to take photos in real time
>>: Analysis of the operating principle and implementation process of Docker Hub
Table of contents 1. Schematic diagram of group q...
This article mainly explains how to deploy Elasti...
Common points: The DIV tag and SPAN tag treat som...
It’s National Day, and everyone is eager to celeb...
It is really not easy to do a good reconstruction...
This article example shares the specific code of ...
Table of contents Download tf-gpu Build your own ...
1. Framework A browser document window can only d...
<br />I have summarized the annotation writi...
vue-element-admin import component encapsulation ...
In rows, dark border colors can be defined indivi...
<br />Previous article: Web Design Tutorial ...
Error: Connection to blog0@localhost failed. [080...
Use JS to zoom in and out when the mouse is on th...
This article uses an example to describe the MySQ...