🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Node.js Notes
Topic #55

MySQL Drop Table


Delete a Table

You can delete an existing table by using the "DROP TABLE" statement:

Example

let mysql = require('mysql');

let con = mysql.createConnection({

host: "localhost",
  user: "yourusername",

password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;

let sql = "DROP TABLE customers";

con.query(sql, function (err, result) {

if (err) throw err;
    console.log("Table deleted");
  });
});

Save the code above in a file called "demo_db_drop_table.js" and run the file:

C:\Users\Your Name>node demo_db_drop_table.js

Which will give you this result:

Table deleted

Drop Only if Exist

If the the table you want to delete is already deleted, or for any other reason does not exist, you can use the IF EXISTS keyword to avoid getting an error.

Example

let mysql = require('mysql');

let con = mysql.createConnection({

host: "localhost",
  user: "yourusername",

password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;

let sql = "DROP TABLE IF EXISTS customers";

con.query(sql, function (err, result) {

if (err) throw err;
    console.log(result);
  });
});

Save the code above in a file called "demo_db_drop_table_if.js" and run the file:

C:\Users\Your Name>node demo_db_drop_table_if.js

If the table exist, the result object will look like this:

{
  fieldCount: 0,
  affectedRows: 0,
  insertId: 0,

serverstatus: 2,
  warningCount: 0,
  message: '',

protocol41: true,
  changedRows: 0
}

If the table does not exist, the result object will look like this:

{
  fieldCount: 0,
  affectedRows: 0,
  insertId: 0,

serverstatus: 2,
  warningCount: 1,
  message: '',

protocol41: true,
  changedRows: 0
}

As you can see the only differnce is that the warningCount property is set to 1 if the table does not exist.

Want to go beyond the notes?

Join CodingNow 2.0's Node.js course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

MySQL Drop Table – FAQs

Quick answers about learning MySQL Drop Table in Node.js.

This free note from CodingNow 2.0 explains MySQL Drop Table in Node.js — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Node.js topic on CodingNow 2.0, including MySQL Drop Table, is 100% free with no signup required.
With focused practice, most students grasp MySQL Drop Table in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now