Summary: in this tutorial, you will learn how to delete data in a MySQL database from a Node.js application.
This tutorial picks up where the Updating Data in MySQL Database from Node.js tutorial left off.
To delete data in MySQL database from a Node.js application, you follow these steps:
- Establish a connection to the MySQL database.
- Execute a
DELETE
statement by calling thequery()
method on aConnection
object. - Disconnect from the MySQL database server.
Deleting data example
The following delete.js
program deletes a row in the todos
table based on the row id:
let mysql = require('mysql');
let connection = mysql.createConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
connection.connect((err) => {
if (err) return console.error(err.message);
let sql = `DELETE FROM todos WHERE id = ?`;
let data = [1];
connection.query(sql, data, (error, results, fields) => {
if (error) return console.error(error.message);
console.log('Rows affected:', results.affectedRows);
});
// close the database connection
connection.end();
});
Code language: JavaScript (javascript)
How it works.
First, import the mysql
module:
let mysql = require('mysql');
Code language: JavaScript (javascript)
Second, create a connection to the MySQL database using the createConnection
method:
let connection = mysql.createConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
Code language: JavaScript (javascript)
Note that we retrieve the connection parameters (host, port, user, password, and database) from environment variables using .env
.
Third, connect to the MySQL database:
connection.connect((err) => {
if (err) return console.error(err.message);
});
Code language: JavaScript (javascript)
This code checks if there is any error and displays it.
Fourth, define an SQL query to delete a record in the todos
table:
let sql = `DELETE FROM todos WHERE id = ?`;
Code language: JavaScript (javascript)
Fifth, specify the data used in the query:
let data = [1];
Code language: JavaScript (javascript)
In this case, the placeholder ?
in the SQL query will be replaced by 1
. To delete a row with a different id, you can change it in the data
array.
Sixth, execute the SQL query using the query
method:
connection.query(sql, data, (error, results, fields) => {
if (error) return console.error(error.message);
console.log('Rows affected:', results.affectedRows);
});
Code language: JavaScript (javascript)
This code executes the query and displays the number of rows affected.
Finally, close the database connection:
connection.end();
Code language: CSS (css)
Verifying the delete
First, connect to the MySQL server:
mysql -u root -p
Second, change the database to todoapp
:
use todoapp;
Code language: PHP (php)
Third, retrieve the row with id 1:
SELECT * FROM todos WHERE id = 1;
Output:
Empty set (0.00 sec)
Code language: JavaScript (javascript)
It returns no row, meaning that the row with id 1 was deleted.
In this tutorial, you have learned how to delete data in MySQL from a Node.js application.