Home »
Node.js
Drop a MYSQL table by using Node.js
In this article, we are going to learn how to drop a MYSQL table using Node.js Server?
Submitted by Manu Jemini, on December 29, 2017
Prerequisite/recommended:
Dropping a table can cause deletion of all your data from a particular table, so always cross check the decision with a surety question.
The very first thing is to prepare a server file of node.js where we require the MySQL module to work with MySQL and after that, we prepare a connection by using mysql.createConnection() method of MySQL module.
Then attributes like a host, user, password and database name are used define inside our create connection method.
Now the last thing, all we need to create an SQL query basically a select query with an identical field to select data from MySQL table and after that we can use query() method to execute query statement with a callback in which we can throw the error if any and print the affected values.
Database details:
- Hostname: localhost
- Port number: 3306
- Username: root
- Password: 123
- Database: demo
- Tables name: city
Steps, we are need to follow:
- Require MySQL module.
- Create Connection variable using mysql.createConnection() method.
- Connect by using con.connect() method.
- Creating a select SQL query to select data from both the tables.
- Execute query by using .query() method.
- Show result.
Server file:
//step-1
var mysql = require('mysql');
//step-2
var con = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "123",
database: "demo"
});
//step-3
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "drop table city ";
//step-4
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
Output