Dark Mode
Image

Node.js MySQL Delete Records

the DELETE FROM command is used to delete records from the table.

Example

Delete employee from the table employees where city is Delhi.

Create a js file named "delete" in DBexample folder and put the following data into it:

  1. var mysql = require('mysql');  
  2. var con = mysql.createConnection({  
  3. host: "localhost",  
  4. user: "root",  
  5. password: "12345",  
  6. database: "javatpoint"  
  7. });  
  8. con.connect(function(err) {  
  9. if (err) throw err;  
  10. var sql = "DELETE FROM employees WHERE city = 'Delhi'";  
  11. con.query(sql, function (err, result) {  
  12. if (err) throw err;  
  13. console.log("Number of records deleted: " + result.affectedRows);  
  14. });  
  15. });  

Now open command terminal and run the following command:

  1. Node delete.js  

Node.js delete record 1

You can verify the deleted record by using SELECT statement:

Node.js delete record 2

Comment / Reply From