Home > Database > Mysql Tutorial > body text

How to delete empty data in mysql

WBOY
Release: 2022-02-24 12:04:10
Original
10349 people have browsed it

In mysql, you can use the delete statement with "NULL" to delete empty data. This statement is used to delete data records in the table. "NULL" is used to indicate that the data is empty. The syntax is "delete from table Name where field name = ' ' OR field name IS NULL;".

How to delete empty data in mysql

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

How to delete empty data in mysql

Use the delete command to delete blank rows in MySQL.

The syntax is as follows

delete from yourTableName where yourColumnName=' ' OR yourColumnName IS NULL;
Copy after login

The above syntax will delete blank lines and NULL lines.

To understand this concept, let us create a table. The query to create the table is as follows

mysql> create table deleteRowDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(20)
   -> );
Copy after login

Use the insert command to insert some records into the table.

The query is as follows

mysql> insert into deleteRowDemo(StudentName) values('John');
mysql> insert into deleteRowDemo(StudentName) values('');
mysql> insert into deleteRowDemo(StudentName) values('');
mysql> insert into deleteRowDemo(StudentName) values(NULL);
mysql> insert into deleteRowDemo(StudentName) values('Carol');
mysql> insert into deleteRowDemo(StudentName) values('Bob');
mysql> insert into deleteRowDemo(StudentName) values('');
mysql> insert into deleteRowDemo(StudentName) values('David');
Copy after login

Use the select statement to display all records in the table.

The query is as follows

mysql> select *from deleteRowDemo;
Copy after login
Copy after login

The following is the output

+----+-------------+
| Id | StudentName |
+----+-------------+
| 1  | John        |
| 2  |             |
| 3  |             |
| 4  | NULL        |
| 5  | Carol       |
| 6  | Bob         |
| 7  |             |
| 8  | David       |
+----+-------------+
8 rows in set (0.00 sec)
Copy after login

This is the query to delete blank rows as well as NULL

mysql> delete from deleteRowDemo where StudentName='' OR StudentName IS NULL;
Copy after login

Now, let us check the table records again.

The query is as follows

mysql> select *from deleteRowDemo;
Copy after login
Copy after login

The following is the output

+----+-------------+
| Id | StudentName |
+----+-------------+
| 1  | John        |
| 5  | Carol       |
| 6  | Bob         |
| 8  | David       |
+----+-------------+
4 rows in set (0.00 sec)
Copy after login

Recommended learning:mysql video tutorial

The above is the detailed content of How to delete empty data in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template