In this tutorial you will learn about the Nodejs MySQL Delete Records and its application with practical example.
Node.js MySQL Delete Records
A record can be deleted from MySQL Table simply by using “DELETE FROM” statement.
Example :- Let’s delete a record from “employees” table where “id” is 5.
Step 1:- Let’s, create a node_mysql_delete_tbl.js file and put the following code in it –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "root", password: "", database: "nodemysql" }); con.connect(function(err) { if (err) throw err; var sql = "DELETE FROM employees WHERE id = 5"; con.query(sql, function (err, result) { if (err) throw err; console.log("W3Adda | MySQL Record Deleted."); console.log("No. of records deleted- " + result.affectedRows); }); }); |
Step 2:- Save the code and open the terminal again, and type the following command in order to run the file.
1 |
$ node node_mysql_delete_tbl.js |
you will see following output on terminal –
Before Delete :-
After Delete :-