Node.js MySQL Create Table

Node.js MySQL Create Table

The CREATE TABLE command in MySQL is used to create a new table within a database. When using Node.js to perform this operation, it’s important to specify the name of the database in your connection configuration before executing the SQL query.

Data Science Tutorial:–Click Here

Example: Creating a Table Named “employees”

Let’s start by creating a table called employees in a MySQL database using Node.js.

Create a JavaScript file named employees.js inside a folder called DBexample, and add the following code:

var mysql = require('mysql');  
var con = mysql.createConnection({  
  host: "localhost",  
  user: "root",  
  password: "12345",  
  database: "updategadh"  
});  

con.connect(function(err) {  
  if (err) throw err;  
  console.log("Connected!");  
  var sql = "CREATE TABLE employees (id INT, name VARCHAR(255), age INT(3), city VARCHAR(255))";  
  con.query(sql, function (err, result) {  
    if (err) throw err;  
    console.log("Table created");  
  });  
});  

Now open your command terminal and run the following command:

Introduction to Applied AI:–Click Here

node employees.js

This will create a new table named employees in the database.

Verification

To verify whether the table was created successfully, use the following MySQL command:

SHOW TABLES;

To check the structure of the created table, run:

DESC employees;

Creating a Table with a Primary Key

Download New Real Time Projects :–Click here

You can also define a primary key while creating a table. Let’s create another table named employee2 with id as the primary key.

Create a file named employee2.js in the DBexample folder and add this code:

var mysql = require('mysql');  
var con = mysql.createConnection({  
  host: "localhost",  
  user: "root",  
  password: "12345",  
  database: "updategadh"  
});  

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

Run the file using the command:

SQL Tutorial :–Click Here

node employee2.js

Verification

Use the following command to confirm table creation:

SHOW TABLES;

To verify that the id column is set as the primary key, use:

DESC employee2;

Adding Columns to an Existing Table

The ALTER TABLE statement allows you to modify an existing table, such as adding new columns. Let’s add a new column named salary to the employee2 table.

Complete Python Course with Advance topics:-Click Here

Replace the content of employee2.js with the following code:

var mysql = require('mysql');  
var con = mysql.createConnection({  
  host: "localhost",  
  user: "root",  
  password: "12345",  
  database: "updategadh"  
});  

con.connect(function(err) {  
  if (err) throw err;  
  console.log("Connected!");  
  var sql = "ALTER TABLE employee2 ADD COLUMN salary INT(10)";  
  con.query(sql, function (err, result) {  
    if (err) throw err;  
    console.log("Table altered");  
  });  
});  

Run the command:

node employee2.js

Verification

To confirm that the column has been added successfully, run:

DESC employee2;

You will see a new column named salary in the table structure.

Machine Learning Tutorial:–Click Here
Complete Advance AI topics:- CLICK HERE
Deep Learning Tutorial:– Click Here


node js mysql create table example node js mysql create table w3schools create html table in node js how to insert data dynamically in mysql using node js node js mysql query return result node js insert data into mysql node js mysql display table on html site how to create model in node js with mysql mysql create table nodejs, create table in mysql using node js, mysql create tables, javascript mysql create table, node.js sqlite3 create table, node sqlite3 create table, node js sqlite create table query, node js mysql insert table, node sqlite create table query, nodejs mysql, nodejs mysql select, create nodejs app, nodejs + mysql, mysql + nodejs, nodejs mysql query, mysql in nodejs, nodejs mysql insert, mysql and nodejs, nodejs and mysql, execute mysql nodejs, mysql with nodejs, nodejs mysql tutorial mysql create table with primary key mysql create table if not exists mysql create table data types mysql create table date mysql create table foreign key create table in mysql and insert data mysql table example how to create table in mysql command line mysql data types mysql create database mysql create table example mysql create table w3schools

Share this content:

Post Comment