- 5 years ago
- Zaid Bin Khalid
- 2,614 Views
-
3
In this session, we will learn and create SQL delete statement and delete records from a database table.
Deleting Data from Tables
As we already learned in the previous session like we can insert records into a table with an insert statement, you can also delete the records in the table by using the DELETE statement.
Syntax
The DELETE statement is used to remove one and more than one row from the database table.
DELETE FROM table_name WHERE condition;
Now the delete some records from the table called the person table that we have created in create table season.
Let’s suppose that our person table has a record shown mention below.
+----+--------------------+------------+-------------+
| id | name | birth_date | phone |
+----+--------------------+------------+-------------+
| 1 | Jhon Wilson | 2000-07-15 | 0711-000001 |
| 2 | Carrie Simpson | 1945-05-01 | 0251-000009 |
| 3 | Ronaldo Ashworth | 1955-10-17 | 0695-000001 |
| 4 | George Bailey | 1993-03-05 | 0897-000040 |
| 5 | Norman Bates | 1987-08-25 | 0522-000700 |
+----+--------------------+------------+-------------+
Delete Records Based on Condition
The mention below the following Example will delete the rows from the person table, where id is greater than 3.
DELETE FROM persons WHERE id > 3;
After executing the statement. The person’s table will look at something as shown below.
+----+--------------------+------------+-------------+
| id | name | birth_date | phone |
+----+--------------------+------------+-------------+
| 1 | Jhon Wilson | 2000-07-15 | 0711-000001 |
| 2 | Carrie Simpson | 1945-05-01 | 0251-000009 |
| 3 | Ronaldo Ashworth | 1955-10-17 | 0695-000001 |
+----+--------------------+------------+-------------+
Delete All Data
If we Do not mention the WHERE Clause in the DELETE statement it will delete all the rows from the database table.
The mention below Example will delete all the records from the table which is called the person table.
After executing the statement where you will select the records from the table, it will show the empty result.
How to write Delete statement in PHP.
You can write a delete statement in PHP for that you just need to consider below code snippet which explains to you. How to write Delete Query in PHP.
<?php
$delQry = mysqli_query('DELETE FROM TABLENAME WHERE id=1');
?>
The above code is the simplest example to write a query in PHP.
- 5 years ago
- Zaid Bin Khalid
- 2,614 Views
-
3