HI WELCOME TO Sirees

Node.js MySQL Create Table

Leave a Comment
Example
For creating a table named "employees".
Create a js file named employees.js having the following data in DBexample folder.
  1. var mysql = require('mysql');  
  2. var con = mysql.createConnection({  
  3. host: "localhost",  
  4. user: "root",  
  5. password: "12345",  
  6. database: "javatpoint"  
  7. });  
  8. con.connect(function(err) {  
  9. if (err) throw err;  
  10. console.log("Connected!");  
  11. var sql = "CREATE TABLE employees (id INT, name VARCHAR(255), age INT(3), city VARCHAR(255))";  
  12. con.query(sql, function (err, result) {  
  13. if (err) throw err;  
  14. console.log("Table created");  
  15. });  
  16. });  
Now open command terminal and run the following command:
  1. Node employees.js  
Node.js create table 1

Verification

To verify if the table is created or not, use the SHOW TABLES command.
Node.js create table 2
You can also check the structure of the table using DESC command:
Node.js create table 3

Create Table Having a Primary Key

Create Primary Key in New Table:
Let's create a new table named "employee2" having id as primary key.
Create a js file named employee2.js having the following data in DBexample folder.
  1. var mysql = require('mysql');  
  2. var con = mysql.createConnection({  
  3. host: "localhost",  
  4. user: "root",  
  5. password: "12345",  
  6. database: "javatpoint"  
  7. });  
  8. con.connect(function(err) {  
  9. if (err) throw err;  
  10. console.log("Connected!");  
  11. var sql = "CREATE TABLE employee2 (id INT PRIMARY KEY, name VARCHAR(255), age INT(3), city VARCHAR(255))";  
  12. con.query(sql, function (err, result) {  
  13. if (err) throw err;  
  14. console.log("Table created");  
  15. });  
  16. });  
Now open command terminal and run the following command:
  1. Node employee2.js  
Node.js create table 4

Verification

To verify if the table is created or not, use the SHOW TABLES command.
Node.js create table 5
You can also check the structure of the table using DESC command to see that id is a primary key :
Node.js create table 6
Add columns in existing Table:
ALTER TABLE statement is used to add a column in an existing table. Take the already created table "employee2" and use a new column salary.
Replace the data of the "employee2" table with the following data:
  1. var mysql = require('mysql');  
  2. var con = mysql.createConnection({  
  3. host: "localhost",  
  4. user: "root",  
  5. password: "12345",  
  6. database: "javatpoint"  
  7. });  
  8. con.connect(function(err) {  
  9. if (err) throw err;  
  10. console.log("Connected!");  
  11. var sql = "ALTER TABLE employee2 ADD COLUMN salary INT(10)";  
  12. con.query(sql, function (err, result) {  
  13. if (err) throw err;  
  14. console.log("Table altered");  
  15. });  
  16. });  
Now open command terminal and run the following command:
  1. Node employee2.js  
Node.js create table 7

Verification

Node.js create table 8
A new column salary is created in the table employee2.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.