Example delete record in mysql with nodejs

Delete Record

You can delete records from an existing table by using the "DELETE FROM" statement:

Example: Delete any record with the address "Mountain 21"

 

var mysql = require('mysql');

var con = mysql.createConnection({

  host: "localhost",

  user: "root",

  password: "",

  database: "mydb"

});

con.connect(function(err) {

  if (err) throw err;

  //Delete all customers with the address "Mountain 21":

  var sql = "DELETE FROM customers WHERE address = 'Mountain 21'";

  con.query(sql, function (err, result) {

    if (err) throw err;

    console.log("Number of records deleted: " + result.affectedRows);

    console.log(result);

  });

});

 

Run and result:

 

C:\Nodejs>node mysqlDeleteRecord.js

Number of records deleted: 1

OkPacket {

  fieldCount: 0,

  affectedRows: 1,

  insertId: 0,

  serverStatus: 34,

  warningCount: 0,

  message: '',

  protocol41: true,

  changedRows: 0 }