Node JS - node js mysql create table - Node - Node JS tutorial - webnode




How Create a Table in Node.js MySql ?

  • Creating a basic table involves naming the table and defining its columns and each column's data type.
  • To create a table in MySQL, use the "CREATE TABLE" statement.
  • The unique name or identifier for the table follows the CREATE TABLE statement.
  • The data type of the columns may vary from one database to another. For example, NUMBER is supported in Oracle database for integer value whereas INT is supported in MySQL.
 node-js mysql create table

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

Example

  • Create a table named "customers":

demo_create_table.js

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 (name VARCHAR(255), address VARCHAR(255))";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Table created");
  });
});
  • Save the code above in a file called "demo_create_table.js" and run the Node JS Tutorial File:
Run "demo_create_table.js"
C:\Users\Your Name>node demo_create_table.js

Output:

Connected!
Table created
Related nodejs article tags - node js - node js tutorial - node js examples

Primary Key:

  • When creating a table, 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 1:

  • 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");
  });
});
  • If the table already exists, use the ALTER TABLE keyword:

Example 2:

  • Create primary key on an existing 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 = "ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Table altered");
  });
});

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 Table