MySQL DELETE Statement

MySQL | DELETE: Learn about the MySQL DELETE Statement, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 04, 2022

DELETE Statement

From the various feature of MySQL, one of the main features is deleting a record. Deleting a record is crucial as if you filled some wrong entry or you want to delete some entry permanently then the DELETE statement of MYSQL comes to our rescue. As we do in the UPDATE statement, here also applies where the condition is an important part.

DELETE Statement Syntax

DELETE FROM table_name;
WHERE conditions;

Let us understand this more deeply by taking a demo database.

DELETE Statement in MySQL Command-Line Client

Learn how can we delete records from the table using the MySQL Command-Line Client.

Using ID in WHERE condition

Suppose we have a table named "student_details" and inside that table, we have the following columns and values:

DELETE Statement (Step 1)

And now if I want to delete the record of ID='5'; then in this case our query will be:

DELETE FROM student_details
WHERE ID='5';
DELETE Statement (Step 2)

As you can see the record of ID='5' is no more visible in the table, it is deleted.

Using any column other than ID

If I want to delete some record using the name then in this case we'll form a query like this,

DELETE FROM student_details
WHERE Student_name="KIARA";
DELETE Statement (Step 3)

Deleting all the records

In this case we'll write,

DELETE FROM table_name;

This statement will delete all the rows present in that table.

Things to keep in mind while deleting a record through Query:

  • Always check the column name, if the column name is in uppercase and you are writing it in lowercase while deleting then it will show you an error.
  • Whatever record you are deleting that record should be there in the table. For example, if your table has only 40 records and you asking to delete the value of record 41 then in that case it will show you an error. Delete query works only on the existing record.
  • It is always considered good practice when you delete the record using ID, this is because IDs are always unique; no two rows can have the same ID so deleting a record becomes easier.
  • Always write the values in ' ' inverted commas else it will throw you the error.

DELETE Statement in MySQL Workbench

Now, let us see how we'll delete an existing record in MySQL workbench.

Suppose the picture given below is our table and we want to delete the record of ID='3';

DELETE Statement (Step 4)

Step 1: Click on the specific row you want to delete, and you'll see the following options:

DELETE Statement (Step 5)

Step 3: Click on delete rows. As soon as you'll click on delete rows your entire row will be deleted.

DELETE Statement (Step 6)



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.