Node.js MySQL Drop Table
Node.js Tutorial » Node.js MySQL Drop Table
In Node js we can delete a table from Database using "DROP TABLE" statement.
Syntax:
We have a table "Users" as:
ID | Name | wage | |
---|---|---|---|
1 | Clark K | clarkkent@mail.com | 1950 |
2 | John Carter | johncarter@mail.com | 2400 |
3 | Peter Parker | clarkkent@mail.com | 3125 |
Let's make a code node js to delete a table from the database.
How to delet a table from database?
Code to DROP Table "Users" from my_db_name
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
if (err) throw err;
var sql = "DROP TABLE Users";
conn.query(sql, function (err, result) {
if (err) throw err;
console.log("Table deleted");
});
});
Save the code above in a file named "demo_test_db_drop.js", and run the file:
Which will give you this result:
Related subjects:
node.js create database
nodejs create table
node.js MySQL select from
Order By
Tags: Node js MySQL Drop Table, delete table How do I drop a table in node JS?
Is it possible to drop database in node JS?
How do you delete node JS data?
How do you delete a collection in node JS?
Deleting a Table if Exists
If the table you want to delete is already deleted or the table does not exist, you can use IF EXISTS to avoid an error.
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
if (err) throw err;
var sql = "DROP TABLE IF EXISTS Users";
conn.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
Save the code above in a file called "demo_test_db_drop2.js", and run the file:
If the table exist, the result object will look like this:
fieldCount: 0,
affectedRows: 0,v
insertId: 0,
serverstatus: 2,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0
}
The Result object when table not exist
If the table does not exist in database, the result object will be like this:
fieldCount: 0,
affectedRows: 0,v
insertId: 0,
serverstatus: 2,
warningCount: 1,
message: '',
protocol41: true,
changedRows: 0
}
Node js MySQL Drop Table, delete table How do I drop a table in node JS?
Is it possible to drop database in node JS?
How do you delete node JS data?
How do you delete a collection in node JS?
Node.js MySQL Drop Table - nodejs
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.