UPDATE

UPDATE is an SQL command used to modify existing records within a database table. It can be used to revise a single or multiple column values. The rows to be updated are typically specified with a condition provided by the WHERE clause. Without a WHERE clause, the command updates all rows.

Example

UPDATE employees
SET salary = 4000
WHERE employeeId = 1;

Output

Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Explanation

In the example, the UPDATE command modifies the data of one row in the ‘employees’ table. The SET clause changes the ‘salary’ value to 4000 for the row where ‘employeeId’ equals 1. The output shows that the update operation was successful, one row was matched and affected, and no warnings were issued.

Example

UPDATE customers
SET email = 'new_email@example.com'
WHERE customer_id = 1;

Output

UPDATE 1

Explanation

The above SQL statement updates email of the customer with customer_id 1 in the customers table to new_email@example.com. The output UPDATE 1 indicates that one row was updated successfully.

Example

UPDATE Employees
SET LastName = 'Johnson', FirstName = 'John'
WHERE EmployeeID = 1;

Output

(1 row(s) affected)

Explanation

The UPDATE statement is used to modify existing records in a table. In the above example, the SQL statement updates the Employees table to set the LastName and FirstName as ‘Johnson’ and ‘John’ respectively for the EmployeeID currently set to 1. The output shows that one row has been affected, meaning the changes have been successfully made.

Example

UPDATE employees
SET salary = 60000
WHERE employee_id = 100;
COMMIT;

Output

1 row updated.

Explanation

In the example query, the UPDATE statement is used to modify the records in the ‘employees’ table. Specifically, it updates the ‘salary’ column value to ‘60000’ for the row where the ‘employee_id’ is ‘100’. The COMMIT statement is used to permanently save the changes in the database.

Example

UPDATE Employees
SET Department = 'Sales'
WHERE EmployeeID = 1;

Output

Query OK, 1 row affected (0.01 sec)

Explanation

The SQL UPDATE statement changes existing records in a table. In this example, it modifies the ‘Department’ column for the employee with an ‘EmployeeID’ of 1 to ‘Sales’. The output indicates that one row was successfully affected.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.