Create a table using “CREATE TABLE”
First, we create a table named 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;
console.log("Connected!");
var sql = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});
Save the file as “demo_create_table.js” and then run it
node demo_create_table.js
If successful, the following will appear
Connected!
Table created