To delete a specific row in MySQL Table, first you need to find the selection criteria by which you can select the specific row.
Then you have to verify if the selection criteria selects only the specific row.
Run the Delete SQL Query.
Usually, if the MySQL table has PRIMARY KEY column, then you can form your selection_criteria using the PRIMARY KEY column value itself. Just to make sure that you delete only one row, append the query with LIMIT 1
.
Syntax – Delete Single Row
Following is the syntax to delete a single row in a table:
DELETE FROM table_name WHERE selection_criteria LIMIT 1;
Example 1 – Delete only one Row in MySQL
Consider the following students
table.
Now we shall remove a specific row from this table. Say, we want to delete Ruma whose age is 10. Column id is the PRIMARY KEY for this table. So, choosing the corresponding id for the selection criteria would be a good choice.
Selection Criteria would be id=4.
Just to be sure, select rows from the table with this selection criteria.
SQL Query to delete just this row would be:
DELETE FROM students WHERE id=4 LIMIT 1;
Execute the query
The row has been successfully deleted.
Now let us cross verify with the table data.
Perfect! We have successfully learned to delete a single specific row in MySQL Table.