Deleting all rows in a MySQL database using phpMyAdmin
We will learn the best way to delete all rows in a database using DELETE. We will explore phpMyAdmin
the command to delete rows from a given table.MySQL
DELETE
We will also learn the difference between the DELETE
, , DROP
and TRUNCATE
commands. If we have associated foreign keys in the table, we will see how to row MySQL
in DELETE
.
We are using MySQL version 8.0.27, you can download the latest version from here.
phpMyAdmin
Delete MySQL
rows from the database using
In phpMyAdmin
, you can easily delete rows in two ways. You can do it using the Graphical User Interface (GUI) option and SQL query.
Let’s learn about each of them below.
Deleting rows in SQL
a query withoutphpMyAdmin
In some cases, you must delete all rows in a table at once. Click 检查所有
and then press 删除
to do this.
If you want to delete only one row from the selected table, then click 删除
the button to delete that particular row. Refer to the image below.
If you want to delete one or more rows but not all, you can check them first and then press Delete
. You can follow the instructions by looking at the following screenshots.
If you want to delete rows from different tables, you have to go to each table and delete as above. This takes time and effort.
Here, SQL queries come into play and make the work very easy.
Use SQL
the query to delete phpMyAdmin
the rows in
There are three different ways you can use to achieve your goal based on your project requirements. Let’s understand them one by one to use them.
You might be thinking phpMyAdmin
where to write SQL
the query while using . You can write the query in SQL
under the tab (see the screenshot given below).phpMyAdmin
SQL
Press the button in the lower right corner Go
or press the Ctrl+ Enterkey to execute the query.
DROP
Is Data Definition Language
one of the commands (DDL).
This command is used to DROP
delete an entire table at once. It will delete the table's permissions, triggers, indexes, constraints, definitions, and all data.
DROP TABLE your_table_name;
DELETE
The command deletes one or more rows from a given table. It is one of the Data Manipulation Language (DML) commands.
DELETE
command WHERE
is used with the -a clause if you want to DELETE
remove a row. WHERE
The -b clause will WHERE
remove all rows from the table by omitting the -c clause.
DELETE
The delete_row command is slower than TRUNCATE
the remove_row command because it tracks each deleted row in the transaction log. DELETE
The remove_row command only deletes rows from the database, not tables.
#delete all rows from the table
DELETE FROM your_table_name;
#delete one row from the table
DELETE FROM your_table_name WHERE your_table_name.id = 1;
TRUNCATE
The command works like DELETE
the Delete command, but it is one of the Data Definition Language (DDL) commands. It is specifically used to delete all rows in a table because it does not allow the Delete WHERE
clause.
Similar to DELETE
, TRUNCATE
also keeps the table in the database but deletes the rows. Because it is recorded only once in the transaction log, it is DELETE
faster than the command.
TRUNCATE TABLE your_table_name;
What if you have a relational database where one table is related to another table using a foreign key. How do you delete it now? In this case, ON DELETE CASCADE
the option has been added.
Whenever a row is deleted in the primary table, the same row will also be deleted in the other table having a foreign key. Let us understand with the help of the sample code given below. We have created two tables named tb_student
and .tb_course
CREATE TABLE tb_student
(
id INT PRIMARY KEY,
firstname varchar(8),
lastname varchar(8),
gender varchar(8)
);
CREATE TABLE tb_course
(
course_id INT PRIMARY KEY,
course_name varchar(8),
student_id INT,
FOREIGN KEY(student_id)
REFERENCES tb_student(id)
ON DELETE CASCADE
);
Populate these tables with data. Once you are done, you can delete rows from them very easily tb_student
.
This is because whenever you delete from , the corresponding row in will also be deleted due to the option tb_student
on the foreign key .ON DELETE CASCADE
tb_course
in conclusion
We explored different ways to delete a row and all rows from a table. We also learned to delete only data and table along with its data. This tutorial also emphasized on safe deletion in case of association with another table as a foreign key.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Changing max_allowed_packet Size in MySQL Server
Publish Date:2025/04/22 Views:192 Category:MySQL
-
This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi
Zerofill usage, advantages and alternatives in MySQL
Publish Date:2025/04/22 Views:195 Category:MySQL
-
In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input
Compare only MySQL timestamp dates to date parameters
Publish Date:2025/04/22 Views:64 Category:MySQL
-
In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi
Calculating Percentages in MySQL
Publish Date:2025/04/22 Views:66 Category:MySQL
-
We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re
Selecting multiple values using WHERE in MySQL
Publish Date:2025/04/22 Views:185 Category:MySQL
-
This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi
Changing the connection timeout in MySQL
Publish Date:2025/04/22 Views:59 Category:MySQL
-
We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti
MySQL fix Data Is Truncated for a Column error
Publish Date:2025/04/22 Views:101 Category:MySQL
-
This article describes possible causes and solutions for the MySQL error Data is truncated for a column . Fix data truncated due to column error in MySQL Here, we will discuss the possible causes and solutions to eliminate MySQL data trunca
MySQL Error Server PID File Could Not Be Found Solution
Publish Date:2025/04/22 Views:192 Category:MySQL
-
In this article, we will study about the Error! Error Server PID File Could Not Be Found! in MySQL and its solution with full explanation. MySQL PID file The file that contains the process identification number or process ID of a running My
Get the last inserted ID using PHP MySQLi function
Publish Date:2025/04/22 Views:99 Category:MySQL
-
This article briefly introduces the PHP mysqli() function and demonstrates how to use it to get the last inserted ID from a MySQL database. PHP mysqli() Function It is an extended version of the MySQL driver called mysqli and is typically u