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:PlayNextMute

Current Time 0:01

/

Duration 18:10

Loaded: 3.30%

 Fullscreen

var mysql = require('mysql');  

var con = mysql.createConnection({  

host: "localhost",  

user: "root",  

password: "12345",  

database: ""  

});  

con.connect(function(err) {  

if (err) throw err;  

var sql = "DELETE FROM employees WHERE city = 'Delhi'";  

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

if (err) throw err;  

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

});  

});

Now open command terminal and run the following command:

Node delete.js  
Node.js delete record 1

You can verify the deleted record by using SELECT statement:

Node.js delete record 2

Comments

Leave a Reply

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