Drupal 8 : How to use UPDATE query with Syntax and Example

UPDATE is the Data Manipulation Language(DML) query in Database management system.  Using the object of core Drupal class we can perform database queries in Drupal 8. Update is the data modifying query used to change data in Database. The update() is a function of Drupal 8 core class used for update records. Below is syntax and example of how to use INSERT query in Drupal 8.

Syntax: 

// Update a record in table.
\Drupal::database()->update('TABLE_NAME')
	->condition(CONDITION)
	->fields([
		'FIELD_1' => NEW_VALUE_1,  // FIELD_1 NEW value.
		'FIELD_2' => NEW_VALUE_2,  // FIELD_2 NEW value.
		'FIELD_3' => NEW_VALUE_3,  // FIELD_3 NEW value.
	])
	->execute();

Example: 

\Drupal::database()->update('employee')
	->condition('employee_id' , 'CE 003')
	->updateFields([
		'employee_name' => 'Swathy',
		'employee_age' => 20,
	])
	->execute();

 

Before Update Record in Table be like:

EMPLOYEE_ID EMPLOYEE_NAME EMPLOYEE_AGE
CE 001 Mohan 20
CE 002 Dinesh 17
CE 003 Kumar 25

 

After Performing Updating a record table like:

 

EMPLOYEE_ID EMPLOYEE_NAME EMPLOYEE_AGE
CE 001 Mohan 20
CE 002 Dinesh 17
CE 003 Swathy 20
Category: