Node.js MySQL Create Table
Node.js Tutorial » Node.js create table
How to create a table in SQL with node js?
In this chapter you will learn how to create a new MySQL table from node.js application.
Use these steps to create a table from nodejs:
1. First connect to MySQL db server.
2. Second call the query() method on the connection object to execute a CREATE TABLE statement.
3. Close the DB connection.
Node.js MySql Create Table
MySQL Database Table can be created using “CREATE TABLE”
Let’s create MySQL database Table named as “Users” under “my_db_name” database.
var mysql = require('mysql');
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "CREATE TABLE Users (
ID INT,
name VARCHAR(255),
address VARCHAR(255),
email VARCHAR(255))";
conn.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});
Related subjects:
node.js create database
nodejs create connection pool
node.js Send Email
upload files
node js create table: example, if not exist, from json, sqlite, sequelize create table, sqlite3 create table if not exists
How do you create a table in SQL with node js?
Does Sequelize create table?
How do I display a table in node js?
Which of the following command is used to check if the table is created or not in node js?
Create Table with Primary Key
Simple we add to ID INT - PRIMARY KEY
var mysql = require('mysql');
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "CREATE TABLE Users (
ID INT PRIMARY KEY,
name VARCHAR(255),
address VARCHAR(255),
email VARCHAR(255))";
conn.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});
Add columns in existing Table:
We use ALTER TABLE statement to add a new column named "country" in an existing table "Users".
var mysql = require('mysql');
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
conn.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "ALTER TABLE Users ADD COLUMN country VARCHAR(255)";
conn.query(sql, function (err, result) {
if (err) throw err;
console.log("Table altered");
});
});
node js create table: example, if not exist, from json, sqlite, sequelize create table, sqlite3 create table if not exists
How do you create a table in SQL with node js?
Does Sequelize create table?
How do I display a table in node js?
Which of the following command is used to check if the table is created or not in node js?
Node.js MySQL Create Table - nodejs
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.