公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

您可以使用 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 文件中

        使用以下命令运行代码

        ul>

        >> node app.js

        登录后复制登录后复制

        示例 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 表的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:MySQL NodeJS 删除
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定