ホームページ  >  記事  >  データベース  >  NodeJSを使用してMySQLテーブルを削除する

NodeJSを使用してMySQLテーブルを削除する

PHPz
PHPz転載
2023-09-05 14:09:061177ブラウズ

使用 NodeJS 删除 MySQL 表

Node.js の「DROP TABLE」ステートメントを使用して、MySql データベースから既存のテーブルを削除できます。場合によっては、テーブル全体を削除する必要がある場合もありますが、企業では、未使用のテーブルを削除する代わりにアーカイブすることが常に推奨されます。

テーブルを削除する場合、2 つの状況があります - 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

    Output

    Error: ER_BAD_TABLE_ERROR: Unknown table 'bo.customers'

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

    Output

    という名前のテーブルがあります。テーブルが存在するため、次の出力が得られます。

    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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。