Unit-5 MYSQL Connectivity
Unit-5 MYSQL Connectivity
Once the MySQL is installed and running, you can access it by using Node.js.
To download and install the "mysql" module, open the Command Terminal and execute
the following:
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:
Now open the command terminal and use the following command:
Node connection.js
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
Create a js file named javatpoint.js having the following data in DBexample folder.
1. Node javatpoint.js
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
Create a js file named employees.js having the following data in DBexample folder.
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:
Create a js file named employee2.js having the following data in DBexample folder.
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:
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
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
Create a js file named "insert" in DBexample folder and put the following data into it:
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
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);
});
});
1. Node insertall.js
Output:
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.
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
Create a js file named "update" in DBexample folder and put the following data into it:
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
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
Create a js file named "delete" in DBexample folder and put the following data into it:
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
18
Shri V J Modha College of IT
Unit-5 Connectivity with MYSQL
Node.js MySQL Select Records
Example
Create a js file named select.js having the following data in DBexample folder.
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
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.
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
Create a js file named selectwildcard.js having the following data in DBexample folder.
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
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.
Create a js file named "delete" in DBexample folder and put the following data into it:
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
25
Shri V J Modha College of IT