>  기사  >  데이터 베이스  >  NodeJS를 사용하여 MySQL 테이블 삭제

NodeJS를 사용하여 MySQL 테이블 삭제

PHPz
PHPz앞으로
2023-09-05 14:09:061175검색

使用 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 파일에 붙여넣습니다.

  • 다음 명령을 사용하여 코드를 실행합니다.

  • 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" 테이블이라는 파일이 없습니다.

    Error: ER_BAD_TABLE_ERROR: Unknown table 'bo.customers'

    라는 이름의 테이블이 있습니다. - Students

    Output

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

    출력

    rreee

위 내용은 NodeJS를 사용하여 MySQL 테이블 삭제의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제