首頁  >  文章  >  資料庫  >  使用 NodeJS 刪除 MySQL 表

使用 NodeJS 刪除 MySQL 表

PHPz
PHPz轉載
2023-09-05 14:09:061177瀏覽

使用 NodeJS 删除 MySQL 表

您可以使用 Node.js 中的「DROP TABLE」語句從 MySql 資料庫中刪除現有資料表。有時,我們需要刪除整個表,儘管在企業中總是建議將不使用的表歸檔而不是刪除它們。

在刪除表時,我們有兩種情況- p>

  • 如果表存在則刪除,否則拋出錯誤

  • 無論表存在與否都刪除。

我們將在這裡討論這兩種情況。

在繼續之前,請檢查以下步驟是否已執行-

  • mkdir mysql-test

  • ##cd mysql-test

  • npm init -y

  • npm install mysql

以上步驟是在專案資料夾中安裝Node-mysql依賴。

p>刪除表格

  • 刪除表格需要先建立app.js檔案。

  • 現在將以下程式碼複製並貼上到app.js 檔案中

  • #使用以下命令執行程式碼

  • #
    >> node app.js
    ul>範例1

    var mysql = require('mysql');
       var con = mysql.createConnection({
          host: "localhost",
          user: "yourusername",
          password: "yourpassword",
          database: "mydb"
       });
    
    con.connect(function(err) {
       if (err) throw err;
       //Delete the "customers" table:
       var sql = "DROP TABLE customers";
       con.query(sql, function (err, result) {
          if (err) throw err;
          console.log("Table deleted");
          console.log(result);
       });
    });

    上面的程式碼片段將引發錯誤,因為我們沒有名為「customers」的表。我們有一個名為 - Students

    輸出

    的表
    Error: ER_BAD_TABLE_ERROR: Unknown table 'bo.customers'

    範例 2

    var mysql = require('mysql');
    var con = mysql.createConnection({
       host: "localhost",
       user: "yourusername",
       password: "yourpassword",
       database: "mydb"
    });
    
    con.connect(function(err) {
       if (err) throw err;
       //Delete the "students" table:
       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 {
       fieldCount: 0,
       affectedRows: 0,
       insertId: 0,
       serverStatus: 2,
       warningCount: 0,    // If table does exist, then the count = 0
       message: '',
       protocol41: true,
       changedRows: 0
    }

    如果存在則刪除表格

    那麼,我們如何克服上述情況。好吧,在上面的例子中我們可以使用「If Exists」子句。這只會從資料庫中刪除表(如果存在),否則不會拋出錯誤,但會給予警告計數。

    • 複製貼上以下內容app.js 檔案中的程式碼

    • #使用下列指令執行程式碼

    • >> node app.js
    範例

    var mysql = require('mysql');
    var con = mysql.createConnection({
       host: "localhost",
       user: "yourusername",
       password: "yourpassword",
       database: "mydb"
    });
    
    con.connect(function(err) {
       if (err) throw err;
       //Delete the "customers" table:
       var sql = "DROP TABLE IF EXISTS customers";
       con.query(sql, function (err, result) {
          if (err) throw err;
          console.log("Table deleted");
          console.log(result);
       });
    });
    輸出

    Table deleted
    OkPacket {
       fieldCount: 0,
       affectedRows: 0,
       insertId: 0,
       serverStatus: 2,
       warningCount: 1, // If table does not exist, then the count > 0
       message: '',
       protocol41: true,
       changedRows: 0
    }

以上是使用 NodeJS 刪除 MySQL 表的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除