Node.js MySQL ALTER MODIFY Column

In this post, we will discuss how to change the datatype of a column in MySQL XAMPP Server with ALTER Command through Node.js script.

It is important to install mysql package in node.js

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				
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 change the datatype of the column using ALTER
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      var sql_query = "ALTER TABLE table_name
    MODIFY COLUMN column new_datatype;";
      connection_data.query(sql_query, function (error, result) {
    	  
        console.log("datatype changed Successfully");
    });
    });
    
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js

Consider the village table with the following structure

alt=

Modify Column Example 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) {
	// Query to change the datatype of a column
  var sql_query = "ALTER TABLE village MODIFY COLUMN vid varchar(255)";
  connection_data.query(sql_query, function (error, result) {
    console.log("datatype changed successfully");
  });
});
Output:
CopiedCopy Code

datatype changed successfully

Let's check in our XAMPP Server whether the datatype is changed or not.

alt=

Modify Column Example 2:

Let's change the vname column datatype from varchar to integer.

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) {
	// Query to change the datatype of a column
  var sql_query = "ALTER TABLE village MODIFY COLUMN vname int";
  connection_data.query(sql_query, function (error, result) {
    console.log("datatype changed successfully");
  });
});
Output:
CopiedCopy Code

datatype changed successfully

Let's check in our XAMPP Server whether the datatype is changed or not.

alt= Summary

In this post, we seen how to add a column to the MySQL table in XAMPP Server through ALTER Command with Node.js Script.