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

Vue implements the drag and drop sorting function of the page div box

vue implements the drag and drop sorting function...

Native js to implement drop-down menu

Drop-down menus are also very common in real life...

A brief discussion on the placement of script in HTML

I used to think that script could be placed anywh...

Detailed explanation of MYSQL stored procedure comments

Table of contents 1. Instructions for use 2. Prep...

Analysis of slow insert cases caused by large transactions in MySQL

【question】 The INSERT statement is one of the mos...

How to implement data persistence using the vuex third-party package

Purpose: Allow the state data managed in vuex to ...

How to install redis in docker and set password and connect

Redis is a distributed cache service. Caching is ...

Summary of some thoughts on binlog optimization in MYSQL

question Question 1: How to solve the performance...

Summary of basic knowledge points of MySql database

Table of contents Basic database operations 2) Vi...

CentOS8 installation tutorial of jdk8 / java8 (recommended)

Preface At first, I wanted to use wget to downloa...

How webpack implements static resource caching

Table of contents introduction Distinguish betwee...

Summary of 3 ways to lazy load vue-router

Not using lazy loading import Vue from 'vue&#...

HTML uses canvas to implement bullet screen function

Introduction Recently, I needed to make a barrage...