Node.js MySQL Create Table

Node.js MySQL Create Table

MySQL package is used to create table in mysql using node js.

CREATE TABLE table_name mysql statement is used to create new table in mysql database.

Example : 

const mysql = require('mysql');

 

const db = mysql.createConnection({

     host: "localhost",
     user: "root",
     password: "*******",
     database : "probdb"


  });

// connect to database

db.connect((err) => {

    if (err) {

        throw err;

    }else{

     console.log('Connected to database!');

     db.query("CREATE TABLE employee(emp_name VARCHAR(255), emp_address VARCHAR(255))", function (err, result) {
          if (err) throw err;
              console.log("New table created!");
          }); 
    }

});

Write above code in server.js file and run this program as below.

D:\nodejs\website> node server.js  

It gives output as below.

Connected to database! New table created!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

88942