The SQL UPDATE general syntax looks like this:
UPDATE Table1 SET Column1 = Value1, Column2 = Value2
WHERE Some_Column = Some_Value
The SQL UPDATE clause changes the data in already existing database row(s) and usually we need to add a conditional SQL WHERE clause to our SQL UPDATE statement in order to specify which row(s) we intend to update.
If we want to update the Mr. Adam Red's date of birth to '5/10/1974' in our Company database table
FirstName | LastName | Email | DOB | Phone |
Scott | Armstrong | Armstrong.yahoo.com | 2/4/1968 | 887-676-2321 |
Adam | Red | red@himail.com | 4/4/1978 | 792 343-2422 |
Santo | Harry | Harry@livingin.org | 5/24/1978 | 416 323-3232 |
Jackson | Jimmy | Jimmy@supermail.co.uk | 20/10/1980 | 416 323-8888 |
we need the following SQL UPDATE statement:
UPDATE Company
SET DOB = '5/10/1974'
WHERE LastName = 'Red' AND FirstName = 'Adam'
If we don’t specify a WHERE clause in the SQL expression above, all Company' DOB will be updated to '5/10/1974', so be careful with the SQL UPDATE command usage.
We can update several database table rows at once, by using the SQL WHERE clause in our UPDATE statement. For example if we want to change the phone number for all Company with last name Armstrong (we have 2 in our example Company table), we need to use the following SQL UPDATE statement:
UPDATE Company
SET Phone = '626 555-5555'
WHERE LastName = 'Armstrong'
After the execution of the UPDATE SQL expression above, the Company table will look as follows:
No comments:
Post a Comment