Node.js MySQL Table Creation

In this post, we will discuss how to create a table in XAMPP Server using Node.js Script.

It is important to install mysql package in node.js

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				
Steps:

Now let's see steps

  1. First start your XAMPP Server (Both Apache and MySQL).
  2. Open Notepad or any text-editor and write the Node.js script
  3. In that script, first we have to load the mysql package using the below syntax
  4. var mysql_package = require('mysql');
  5. Create the connection using the server,username and password.
  6. CopiedCopy Code
    
    var connection_data = mysql_package.createConnection({
      host: "localhost",
      user: "root",
      password: ""
    });
    
  7. Write the sql query to create a table in XAMPP. It will take two parameetrs. The first parameter is the SQL Query and the second parameter will handle the result.
  8. CopiedCopy Code
    
    connection_data.connect(function(err) {
      var sql = "CREATE TABLE table_name (column_name datatype,....)";
      connection_data.query(sql, function (err, result) {
       console.log("Table successfully created");
      });
    });
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js
  11. You can verify whether the table is created or not in XAMPP SERver by using the URL in a browser:
  12. http://localhost/phpmyadmin

TABLE Creation Example 1:-

Let's create a table named village in the facility database that have 3 columns.

CopiedCopy Code

 // Load the mysql package
var mysql_package = require('mysql');

// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});

connection_data.connect(function(err) {
 
 // Create a table with three columns in facility database.
 //Here columns are village_name,district and people.
  var sql = "CREATE TABLE village (village_name VARCHAR(255), 
  distcict VARCHAR(255), people int)";
  connection_data.query(sql, function (err, result) {
   console.log("Table-village successfully created");
  });
});
Output:
CopiedCopy Code

Table-village successfully created
http://localhost/phpmyadmin/sql.php?db=facility&table=village/pos=0

TABLE Creation Example 2:-

CopiedCopy Code

// Load the mysql package
var mysql_package = require('mysql');

// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});

connection_data.connect(function(err) {
 
 // Create a table with three columns in facility database.
 //Here columns are village_name,field_type.
  var sql = "CREATE TABLE field (village_name VARCHAR(255), 
  field_type varchar(255))";
  connection_data.query(sql, function (err, result) {
   console.log("Table-field successfully created");
  });
});
Output:
CopiedCopy Code

Table-field successfully created

You can see that the table- field is created. Now let's check in XAMPP Server. - http://localhost/phpmyadmin/sql.php?db=facility&table=field&pos=0

Summary

So we seen how to create a table in XAMPP Server through Node.js script with two examples. make sure that you have to install mysql package and xampp server.