Node.js MySQL Drop Table 语句
删除表
使用 "DROP TABLE" 语句删除现有表:
实例
删除 "customers" 表:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
var sql = "DROP TABLE customers";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table deleted");
});
});
运行实例 »
将上述代码保存在名为 "demo_db_drop_table.js" 的文件中,然后运行该文件:
运行 "demo_db_drop_table.js"
C:\Users\Your Name>node demo_db_drop_table.js
返回结果:
Table deleted
Drop Only if Exist
如果要删除的表已被删除,或者由于任何其他原因不存在,可以使用 IF EXISTS 关键字以避免出现错误。
实例
删除 "customers" 表(如果存在):
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
var sql = "DROP TABLE IF EXISTS customers";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
运行实例 »
将上述代码保存在名为 "demo_db_drop_table_if.js"