Node JS - Node js mysql create database - myssql- Node - Node JS tutorial - webnode




How to create a Database in Node.js:

  • To create a database in MySQL, use the "CREATE DATABASE" statement:
  • Node.js can be used in database applications.
  • One of the most popular databases is MySQL.
 node-js mysql create database

Learn Node js - node js Tutorial - node-js mysql create database - node - Node js Examples

Example:

  • Create a database named "mydb":
var con = mysql.createConnection({
  host: "localhost",
  user: "wikitechy",
  password: "venkat_wikitechy"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  con.query("CREATE DATABASE mydb", function (err, result) {
    if (err) throw err;
    console.log("Database created");
  });
});

  • Save the code above in a file called "demo_create_db.js" and run the Node JS Tutorial File:
Run "demo_create_db.js"
C:\Users\Your Name>node demo_create_db.js

Primary Key:

  • When creating a table, you should also create a column with a unique key for each record.
  • This can be done by defining a column it as "INT AUTO_INCREMENT PRIMARY KEY" which will insert a unique number for each record.
  • Starting at 1, and increased by one for each record.

Example

  • Create primary key when creating the table:
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "wikitechy",
  password: "Venkat_wikitechy",
  database: "mydb"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  var sql = "CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Table created");
  });
});

Output:

 node.js-mysql-create-database

Learn Node js - node js Tutorial - node.js mysql create database - - node - Node js Examples


This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

Related Searches to Node.js MySQL Create Database