CRUD operation in MySQL using Node.js


In this article we will learn about how we can perform CRUD (Create , Read , Update , Delete) operations in MySQL using Node.js
I will be referring to Node.js as node from here onwards in this article.


MySQL is a Relational Database Management System(RDBMS). In most general terms, It allows you to put information in a database. The information is stored in the form of Rows and columns. For more information about RDBMS


Installing the required modules
		        
npm install mysql
                
	            

After installing the mysql, we can move to our javascript file " node_crud.js "

		        
var mysql = require('mysql');
                
	            


We need to require "mysql" module because it provide us with the functionalities with the help of which we can perform CRUD operations on MySQL using node.
Now we need to establish a connection between the database and the node app.

		        
var connection = mysql.createConnection({
  host     : 'localhost',									
  user     : 'root',
  password : '',
  database : 'my_db'
});
                
	            


Here :
  • host : Whether your database is hosted on localhost or some URL
  • user : username of MySQL which you used to login.
  • password : password of MySQL which you used to login.
  • database : Name of the database with which the connection is to be established.(There should be a Database in MySQL with this name, Node.js will not create it)
You will have to manually create the table in the database. We can do that by coding also but we will be doing that in the next part of this tutorial. For now, create a table "details" in the database "my_db" .


As the name suggests, Create opeation is used to Insert the data in the Table of the database. We will perform this opeartion with the help of SQL(Structured Query Language) queries.

		        
//Here is the Query for create(i.e. Insertion) is stored in the variable "insert"
var insert = 'INSERT INTO details(name,lastname,age,mobile) VALUE(?,?,?,?)';

//Inserting a Record in TABLE details
 connection.query(insert,['ricky','sharma','25','9876543210'], function(Error,Record){
    if(Error){
		console.log("An Error occured : " + Error);
	}
    else{
        console.log('New Details Added Successfully');
    }
});
                
	            



Read opeation is used to retrieve the data from the table.

		        
//Here is the Query for Read(i.e. Retrieve) operation which is stored in the variable read.
var read = 'SELECT * FROM details';

//Retrieving all the Records from the  table "details"
  connection.query(read,function(Error,Record){
    if(Error){
		console.log("An Error occured : " + Error);
	}
    else{
        console.log('Recods : ' + JSON.stringify(Record));
    }
});
                
	            



Update opeation is used to update the data in the table.

		        
//Here is the Query for Update operation which is stored in the variable update.
var update = 'UPDATE details SET age = ? WHERE name=?';

//Updating the age of records where name = "Ricky" 
 connection.query(update,[30,'Ricky'],function(Error,Record){
    if(Error){
		console.log("An Error occured : " + Error);
	}
    else{
        console.log('Updated Successfully');
    }
});
                
	            



Delete opeation is used to Delete the data from the table.

		        
//the Query for delete operation is stored in the variable delete.
var r_delete = 'DELETE FROM details WHERE name=?';

//Deleting the record where name = "Ricky"
 connection.query(r_delete,['Ricky'],function(Error,Record){
    if(Error){
		console.log("An Error occured : " + Error);
	}
    else{
        console.log('Deleted  Successfully');
    }
});