Node.js MySQL DELETE

In this post, we will discuss how to delete rows from MySQL XAMPP Server using Node.js

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				

DELETE Records

DELETE is used to delete rows from a table based on the condition specified using WHERE Clause.

Steps:

Now let's see steps

  1. First start your XAMPP Server (Both Apache and MySQL).
  2. Open Notepad or any text-editor and write the Node.js script
  3. In that script, first we have to load the mysql package using the below syntax
  4. var mysql_package = require('mysql');
  5. Create the connection using the server,username and password.
  6. CopiedCopy Code
    
    var connection_data = mysql_package.createConnection({
      host: "localhost",
      user: "root",
      password: "",
      database:"database_name"
    });
    
  7. Write the sql query that uses DELETE command
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      var delete_query = "DELETE FROM table_name WHERE condition/s....";
      connection_data.query(delete_query, function (error, result) {	  
        console.log("Row/s deleted Successfully!");
    });
    });
    
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js

Consider the field table with the following records:

alt=

DELETE Example 1:-

Let's delete rows from field table with id=1.

CopiedCopy Code

// Load the mysql package
var mysql_package = require('mysql');

// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(error) {
	// Delete rows with id=1.
  var delete_query = "DELETE FROM field WHERE id = 1";
  connection_data.query(delete_query, function (error, result) {
	  
    console.log("Row/s deleted Successfully!");
});
});
Output:
CopiedCopy Code

Row/s deleted Successfully!

Let's check in our XAMPP Server whether the rows deleted or not.

alt=

DELETE Example 2:-

Let's delete rows from field table with id=3 or type = 'sand'.

CopiedCopy Code

// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(error) {
	// Delete rows with id=3 or type = 'sand'.
  var delete_query = "DELETE FROM field WHERE id = 3 or type = 'sand'";
  connection_data.query(delete_query, function (error, result) {	  
    console.log("Row/s deleted Successfully!");
});
});
Output:
CopiedCopy Code

Row/s deleted Successfully!

Let's check in our XAMPP Server whether the rows deleted or not.

alt= Summary

In this post, we seen how to delete rows based on conditions using DELETE command using Node.js script in XAMPP Server.