AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js MySQL Create Table


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

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.

Example: create a CREATE TABLE named "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 = "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

Example: 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".

Example: ALTER TABLE statement to add a new column :

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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Error: Unable to connect to MySQL. Debugging errno: 1045 Debugging error: Access denied for user 'u142985959_ager'@'localhost' (using password: YES)