1. MySQL Create Connection
1.1. Install MySQL Driver : npm install mysql
1.2. Create Connection : connection.js
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "abcd"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
2. MySQL Create Database
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "abcd"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE dbMysql", function (err, result) {
if (err) throw err;
console.log("Database created");
});
});
3. MySQL Create Table
3.1. Membuat Table
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "abcd",
database: "dbMysql"
});
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");
});
});
3.2. Membuat Table Primary Key
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "dbMysql"
});
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");
});
});