Junior — Middle
Explain the principle of bulk data update operation in a database.
sobes.tech AI
Answer from AI
The bulk update operation in a database allows changing multiple records with a single command, which is much more efficient than updating each record separately.
Working principle:
- A single SQL query is formed with a condition that covers all the necessary records (for example,
UPDATE table SET column = value WHERE condition). - The database server executes the update of all matching rows in one transaction.
- This reduces overhead for query transmission and decreases lock times.
Example in SQL:
UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales';
In Java using JDBC, it might look like this:
String sql = "UPDATE employees SET salary = salary * 1.1 WHERE department = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, "Sales");
int updatedRows = stmt.executeUpdate();
System.out.println("Updated records: " + updatedRows);
}
Thus, bulk update allows efficiently and quickly changing a large amount of data with minimal resource expenditure.