MYSQL - DELETE Query

MYSQL DELETE Query is used to delete existing records in a table.

  • You can delete one or more field altogether.
  • You can use any condition using the WHERE clause.
  • You can delete the values in a single table at a time.

The WHERE clause is very useful when you want to delete the selected rows in a table.


MYSQL Syntax :

DELETE FROM table_name
WHERE condition;

MYSQL Query Example :
DELETE FROM tblstudent WHERE stud_id = 2;

Shell command script:
Also you can run delete query using mysql command Prompt.
[root@host]# mysql -u root -p
Enter password:********

mysql> use aryatechno;
Database changed

mysql> DELETE FROM tblstudent WHERE stud_id = 2;
Query OK, 1 row affected (0.003 sec)



PHP SCRIPT :
You can delete any records from table using php code as per as below.

<?php
$hostname="localhost";
$username="root";
$password="********";
$link= mysqli_connect($hostname, $username, $password);
mysqli_select_db($link,DATABASE);
$returnval = mysqli_query($link,"DELETE FROM tblstudent WHERE stud_id = 2");
 
if($returnval ){
        echo "Data is deleted successfully.";
}else{
       die('MySQL Error : ' . mysqli_error($link));
}
mysqli_close($link);

?>

Comments

Leave a Reply

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

80090