SQL UPDATE Syntax
UPDATE table_name
SET column1=value, column2=value2,... WHERE some_column=some_value |
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
SQL UPDATE Example
The "Persons" table:
Id
|
LastName
|
FirstName
|
Address
|
City
|
1
|
Navis
|
Anto
|
Madras
|
TVL
|
2
|
Ji
|
Bala
|
Bombay
|
TVL
|
3
|
Christopher
|
Franklin
|
America
|
KK
|
4
|
Nilsen
|
Johan
|
Bakken 2
|
KK
|
5
|
Tjessem
|
Jakob
|
|
|
Now we want to update the person "Tjessem, Jakob" in the "Persons" table.
We use the following SQL statement:
UPDATE Persons
SET Address='Nissestien 67', City='TVL' WHERE LastName='Tjessem' AND FirstName='Jakob' |
The "Persons" table will now look like this:
Id
|
LastName
|
FirstName
|
Address
|
City
|
1
|
Navis
|
Anto
|
Madras
|
TVL
|
2
|
Ji
|
Bala
|
Bombay
|
TVL
|
3
|
Christopher
|
Franklin
|
America
|
KK
|
4
|
Nilsen
|
Johan
|
Bakken 2
|
KK
|
5
|
Tjessem
|
Jakob
|
Nissestien 67
|
TVL
|
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='TVL' |
The "Persons" table would have looked like this:
Id
|
LastName
|
FirstName
|
Address
|
City
|
1
|
Navis
|
Anto
|
Nissestien 67
|
TVL
|
2
|
Ji
|
Bala
|
Nissestien 67
|
TVL
|
3
|
Christopher
|
Franklin
|
Nissestien 67
|
TVL
|
4
|
Nilsen
|
Johan
|
Nissestien 67
|
TVL
|
5
|
Tjessem
|
Jakob
|
Nissestien 67
|
TVL
|
0 comments:
Post a Comment