DELETE statement in MySQL




Hello and welcome everyone. In this article, we will learn about the DELETE statement in MySQL. Let’s go.

MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/

The MySQL DELETE command is used to delete rows data that is not important or you want to remove from table. You can remove more than one row at a time using DELETE command. You can use DELETE command with WHERE clause. When large number of rows that you want to remove then you can use DELETE command and it’s an advantage. Before deleting rows data take a backup for deleting data, because once you deleted row it can’t recovered.
Most important thing is when you deleting specific rows data from the table, then you need to use WHERE command. One thing keeps in mind if you are not used WHERE clause, then all rows data from the table will be deleted.
Following are the DELETE command syntax that provided by MySQL:
         DELETE FROM
         table_name
         WHERE condition
         ORDER BY column_name
         LIMIT row_count;

Here is
         DELETE FROM – it is a delete rows data command in MySQL
table_name – which tables row data you want to delete then specify the table name here.
WHERE condition – it is optional, but if you want to specific row delete then use WHERE condition. If the condition matches with rows then it will be deleted otherwise MySQL fetch error.
ORDER BY column_name – rows will delete in specific order.
LIMIT row_count – here you can place limit on the number of rows that you want to deleted.

For Example:
Here is the, we will create a table for an employee in the database.

CREATE TABLE IF NOT EXISTS employee (
                   e_id int (10) PRIMARY KEY,
                   e_firstname varchar (50) NOT NULL,
                   e_lastname varchar (50) NOT NULL,
                   e_age int (10),
                   e_city varchar (250)
e_sex char (1)
e_phnumber int (15) NOT NULL
)

Here we insert record in the employee table.
INSERT INTO employee (
  ‘e_id’, ’e_firstname’, ’e_lastname’,
  ’e_age’, ’e_city’, ’e_sex’,
  ’e_phnumber’
)
VALUES
  (
    1, ‘Hone’, ‘Reus’, 25, ‘San Jose’, ‘M’, ‘ + 1 555 555 1111’
  ),
  (
    2, ‘Cries’, ’Fence’, 23, ’Fresno’, ’F’, ’ +1 555 555 1111’
  );

Final Result
‘e_id’ ’e_firstname’ ’e_lastname’ ’e_age’ ’e_city’  ’e_sex’ ’e_phnumber’
  1    Hone        Reus     25    San Jose   M   +1 555 555 1111
 2    Cries         Fence     23     Fresno      F    +1 555 555 1111

Now, I want to delete all the rows from employee table.

         DELETE FROM employee;

Then all rows data will be deleted.

So I want to delete first row from employee table.

         DELETE FROM employee
         WHERE e_id = ‘1’;

Let see the result after executing above query.

         SELECT * FROM employee;
        

‘e_id’ ’e_firstname’ ’e_lastname’ ’e_age’ ’e_city’  ’e_sex’   ’e_phnumber’
  2    Cries        Fence     23    Fresno     F     +1 555 555 1111

In this article, we are learned about DELETE keyword with example. Most important thing is when you deleting specific rows data from the table, then you need to use WHERE command. One thing keeps in mind if you are not used WHERE clause, then all rows data from the table will be deleted.


MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/


Comments

Popular posts from this blog

ALTER statement in MySQL

MySQL Constraint – PRIMARY KEY

Order By Clause in MySQL