0% found this document useful (0 votes)
11 views

Unit-5 MYSQL Connectivity

Uploaded by

jayvaghela1542
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Unit-5 MYSQL Connectivity

Uploaded by

jayvaghela1542
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Unit-5 Connectivity with MYSQL

Node.Js Create Connection with MySQL


We can use Node.js in database applications. Here we use MySQL as a database with
Node.js.

Install MySQL on your computer.


You can download it from here https://www.mysql.com/downloads/.

Once the MySQL is installed and running, you can access it by using Node.js.

Install MySQL Driver


You have to install MySQL driver to access a MySQL database with Node.js. Download
MySQl module from npm.

To download and install the "mysql" module, open the Command Terminal and execute
the following:

1. npm install mysql

1
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Create Connection
Create a folder named "DBexample". In that folder create a js file named "connection.js"
having the following code:

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});

Now open the command terminal and use the following command:

Node connection.js

Now Node.js is connected with MySQL.

2
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Node.js MySQL Create Database
CREATE DATABASE statement is used to create a database in MySQL.

Example

For creating a database named "javatpoint".

Create a js file named javatpoint.js having the following data in DBexample folder.

1. var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE javatpoint", function (err, result) {
if (err) throw err;
console.log("Database created");
});
});

Now open command terminal and run the following command:

1. Node javatpoint.js

You can see the database is created.

Verification

3
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
To verify if the database is created or not, use the SHOW DATABASES command. Before
this, go to initial path by using mysql-p command.

4
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Node.js MySQL Create Table
CREATE TABLE command is used to create a table in MySQL. You must make it sure that
you define the name of the database when you create the connection.

Example

For creating a table named "employees".

Create a js file named employees.js having the following data in DBexample folder.

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "javatpoint"
});
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 VARCHA
R(255))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});

5
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Now open command terminal and run the following command:

1. Node employees.js

Verification
To verify if the table is created or not, use the SHOW TABLES command.

You can also check the structure of the table using DESC command:

6
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
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.

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "javatpoint"
});
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");
});
});

7
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Now open command terminal and run the following command:

1. Node employee2.js

Verification
To verify if the table is created or not, use the SHOW TABLES command.

You can also check the structure of the table using DESC command to see that id is a
primary key :

8
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
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:

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "javatpoint"
});
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");
});
});

9
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Now open command terminal and run the following command:

1. Node employee2.js

Verification

A new column salary is created in the table employee2.

10
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Node.js MySQL Insert Records
INSERT INTO statement is used to insert records in MySQL.

Example

Insert Single Record:

Insert records in "employees" table.

Create a js file named "insert" in DBexample folder and put the following data into it:

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "javatpoint"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO employees (id, name, age, city) VALUES ('1', 'Ajeet Kumar', '27', 'Alla
habad')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});

11
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Now open command terminal and run the following command:

1. Node insert.js

Check the inserted record by using SELECT query:

SELECT * FROM employees;

Insert Multiple Records


Create a js file named "insertall" in DBexample folder and put the following data into it:

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "javatpoint"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO employees (id, name, age, city) VALUES ?";
var values = [
['2', 'Bharat Kumar', '25', 'Mumbai'],
['3', 'John Cena', '35', Las Vegas'],
['4', 'Ryan Cook', '15', CA']
];

12
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
con.query(sql, [values], function (err, result) {
if (err) throw err;
console.log("Number of records inserted: " + result.affectedRows);
});
});

Now open command terminal and run the following command:

1. Node insertall.js

Output:

Check the inserted record by using SELECT query:

SELECT * FROM employees;

13
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
The Result Object
When executing the insert() method, a result object is returned.The result object contains
information about the insertion.

It is looked like this:

14
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Node.js MySQL Update Records
The UPDATE command is used to update records in the table.

Example

Update city in "employees" table where id is 1.

Create a js file named "update" in DBexample folder and put the following data into it:

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "javatpoint"
});
con.connect(function(err) {
if (err) throw err;
var sql = "UPDATE employees SET city = 'Delhi' WHERE city = 'Allahabad'";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result.affectedRows + " record(s) updated");
});
});

15
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Now open command terminal and run the following command:

1. Node update.js

It will change the city of the id 1 is to Delhi which is prior Allahabad.

You can check the updated record in the new table:

In the old table city of "Ajeet Kumar" is Allahabad.

16
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Node.js MySQL Delete Records
The DELETE FROM command is used to delete records from the table.

Example

Delete employee from the table employees where city is Delhi.

Create a js file named "delete" in DBexample folder and put the following data into it:

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "javatpoint"
});
con.connect(function(err) {
if (err) throw err;
var sql = "DELETE FROM employees WHERE city = 'Delhi'";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Number of records deleted: " + result.affectedRows);
});
});

17
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Now open command terminal and run the following command:

1. Node delete.js

You can verify the deleted record by using SELECT statement:

18
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Node.js MySQL Select Records
Example

Retrieve all data from the table "employees".

Create a js file named select.js having the following data in DBexample folder.

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "javatpoint"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM employees", function (err, result) {
if (err) throw err;
console.log(result);
});
});

19
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Now open command terminal and run the following command:

1. Node select.js

You can also use the statement:

1. SELECT * FROM employees;

20
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Node.js MySQL SELECT Unique Record
(WHERE Clause)
Retrieve a unique data from the table "employees".

Create a js file named selectwhere.js having the following data in DBexample folder.

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "javatpoint"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM employees WHERE id = '1'", function (err, result) {
if (err) throw err;
console.log(result);
});
});

21
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Now open command terminal and run the following command:

1. Node selectwhere.js

Node.js MySQL Select Wildcard


Retrieve a unique data by using wildcard from the table "employees".

Create a js file named selectwildcard.js having the following data in DBexample folder.

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "javatpoint"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM employees WHERE city LIKE 'A%'", function (err, result) {
if (err) throw err;
console.log(result);
});
});

22
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Now open command terminal and run the following command:

1. Node selectwildcard.js

It will retrieve the record where city start with A.

23
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Node.js MySQL Drop Table
The DROP TABLE command is used to delete or drop a table.

Let's drop a table named employee2.

Create a js file named "delete" in DBexample folder and put the following data into it:

var mysql = require('mysql');


var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "javatpoint"
});
con.connect(function(err) {
if (err) throw err;
var sql = "DROP TABLE employee2";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table deleted");
});
});

24
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Now open command terminal and run the following command:

ADVERTISEMENT

1. Node drop.js

Verify that the table employee2 is no more in the database.

25
Shri V J Modha College of IT

You might also like