Summary of knowledge points about null in MySQL database

Summary of knowledge points about null in MySQL database

In the MySQL database, null is a common situation. What are the precautions regarding null in MySQL? The following is a brief summary, which will be supplemented later.

1. is null

First, determine whether the value of a column in the database is null. You cannot use equals to determine whether it is null. You must use is. For example, select * from users where user_name is null or select * from users where user_name is not null, but not select * from users where user_name = null

2. ISNULL()

The ISNULL() function is built into the MySQL database and its usage is the same as other built-in functions such as sum() provided in MySQL. For example, select ISNULL(user_name) from users where user_name = 'Demrystv' returns 0; select ISNULL(NULL) returns 1

3. IFNULL()

The MySQL database has a built-in IFNULL() function, and its usage is the same as other built-in functions such as sum() provided in MySQL. It mainly receives two parameters. The first parameter is the field or value to be judged as null, and the second field is another value to be returned when the first parameter is null. That is, if the first field is null, it will be replaced with another value. For example, select IFNULL(NULL, "java is the best language of the world"), since the value of the first parameter is NULL, the value of the second parameter, java is the best language of the world, will be output; similarly, if the first field is not empty, the value of the first field will be returned.

4. insert into and null

When using insert into to fill data into a table, you need to first make sure whether the table is null or empty. If it is null, you cannot use insert into to fill data, and you must use update. This seems simple, but it is often overlooked in actual development, so special attention needs to be paid.

example:

Create a test table, colA cannot store null values, but colB can store null values.

CREATE TABLE `test` (
`colA` varchar(255) NOT NULL,
`colB` varchar(255) DEFAULT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

The above are all the relevant knowledge points introduced this time. Thank you for your learning and support for 123WORDPRESS.COM.

You may also be interested in:
  • PHP basics: connecting to MySQL database and querying data
  • Basic tutorial on connecting and operating MySQL database with PHP
  • MySQL Basic Database Creation
  • MySQL 8.0.15 installation graphic tutorial and database basics
  • MySQL database basic commands (collection)
  • Very comprehensive Mysql database, database table, data basic operation notes (including code)
  • MySQL database basic notes
  • Detailed explanation of real-time backup knowledge points of MySQL database
  • Detailed explanation of nodejs connecting to mysql database and basic knowledge points
  • Summary of basic knowledge and operations of MySQL database

<<:  Tutorial on building svn server with docker

>>:  js realizes horizontal and vertical sliders

Recommend

How to optimize MySQL query speed

In the previous chapters, we introduced how to ch...

Implementation of CSS Fantastic Border Animation Effect

Today I was browsing the blog site - shoptalkshow...

A brief discussion on the difference between src and href in HTML

Simply put, src means "I want to load this r...

Summary of methods to clear cache in Linux system

1) Introduction to cache mechanism In the Linux s...

How to display the border when td is empty

Previously, I summarized how to use CSS to achieve...

Detailed explanation of Nginx version smooth upgrade solution

Table of contents background: Nginx smooth upgrad...

Native JS to implement real-time clock

Share a real-time clock effect implemented with n...

How to Delete Junk Files in Linux Elegantly

I wonder if you are like me, a programmer who arr...

Getting Started Guide to Converting Vue to React

Table of contents design Component Communication ...

MySQL Series 8 MySQL Server Variables

Tutorial Series MySQL series: Basic concepts of M...

JavaScript to achieve balance digital scrolling effect

Table of contents 1. Implementation Background 2....