Node.js MySQL - ALTER DROP Column
In this post, we will discuss how to remove/drop particular column from the 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:
Copied
npm install mysql
ALTER DROP Column
ALTER is used to change or modify the table structure. here we will see how to remove the column from the table.
Now let's see details steps
- First start your XAMPP Server (Both Apache and MySQL).
- Open Notepad or any text-editor and write the Node.js script
- In that script, first we have to load the mysql package using the below syntax var mysql_package = require('mysql');
- Create the connection using the server,username and password.
- Write the sql query that drop the column using ALTER
- Now type the following command in your command prompt to run the script. node file_name.js
Copied
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"database_name"
});
Copied
connection_data.connect(function(error) {
var sql_query = "ALTER TABLE table_name
DROP COLUMN column;";
connection_data.query(sql_query, function (error, result) {
console.log("column dropped Successfully");
});
});
Consider the village table with the following structure
Drop Column Example
Let's drop column-c1.
Copied
// 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 drop column
var sql_query = "ALTER TABLE village DROP COLUMN c1";
connection_data.query(sql_query, function (error, result) {
console.log("column-c1 dropped successfully");
});
});
Output:
Copied
column-c1 dropped successfully
Let's check in our XAMPP Server whether the column-c1 is dropped or not.
SummaryIn this post, we seen how to drop a column from the MySQL table in XAMPP Server through ALTER Command with Node.js Script.