how to create mysql database table in node.js

The tutorial provides the node.js integration to create the mysql Database Tables. The tutorial provides the steps on how to create tables in mysql database using node.js application .

Create Table into mysql using node.js

The MySQL uses ‘CREATE TABLE’ statement for creating tables in mySQL. Ensure that you have the mysql database created before proceeding with the table creation process.

var mysql = require('mysql');  
var con = mysql.createConnection({  
  host: "localhost",  
  user: "root",  
  password: ""  ,
  database: "oracleappshelpDB"
});
con.connect(function(err) {
  if (err) throw err;
  console.log("MySQL Database oracleappshelpDB is Connected successfully!");
  var sql = "CREATE TABLE oracleappshelpUsers (firstName VARCHAR(255), lastName VARCHAR(255), address VARCHAR(255))";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Table oracleappshelpUsers is created successfully in the Database - oracleappshelpDB");
  });
});

Mysql create Primary Key

Lets modify the node.js program to include the Primary Key creation and SELECT Statement also for the table -oracleappshelpUsers.

var mysql = require('mysql');  
var con = mysql.createConnection({  
  host: "localhost",  
  user: "root",  
  password: ""  ,
  database: "oracleappshelpDB"
});
con.connect(function(err) {
  if (err) throw err;
  console.log("MySQL Database oracleappshelpDB is Connected successfully!");
  var sql = "CREATE TABLE oracleappshelpUsers (id INT AUTO_INCREMENT PRIMARY KEY, firstName VARCHAR(255), lastName VARCHAR(255), address VARCHAR(255))";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Table oracleappshelpUsers is created successfully in the Database - oracleappshelpDB");
  });
var sql2 = "SELECT * FROM `oracleappshelpusers` ";
  con.query(sql2, function (err, result) {
    if (err) throw err;
    console.log(" SELECT Statement on the table oracleappshelpUsers is executed successfully");
  });
  
});