Should nullable fields in MySQL be set to NULL or NOT NULL?

Should nullable fields in MySQL be set to NULL or NOT NULL?

People who often use MySQL may encounter the following situations:

1. My field type is not null, why can I insert a null value?

2. Why is not null more efficient than null?

3. When judging whether a field is not empty, what should be used

select * from table where column <> ''

Still need to use

select * from table where column is not null

With the above questions in mind, let's briefly study the difference between null and not null, what the difference between them is and their respective efficiency issues.

First, we need to clarify the concepts of "null value" and "NULL":

1. Empty values ​​do not take up space

2. NULL in MySQL actually takes up space. The following is an official explanation from MYSQL

“NULL columns require additional space in the row to record whether their values ​​are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.”

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 do an example to test it:

CREATE TABLE `codetc` ( 
`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 `codetc` VALUES (null,1);

mysql error:

#1048 - Column 'col1' cannot be null

One more

INSERT INTO `codetc` 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.

Moreover, B-tree indexes do not store NULL values, so if the indexed field can be NULL, the efficiency of the index will drop significantly.

Let's insert a few more pieces of data into the codetc table:

INSERT INTO `codetc` VALUES ('', NULL);
INSERT INTO `codetc` VALUES ('1', '2');

Now according to the requirements, I want to count all the data in codetc 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 `codetc` WHERE col1 IS NOT NULL;

SELECT * FROM `codetc` WHERE col1 <> '';

As you can see, the results are quite different, so in actual situations, we must figure out whether we need to use null or not null based on business needs.

Note: MySQL fields try to avoid NULL, you should specify the column as NOT NULL unless you want to store NULL. In MySQL, columns containing null values ​​are difficult to query and NULL values ​​are not stored when indexing a table. Therefore, if the indexed field can be NULL, the efficiency of the index will drop significantly. Because they make indexes, index statistics, and comparison operations more complex. You should replace null values ​​with 0, a special value, or an empty string.

mysql set field not null to null

Statement:

ALTER TABLE table name MODIFY field name VARCHAR(20) DEFAULT NULL 

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Why should MySQL fields use NOT NULL?
  • Solve the problem of MySQL using not in to include null values
  • Solve the problem of not finding NULL from set operation to mysql not like
  • Detailed explanation of the difference between MySQL null and not null and null and empty value''''''''
  • Detailed explanation of the usage of NULL and NOT NULL when creating tables in MySQL
  • Solution to the problem of null column in NOT IN filling pit in MySQL
  • MySQL query empty fields or non-empty fields (is null and not null)
  • mysql not in, left join, IS NULL, NOT EXISTS efficiency problem record
  • MySQL not null constraint case explanation

<<:  WeChat applet to obtain mobile phone number step record

>>:  How to install centOS8 in VMware12 (tutorial on installing centos8 in vm virtual machine)

Recommend

HTML left, center, right adaptive layout (using calc css expression)

In the latest HTML standard, there is a calc CSS e...

Steps to set up and mount shared folders on Windows host and Docker container

Programs in Docker containers often need to acces...

Implementing a simple student information management system based on VUE

Table of contents 1. Main functions 2. Implementa...

Detailed explanation of three ways to import CSS files

There are three ways to introduce CSS: inline sty...

Comparative Analysis of MySQL Binlog Log Processing Tools

Table of contents Canal Maxwell Databus Alibaba C...

How to deal with time zone issues in Docker

background When I was using Docker these two days...

JavaScript to implement simple tab bar switching content bar

This article shares the specific code of JavaScri...

Linux checkup, understand your Linux status (network IO, disk, CPU, memory)

Table of contents 1. Core commands 2. Common comm...

Implementation of vertical centering with unknown height in CSS

This article mainly introduces the implementation...

Introduction to Linux system swap space

Swap space is a common aspect of computing today,...