MySQL query to delete row



Use the DELETE to delete a row in MySQL. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> CustomerName varchar(100),
   -> CustomerAge int
   -> );
Query OK, 0 rows affected (1.30 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(CustomerName,CustomerAge) values('John',33);
Query OK, 1 row affected (0.10 sec)

mysql> insert into DemoTable(CustomerName,CustomerAge) values('Bob',25);
Query OK, 1 row affected (0.16 sec)

mysql> insert into DemoTable(CustomerName,CustomerAge) values('David',28);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable(CustomerName,CustomerAge) values('Carol',29);
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+------------+--------------+-------------+
| CustomerId | CustomerName | CustomerAge |
+------------+--------------+-------------+
|          1 | John         | 33          |
|          2 | Bob          | 25          |
|          3 | David        | 28          |
|          4 | Carol        | 29          |
+------------+--------------+-------------+
4 rows in set (0.00 sec)

Following is the query to delete a row in MySQL −

mysql> delete from DemoTable where CustomerName='David' and CustomerAge=28;
Query OK, 1 row affected (0.23 sec)

Let us check the table records once again −

mysql> select *from DemoTable;

Output

This will produce the following output −

+------------+--------------+-------------+
| CustomerId | CustomerName | CustomerAge |
+------------+--------------+-------------+
| 1          | John         | 33          |
| 2          | Bob          | 25          |
| 4          | Carol        | 29          |
+------------+--------------+-------------+
3 rows in set (0.00 sec)
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements