Latest updates

[5]

SQL UPDATE Statement




The UPDATE statement is used to update records in a table. Or to update existing records in a table.

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

“Persons” TABLE:
P_Id
LastName
FirstName
Address
City
1
Suhail
Osmani
Maharaj Nagar
Sitapur
2
Maxwel
Tom
Biswan
Sitapur
3
Rahi
Ayon
Okhla
Delhi
4
Nyla
Johan
Bakken 2
Delhi
5
Thoria
Jakob


Now we want to update the person "Thoria, Jakob" in “Persons” TABLE.
We use the following SQL statement:
UPDATE Persons SET Address='New Road', City='Sitapur' WHERE LastName='Thoria' AND FirstName='Jakob'
“Persons” TABLE will now look like this:
P_Id
LastName
FirstName
Address
City
1
Suhail
Osmani
Maharaj Nagar
Sitapur
2
Maxwel
Tom
Biswan
Sitapur
3
Rahi
Ayon
Okhla
Delhi
4
Nyla
Johan
Bakken 2
Delhi
5
Thoria
Jakob
New Road
Sitapur

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='New Road', City='Sitapur'
“Persons” TABLE would have looked like this:
P_Id
LastName
FirstName
Address
City
1
Suhail
Osmani
New Road
Sitapur
2
Maxwel
Tom
New Road
Sitapur
3
Rahi
Ayon
New Road
Sitapur
4
Nyla
Johan
New Road
Sitapur
5
Thoria
Jakob
New Road
Sitapur


SQL UPDATE Statement SQL UPDATE Statement Reviewed by Admin on 04:11:00 Rating: 5

No comments:

Sora Templates