MySQL

MySQL DELETE

DELETE command is used to delete rows from a database table. DELETE command can delete multiple rows from a table in a single query. Once a row has been deleted, it cannot be recovered. Therefore, it is highly recommended to make database backups before deleting data from the database. This can allow you to restore the database and view the data later if necessary.
 

Syntax of the DELETE command

The basic syntax of the DELETE command is as follows.

DELETE FROM tableName [WHERE condition];

If the clause WHERE is not used in the DELETE query, all rows will be deleted.
 

 

Example:

Consider the following table “Customers” with four columns “ID”, “Name”, “Age” and “Address”.

+----------+-----------+--------+-----------------------+
|    ID    |    Name   |  Age   |     Address           |
+----------+-----------+--------+-----------------------+
|    101   |   Alex    |   25   | San Francisco         |
|    102   |   Emily   |   15   | San Francisco         |
|    103   |   Jean    |   35   | Boston                |
|    104   |   Bob     |   40   | Boston                |
+----------+-----------+--------+-----------------------+

 
To remove Alex from “Customers” table, we can use the following query:

DELETE FROM Customers WHERE ID = 101;

 
Now we can check if Alex has been deleted from the “Customers” table.

> SELECT * FROM Customers;

+----------+-----------+--------+-----------------------+
|    ID    |    Name   |  Age   |     Address           |
+----------+-----------+--------+-----------------------+
|    101   |   Alex    |   25   | San Francisco         |
|    102   |   Emily   |   15   | San Francisco         |
|    103   |   Jean    |   35   | Boston                |
|    104   |   Bob     |   40   | Boston                |
+----------+-----------+--------+-----------------------+
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *