日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

您可以使用 Node.js 中的“DROP TABLE”語句從 MySql 數(shù)據(jù)庫中刪除現(xiàn)有表。有時(shí),我們需要?jiǎng)h除整個(gè)表,盡管在企業(yè)中總是建議將不使用的表歸檔而不是刪除它們。

在刪除表時(shí),我們有兩種情況 – p>

    如果表存在則刪除,否則拋出錯(cuò)誤

    無論表存在與否都刪除。

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

    在繼續(xù)之前,請檢查以下步驟是否已執(zhí)行 –

      mkdir mysql-test

      cd mysql-test

      npm init -y

      npm install mysql

      以上步驟是在項(xiàng)目文件夾中安裝Node-mysql依賴。 p>

      刪除表

        刪除表需要先創(chuàng)建app.js文件。

        現(xiàn)在將以下代碼復(fù)制粘貼到 app.js 文件中

        使用以下命令運(yùn)行代碼

        ul>

        >> node app.js

        登錄后復(fù)制登錄后復(fù)制

        示例 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);
           });
        });

        登錄后復(fù)制

        上面的代碼片段將引發(fā)錯(cuò)誤,因?yàn)槲覀儧]有名為“customers”的表。我們有一個(gè)名為 – Students

        輸出

        的表

        Error: ER_BAD_TABLE_ERROR: Unknown table 'bo.customers'

        登錄后復(fù)制

        示例 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);
           });
        });

        登錄后復(fù)制

        輸出

        由于表存在,我們將得到以下輸出。

        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
        }

        登錄后復(fù)制

        如果存在則刪除表

        那么,我們?nèi)绾慰朔鲜銮闆r。好吧,在上面的例子中我們可以使用“If Exists”子句。這只會(huì)從數(shù)據(jù)庫中刪除表(如果存在),否則不會(huì)拋出錯(cuò)誤,但會(huì)給出警告計(jì)數(shù)。

          復(fù)制粘貼以下內(nèi)容app.js 文件中的代碼

          使用以下命令運(yùn)行代碼

          >> node app.js

          登錄后復(fù)制登錄后復(fù)制

          示例

          <!–

          現(xiàn)場演示

          –>

          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);
             });
          });

          登錄后復(fù)制

          輸出

          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
          }

          登錄后復(fù)制

          以上就是使用 NodeJS 刪除 MySQL 表的詳細(xì)內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!

分享到:
標(biāo)簽:MySQL NodeJS 刪除
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評(píng)定2018-06-03

通用課目體育訓(xùn)練成績評(píng)定