<

Node.js MySQL Create Table Tutorial

CREATE TABLE command is used to create a table in MySQL. You must make it sure that you define the name of the database when you create the connection.

Node.js MySQL Create Table Example



table.js
var mysql = require('mysql');  
var con = mysql.createConnection({  
host: "localhost",  
user: "root",  
password: "12345",  
database: "java"  
});  
con.connect(function(err) {  
if (err) throw err;  
console.log("Connected!");  
var sql = "CREATE TABLE data (id INT, name VARCHAR(255), age INT(3), city VARCHAR(255))";  
con.query(sql, function (err, result) {  
if (err) throw err;  
console.log("Table created");  
});  
});  


Now open command terminal and run the following command:


Node table.js  

















© copyright 2017-2022 Completedone pvt ltd.