Node.js MySQL Update
🚀 Node.js MySQL – UPDATE Records
The UPDATE statement is used to modify existing records in a table.
We’ll use mysql2 for all examples.
✅ 1. Install mysql2
✅ 2. Connect to MySQL Database
✅ 3. Update a Single Record
Output Example:
✅ 4. Update Multiple Records
✅ 5. Update Without WHERE (Update All Rows)
⚠️ Be careful! This will update all rows in the table.
✅ 6. Using Async/Await (Promise API)
✅ 7. Update Multiple Columns
✅ 8. Best Practices
| Practice | Reason |
|---|---|
| Always use WHERE | Avoid updating all rows unintentionally |
Use prepared statements (?) |
Prevent SQL injection |
| Wrap in try/catch when using async/await | Catch errors safely |
| Validate input | Prevent invalid data |
