Using SQL DELETE statement to delete a row

No comments
SQL DELETE statement is used to delete row (record) from the table. Usually we use WHERE clause with DELETE to specify the row which we want to delete.
+----+--------+---------+------+-----------+
| ID | FNAME  | LNAME   | AGE  | COUNTRY   |
+----+--------+---------+------+-----------+
|  1 | AKSHAY | KUMAR   |   12 | INDIA     |
|  2 | JONTY  | RHODES  |   40 | CHINA     |
|  3 | JOHNY  | DEPP    |   45 | INDIA     |
|  4 | JESSIE | PINKMAN |   25 | AUSTRALIA |
|  5 | JESSIE | JACKSON |   22 | CANADA    |
|  6 | NICOLE | KIDMAN  |   50 | USA       |
|  7 | GEORGE | CLOONEY | NULL | USA       |
+----+--------+---------+------+-----------+
7 rows in set (0.08 sec)

SQL DELETE statement syntax and example in MySql

Suppose we want to delete the row with ID 7 from the above table, use the following SQL statement -

DELETE FROM CUSTOMER WHERE ID = 7;
+----+--------+---------+------+-----------+
| ID | FNAME  | LNAME   | AGE  | COUNTRY   |
+----+--------+---------+------+-----------+
|  1 | AKSHAY | KUMAR   |   12 | INDIA     |
|  2 | JONTY  | RHODES  |   40 | CHINA     |
|  3 | JOHNY  | DEPP    |   45 | INDIA     |
|  4 | JESSIE | PINKMAN |   25 | AUSTRALIA |
|  5 | JESSIE | JACKSON |   22 | CANADA    |
|  6 | NICOLE | KIDMAN  |   50 | USA       |
+----+--------+---------+------+-----------+
6 rows in set (0.00 sec)
Now we can see that row with ID 7 has been deleted.

DELETE all table rows SQL

We need to be careful while using DELETE statement , because we we don't use the WHERE clause it will delete all the rows from the table. See the statement below.
-- Warning ! below statement will delete all the rows from table CUSTOMER
DELETE FROM CUSTOMER;

No comments :

Post a Comment