SQL UPDATE Statement
The UPDATE
statement in SQL is used to modify existing data within a table. It allows you to change the values in one or more columns for specific rows.
UPDATE: Definition and Usage
UPDATE
is a fundamental command for maintaining and updating information in your database. It's crucial to always include a WHERE
clause to specify which rows should be modified. Otherwise, the UPDATE
statement will change *every row* in the table, which could have disastrous consequences.
Syntax
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Examples
Updating a Single Row
This example updates the contact name and city for a specific customer (CustomerID = 1). (This assumes a 'Customers' table with 'CustomerID', 'ContactName', and 'City' columns.)
Syntax
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;
Output
(The ContactName and City for the Customer with CustomerID 1 will be updated.)
Updating Multiple Rows
This updates the 'ContactName' for all customers from Mexico.
Syntax
UPDATE Customers
SET ContactName = 'Juan'
WHERE Country = 'Mexico';
Output
(All rows in the Customers table where Country is 'Mexico' will have their ContactName updated to 'Juan'.)