SQL UPDATE |
SQL Sample Code - SQL UPDATE. |
| UPDATE table_name SET column1=value, column2=value2, column3=value3... WHERE specified_column=specified_value; |
Always include the WHERE clause otherwise all columns will be updated
See the examples section below for an explanation.
What does the SQL UPDATE statement do? |
The SQL UPDATE statement simply updates the value of a record in a table.
Sample – SQL UPDATE |
In this SQL sample we will use the database table ‘tblCompany’.
| Company_ID | CompanyName | Address | Town | Sales |
| 1 | SQL Sample | 1 Sample St | Hamburg | 10000 |
| 2 | SQL Code Land | 2 Code Rd | Hamburg | 20000 |
| 3 | Sample Code World | 66 SQL St | Curry | 18000 |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa | 17000 |
In the table above we want to update the SQL Reference Ltd record as they have moved Address from number 34 to 50 Reference St and their Sales figures have also changed.
Use the SELECT statement below:
| UPDATE tblCompany SET Address=’50 Reference St ' AND Sales=‘21000’ WHERE Address=’34 Reference St’ AND Sales=’17000’; |
The table of results will look like this:
| Company_ID | CompanyName | Address | Town | Sales |
| 1 | SQL Sample | 1 Sample St | Hamburg | 10000 |
| 2 | SQL Code Land | 2 Code Rd | Hamburg | 20000 |
| 3 | Sample Code World | 66 SQL St | Curry | 18000 |
| 4 | SQL Reference Ltd | 50 Reference St | Pisa | 21000 |
Related SQL Sample Code:
|
|