SQL Delete Statement

0
18

sqltuts

If you want to delete a record from any MySQL table, then you can use SQL DELETE Statement.

1.)command at mysql> prompt

2.) any script like PHP.

 
DELETE FROM table_name [WHERE Clause]
 

The WHERE clause is used when a user  wants to delete selected rows in a table of a given database.

Deleting Data from Command Prompt:

SQL DELETE Statment with WHERE clause is used in order to delete selected data into MySQL table tutorials_table.

Example:

  1. root@host# mysql -u root -p password;
  2. Enter password:*******
  3. mysql> use Database TUTORIALS;
  4. Database changed
  5. mysql> DELETE FROM Database tutorials_table WHERE tutorial_id=15;
  6. Query OK, 1 row affected (0.23 sec)
  7.  
  8. mysql>

 

Deleting Data Using PHP Script:

Example:

 

  1. <?php
  2. $dbhost = 'localhost';
  3. $dbusername = 'root';
  4. $dbpassword = 'rootpassword';
  5. $conn = mysql_connect($dbhost, $dbusername, $dbpassword);
  6. if(! $con )
  7. {
  8.   die('Could not connect: ' . mysql_error());
  9. }
  10. $sql = 'DELETE FROM Datbase tutorials_table
  11.        WHERE tutorial_id=15';
  12.  
  13. mysql_select_db('DatabaseTUTORIALS');
  14. $retval = mysql_query( $sql, $con );
  15. if(! $result )
  16. {
  17.   die('Could not be able to delete data: ' . mysql_error());
  18. }
  19. echo "Deleted  the desired data successfully\n";
  20. ?>
Facebook Comments
(Visited 13 times, 1 visits today)
SHARE
Previous articleSQL UPDATE Statement
Next articleSQL Create Table
Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!

LEAVE A REPLY

Please enter your comment!
Please enter your name here