English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS Basic Tutorial

NodeJS Express.js

NodeJS Buffer&URL;

NodeJS MySql

NodeJS MongoDB

NodeJS File(FS)

Other NodeJS

Node.js MySQL UPDATE Query

Node.js MySQL UPDATE Update Table Query

You can use MySQL UPDATE query to update records in the table.

  • MySQL update record(s) table

  • All records updated in MySQL table

Node.js MySQL Update Update Table Records

Considering that due to human error, instudentsrecords marked as inserted into the table74instead of84.Now, we will execute a MySQL update query that updatesmarksThe value of the column is84Among which the value is74.

// Import the mysql module
var mysql = require('mysql'); 
 
// Create a connection variable with the required details
var con = mysql.createConnection({ 
  host: "localhost", // The IP address of the server running mysql
  user: "arjun", // The username for the mysql database
  password: "password", // The corresponding password
  database: "studentsDB" // Use the specified database
 }); 
 
// Establish a connection with the database.
con.connect(function(err) { 
  if (err) throw err; 
  // 
  con.query("UPDATE students SET marks=",84 WHERE marks=74", function (err, result, fields) { 
    // If any errors occur during the execution of the above query, an error is thrown
    if (err) throw err; 
    // If there are no errors, you will get the result
    console.log(result); 
  }); 
 });

Run the above program in the terminal

Terminal Output

arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node UpdateRecordsFiltered.js 
OkPacket {}} 
  fieldCount: 0, 
  affectedRows: 3, 
  insertId: 0, 
  serverStatus: 34, 
  warningCount: 0, 
  message: '(Rows matched: 3  Changed: 3  Warnings: 0', 
  protocol41: true, 
  changedRows: 3 }

Node.js MySQL Update All Records in Table

Consider a situation where an exam for a student has been cancelled for some reason, and you want to conduct the exam again. Therefore, you want to update the scores of all students to 0. The following Node.js MySQL UPDATE query example shows how to update all records in the table.

// Import the mysql module
var mysql = require('mysql'); 
 
// Create a connection variable with the required details
var con = mysql.createConnection({ 
  host: "localhost", // The IP address of the server running mysql
  user: "arjun", // The username for the mysql database
  password: "password", // The corresponding password
  database: "studentsDB" // Use the specified database
 }); 
 
// Establish a connection with the database.
con.connect(function(err) { 
  if (err) throw err; 
  // 
  con.query("UPDATE students SET marks=",84", function (err, result, fields) { 
    // If any errors occur during the execution of the above query, an error is thrown
    if (err) throw err; 
    // If there are no errors, you will get the result
    console.log(result); 
  }); 
 });

It was observed that there was no WHERE clause in the query, so all records were selected for the update process.

Conclusion:

In this Node.js tutorial – Node.js MySQL – UPDATE Table Query, we learned how to update records or all records in a table based on conditions.