- Node.js Tutorial
- NodeJS Home
- NodeJS Introduction
- NodeJS Setup
- NodeJS First App
- NodeJS REPL
- NodeJS Command Line
- NodeJS NPM
- NodeJS Callbacks
- NodeJS Events
- NodeJS Event-Loop
- NodeJS Event-Emitter
- NodeJS Global-Objects
- NodeJS Console
- NodeJS Process
- NodeJS Buffers
- NodeJS Streams
- Node.js File Handling
- Node.js File System
- Node.js Read/Write File
- Working with folders in Node.js
- HTTP and Networking
- Node.js HTTP Module
- Anatomy of an HTTP Transaction
- Node.js MongoDB
- MongoDB Get Started
- MongoDB Create Database
- MongoDB Create Collection
- MongoDB Insert
- MongoDB Find
- MongoDB Query
- MongoDB Sort
- MongoDB Delete
- MongoDB Update
- MongoDB Limit
- MongoDB Join
- Node.js MySQL
- MySQL Get Started
- MySQL Create Database
- MySQL Create Table
- MySQL Insert Into
- MySQL Select From
- MySQL Where
- MySQL Order By
- MySQL Delete
- MySQL Update
- MySQL Join
- Node.js Modules
- Node.js Modules
- Node.js Built-in Modules
- Node.js Utility Modules
- Node.js Web Module
- Node.js Advanced
- Node.js Debugger
- Node.js Scaling Application
- Node.js Packaging
- Node.js Express Framework
- Node.js RESTFul API
- Node.js Useful Resources
- Node.js Useful Resources
- Node.js Discussion
Node.js MySQL Delete
The DELETE
statement in MySQL is used to delete existing records from a table. It allows you to remove one or more rows from the database based on a condition.
Key Features of Node.js MySQL Delete
- Delete Specific Rows: You can delete specific rows based on a condition.
- Delete All Rows: You can delete all rows in a table using the
DELETE
statement without specifying a condition. - Safe Deletion: Always use a
WHERE
clause to prevent deleting all rows by accident. - Transactional Deletion: Deleting data is often wrapped in transactions to ensure data integrity.
Step 1 Prerequisites
Ensure that MySQL is installed and the mysql2
package is added to your Node.js project.
Step 2 Basic Query to Delete Data
The simplest form of the DELETE
statement is without a condition, which removes all rows from the table.
Example Code for Deleting Specific Rows
Output:
The result indicates that one row was deleted from the users
table where the name
column matched 'John Doe'.
Step 3 Delete All Rows from a Table
If you want to delete all rows from a table, you can omit the WHERE
clause.
Example Code for Deleting All Rows
Output:
This shows that all five rows in the users
table have been deleted.
Step 4 Deleting with Multiple Conditions
You can delete rows based on multiple conditions by combining them using AND
or OR
.
Example Code for Deleting Rows with Multiple Conditions
Output:
Step 5 Deleting Records with Limit
To delete a limited number of rows, you can use the LIMIT
clause along with DELETE
.
Example Code for Deleting Limited Rows
Output:
Step 6 Using Transactions for Deletion
To ensure that the deletion process is safe and reliable, it is a good practice to use transactions.
Example Code with Transaction
Output:
Summary
The DELETE
statement in MySQL allows you to remove one or more rows from a table based on specific conditions. It can be used with WHERE
, LIMIT
, and ORDER BY
to manage which rows get deleted. For safety, always use a WHERE
clause to avoid deleting all records, and consider using transactions for atomicity when performing deletions in critical applications.