AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js MySQL Drop Table


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

Node.js Tutorial Node.js MySQL Drop Table

In Node js we can delete a table from Database using "DROP TABLE" statement.

Syntax:

DROP TABLE table_name

 

We have a table "Users" as:

ID Name Email 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

Example: DROP Table "Users" from my_db_name
var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});

conn.connect(function(err) {
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:

E:\Users\Your_Name>node demo_test_db_drop.js

Which will give you this result:

Table deleted

 

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.

Example:
var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});

conn.connect(function(err) {
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:

Example: : Run "demo_test_db_drop2.js"
E:\Users\Your_Name>node demo_test_db_drop2.js

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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Error: Unable to connect to MySQL. Debugging errno: 1045 Debugging error: Access denied for user 'u142985959_ager'@'localhost' (using password: YES)