The UPDATE Statement
The UPDATE statement is used to update existing records in a table.SQL UPDATE Syntax
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
SET column1=value, column2=value2,...
WHERE some_column=some_value
SQL UPDATE Example
The "Persons" table:P_Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Hansen | Ola | Timoteivn 10 | Sandnes |
2 | Svendson | Tove | Borgvn 23 | Sandnes |
3 | Pettersen | Kari | Storgt 20 | Stavanger |
4 | Nilsen | Johan | Bakken 2 | Stavanger |
5 | Tjessem | Jakob |
We use the following SQL statement:
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'
P_Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Hansen | Ola | Timoteivn 10 | Sandnes |
2 | Svendson | Tove | Borgvn 23 | Sandnes |
3 | Pettersen | Kari | Storgt 20 | Stavanger |
4 | Nilsen | Johan | Bakken 2 | Stavanger |
5 | Tjessem | Jakob | Nissestien 67 | Sandnes |
SQL UPDATE Warning
Be careful when updating records. If we had omitted the WHERE clause in the example above, like this: UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
SET Address='Nissestien 67', City='Sandnes'
P_Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Hansen | Ola | Nissestien 67 | Sandnes |
2 | Svendson | Tove | Nissestien 67 | Sandnes |
3 | Pettersen | Kari | Nissestien 67 | Sandnes |
4 | Nilsen | Johan | Nissestien 67 | Sandnes |
5 | Tjessem | Jakob | Nissestien 67 | Sandnes |
No comments:
Post a Comment