Node.js से DROP TABLE स्टेटमेंट द्वारा मौजूदा टेबल हटाएं। कॉर्पोरेट परिवेश में अनुपयोगी टेबल आर्काइव करने की सलाह दी जाती है।
दो परिदृश्य
- टेबल मौजूद हो तो हटाएं, वरना त्रुटि।
- टेबल मौजूद हो या न हो, हटाएं (त्रुटि नहीं)।
पूर्वापेक्षा
mkdir mysql-test cd mysql-test npm init -y npm install mysql
उदाहरण 1: मौजूद टेबल हटाएं (students)
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 students";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table deleted");
console.log(result);
});
});
आउटपुट: Table deleted + OkPacket { affectedRows: 0, serverStatus: 2, warningCount: 0 }
उदाहरण 2: मौजूद न होने पर त्रुटि (customers)
var sql = "DROP TABLE customers"; // त्रुटि: ER_BAD_TABLE_ERROR: Unknown table 'bo.customers'
IF EXISTS क्लॉज उपयोग करना (त्रुटि नहीं, चेतावनी)
var sql = "DROP TABLE IF EXISTS customers";
// आउटपुट: Table deleted + OkPacket { warningCount: 1 }
warningCount > 0 इंगित करता है टेबल मौजूद नहीं थी।