حذف الجداول من قاعدة البيانات (Node.js MySQL Drop Table)

حذف جدول Node.js MySQL Drop Table
يمكنك حذف جدول موجود باستخدام  الأمر"DROP TABLE":
مثال
حذف جدول "customers":
 
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  var sql = "DROP TABLE customers";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Table deleted");
  });
});
احفظ الكود أعلاه في ملف بإسم "demo_db_drop_table.js" وقم بتشغيل الملف:
C:\Users\Your Name>node demo_db_drop_table.js
والتي ستعطيك هذه النتيجة:
Table deleted
حذف الجدول فقط إذا كان موجودًا
 إذا تم حذف الجدول الذي تريد حذفه بالفعل ، أو لأي سبب آخر غير موجود ، يمكنك استخدام الكلمة الأساسية IF EXISTS لتجنب حدوث خطأ. 
 مثال احذف جدول "customers" إذا كان موجودًا:
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  var sql = "DROP TABLE IF EXISTS customers";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log(result);
  });
});
احفظ الكود أعلاه في ملف بإسم "demo_db_drop_table_if.js" وقم بتشغيل الملف:
C:\Users\Your Name>node demo_db_drop_table_if.js
إذا كان الجدول موجودًا ، فسيبدو الكائن الناتج (Object result)كما يلي:
{
  fieldCount: 0,
  affectedRows: 0,
  insertId: 0,
  serverstatus: 2,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0
}
إذا لم يكن الجدول موجودًا ، فسيبدو الكائن الناتج  (Object result) كما يلي:
{
  fieldCount: 0,
  affectedRows: 0,
  insertId: 0,
  serverstatus: 2,
  warningCount: 1,
  message: '',
  protocol41: true,
  changedRows: 0
}
كما ترون ، فإن الاختلاف الوحيد هو أن الخاصية WarningCount ترجع القيمة 1 إذا لم يكن الجدول موجودًا.