JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Deleting all rows in a MySQL database using phpMyAdmin

Author:JIYIK Last Updated:2025/04/22 Views:

We will learn the best way to delete all rows in a database using DELETE. We will explore phpMyAdminthe command to delete rows from a given table.MySQLDELETE

We will also learn the difference between the DELETE, , DROPand TRUNCATEcommands. If we have associated foreign keys in the table, we will see how to row MySQLin DELETE.

We are using MySQL version 8.0.27, you can download the latest version from here.


phpMyAdminDelete MySQLrows 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 SQLa 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 SQLthe query to delete phpMyAdminthe 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 phpMyAdminwhere to write SQLthe query while using . You can write the query in SQLunder the tab (see the screenshot given below).phpMyAdminSQL

Press the button in the lower right corner Goor press the Ctrl+ Enterkey to execute the query.

DROPIs Data Definition Languageone of the commands (DDL).

This command is used to DROPdelete an entire table at once. It will delete the table's permissions, triggers, indexes, constraints, definitions, and all data.

DROP TABLE your_table_name;

DELETEThe command deletes one or more rows from a given table. It is one of the Data Manipulation Language (DML) commands.

DELETEcommand WHEREis used with the -a clause if you want to DELETEremove a row. WHEREThe -b clause will WHEREremove all rows from the table by omitting the -c clause.

DELETEThe delete_row command is slower than TRUNCATEthe remove_row command because it tracks each deleted row in the transaction log. DELETEThe 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;

TRUNCATEThe command works like DELETEthe 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 WHEREclause.

Similar to DELETE, TRUNCATEalso keeps the table in the database but deletes the rows. Because it is recorded only once in the transaction log, it is DELETEfaster 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 CASCADEthe 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_studentand .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_studenton the foreign key .ON DELETE CASCADEtb_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.

Article URL:

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

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial