Node.js Tutorial
Estimated reading: 4 minutes 24 views

๐Ÿ—„๏ธ Node.js โ€“ MySQL: CRUD Operations โ€“ Insert, Read, Update & Delete with Ease

๐Ÿงฒ Introduction โ€“ Why Learn CRUD with Node.js and MySQL?

CRUD operations are the foundation of all database-driven apps. Node.js, when paired with MySQL, allows developers to perform Create, Read, Update, and Delete actions on structured dataโ€”making it a go-to solution for RESTful APIs, admin panels, and CMS systems.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to perform Insert, Select, Update, Delete operations
  • How to use WHERE, ORDER BY, and LIMIT clauses
  • Real SQL examples executed through Node.js

๐Ÿ“˜ Topics Covered

๐Ÿ”น Topic๐Ÿ“– Description
โž• Node.js โ€“ MySQL InsertAdd new records to the MySQL table
๐Ÿ” Node.js โ€“ MySQL SelectRetrieve records using SELECT statements
๐Ÿงพ Node.js โ€“ MySQL WhereFilter records with conditional logic
๐Ÿ”ข Node.js โ€“ MySQL Order BySort records based on one or more fields
โ™ป๏ธ Node.js โ€“ MySQL UpdateModify existing records based on conditions
โŒ Node.js โ€“ MySQL DeleteRemove records using specific criteria
โ›” Node.js โ€“ MySQL LimitLimit the number of rows returned by a SELECT query

โž• Node.js โ€“ MySQL Insert Into

const db = require('./db');

const sql = `INSERT INTO employees (name, position, salary) VALUES ?`;
const values = [
  ['John Doe', 'Manager', 70000],
  ['Jane Smith', 'Developer', 55000]
];

db.query(sql, [values], (err, result) => {
  if (err) throw err;
  console.log("Records inserted: " + result.affectedRows);
});

๐Ÿงช Output:

Records inserted: 2

โœ… Use parameterized queries (?) to prevent SQL injection.


๐Ÿ” Node.js โ€“ MySQL Select From

db.query("SELECT * FROM employees", (err, results) => {
  if (err) throw err;
  console.log(results);
});

๐Ÿงช Output:

[
  { id: 1, name: "John Doe", position: "Manager", salary: 70000 },
  { id: 2, name: "Jane Smith", position: "Developer", salary: 55000 }
]

โœ… Use aliases and specific fields when retrieving data in real apps.


๐Ÿงพ Node.js โ€“ MySQL WHERE Clause

db.query("SELECT * FROM employees WHERE position = 'Developer'", (err, results) => {
  if (err) throw err;
  console.log(results);
});

๐Ÿงช Output:

[
  { id: 2, name: "Jane Smith", position: "Developer", salary: 55000 }
]

โœ… The WHERE clause filters rows based on conditions.


๐Ÿ”ข Node.js โ€“ MySQL ORDER BY

db.query("SELECT * FROM employees ORDER BY salary DESC", (err, results) => {
  if (err) throw err;
  console.log(results);
});

๐Ÿงช Output:

Sorted by salary from highest to lowest

โœ… Use ASC (ascending) or DESC (descending) to control sort order.


โ™ป๏ธ Node.js โ€“ MySQL UPDATE

const sql = "UPDATE employees SET salary = ? WHERE name = ?";
db.query(sql, [60000, 'Jane Smith'], (err, result) => {
  if (err) throw err;
  console.log("Rows updated: " + result.affectedRows);
});

๐Ÿงช Output:

Rows updated: 1

โœ… Always use conditions to avoid updating unintended rows.


โŒ Node.js โ€“ MySQL DELETE

db.query("DELETE FROM employees WHERE name = 'John Doe'", (err, result) => {
  if (err) throw err;
  console.log("Rows deleted: " + result.affectedRows);
});

๐Ÿงช Output:

Rows deleted: 1

โœ… Add LIMIT 1 or use id to ensure precision and safety.


โ›” Node.js โ€“ MySQL LIMIT

db.query("SELECT * FROM employees LIMIT 2", (err, results) => {
  if (err) throw err;
  console.log(results);
});

๐Ÿงช Output:

Returns only 2 records

โœ… Useful for pagination or previewing rows.


๐Ÿ“Œ Summary โ€“ Recap & Next Steps

CRUD operations are essential for working with relational data in Node.js. By combining MySQLโ€™s powerful querying capabilities with Nodeโ€™s asynchronous design, developers can build scalable data-driven apps.

๐Ÿ” Key Takeaways:

  • Use INSERT INTO to add new data
  • Retrieve records with SELECT and refine with WHERE, ORDER BY, and LIMIT
  • Use UPDATE and DELETE carefully with conditionals
  • Always sanitize inputs using prepared statements

โš™๏ธ Real-World Uses:

  • User registration and login systems
  • Product inventory or order tracking
  • Admin dashboards with update/delete access
  • Paginated blog or comment systems

โ“ Frequently Asked Questions

โ“ How do I prevent SQL injection in Node.js MySQL?
โœ… Use placeholders (?) and parameter arrays in queries to automatically escape input.


โ“ What is the difference between SELECT * and SELECT column?
โœ… SELECT * retrieves all columns, while SELECT column1, column2 retrieves specific fieldsโ€”better for performance.


โ“ Can I use async/await with MySQL in Node.js?
โœ… Use the mysql2 or promise-mysql library for Promise support with async/await syntax.


โ“ How do I paginate results from MySQL in Node.js?
โœ… Combine LIMIT with OFFSET:

SELECT * FROM employees LIMIT 10 OFFSET 20

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ—„๏ธ Node.js โ€“ MySQL: CRUD Operations

Or Copy Link

CONTENTS
Scroll to Top