SQL SET Keyword
The SET keyword in SQL is used within the UPDATE statement to specify the new values for the columns you want to modify in a table.
SET: Definition and Usage
The SET clause comes after the UPDATE keyword and before the WHERE clause (if you're updating specific rows). It's crucial for defining which columns are being changed and to what new values.
Important Note about WHERE Clauses
Always, always use a WHERE clause with your UPDATE statements! If you omit the WHERE clause, the UPDATE statement will modify *every single row* in the table. This can lead to significant data loss if not handled carefully.
Syntax
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Examples
Updating a Specific Row
This updates the 'ContactName' and 'City' for the customer with 'CustomerID' = 1.
Syntax
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;
Output
The 'ContactName' and 'City' fields for the customer with 'CustomerID' 1 are updated.
(The row for CustomerID 1 in the Customers table 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' changed to 'Juan'.
(Multiple rows in the Customers table will be updated)