Node.js MySQL Delete Records

Node.js MySQL Delete Records

The DELETE FROM command in MySQL is used to remove one or more records from a database table. It’s an essential part of CRUD (Create, Read, Update, Delete) operations, allowing you to manage and maintain clean data efficiently.

Data Science Tutorial:–Click Here

Let’s go through an example to understand how to delete specific records using Node.js and MySQL.

Example: Delete Employee Record Based on City

Suppose you want to delete employees from the table employees where the city is Delhi.
Follow the steps below to implement this.

Introduction to Applied AI:–Click Here

  1. Create a new JavaScript file named delete.js inside a folder called DBexample.
  2. Add the following code to your delete.js file:

var mysql = require('mysql');  
var con = mysql.createConnection({  
  host: "localhost",  
  user: "root",  
  password: "12345",  
  database: "updategadh"  
});  

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);  
  });  
});  

Run the Script

Download New Real Time Projects :–Click here

Open your command terminal, navigate to the DBexample folder, and run the following command:

node delete.js

This command executes the script and deletes the records from the employees table where the city is Delhi.

Verify the Deleted Records

To confirm that the records have been removed successfully, you can use a SELECT statement to display the current data in the table:

SQL Tutorial :–Click Here

SELECT * FROM employees;

This will show you the updated records after the deletion operation.

Deep Learning Tutorial:– Click Here
Machine Learning Tutorial:–Click Here
Complete Advance AI topics:- CLICK HERE
Complete Python Course with Advance topics:-Click Here


nodejs mysql delete, nodejs mysql delete query, mysql delete nodejs, mysql delete query nodejs, nodejs delete mysql, delete query mysql nodejs, nodejs mysql delete query postman, error in delete db record using node.js and mysql, node.js mysql delete, node.js mysql delete query, delete a record mysql db, node.js mysql delete data, nodejs mysql query to delete data from database, unable to delete row from mysql using nodejs, delete data mysql node.js, node js mysql delete data, delete in node js mysql node js mysql delete records example node js mysql query return result mysql delete command mysql delete join write a program to update table records using node js and mysql database delete query in node js delete row mysql w3school sdelete all records from table mysql

Share this content:

Post Comment