SQL Tutorial - UPDATE Query
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
An UPDATE statement in SQL can be used to update existing records in a table.
You can either update a single record, or multiple records in a table.
You can specify which records to update via a WHERE clause.
Syntax of UPDATE Statement is as follows:
UPDATE TABLENAME SET column1=value1, column2=value2... columnN=valueN
WHERE condition
Imagine we have table called CUSTOMER, with the following data:
FIRSTNAME
LASTNAME
AGE
COUNTRY
Rambo
Robert
25
Belgium
Mugambo
37
Norway
Nagashekar
Rao
47
India
We can use an UPDATE statement as follows.
UPDATE CUSTOMER SET LASTNAME='Kichidi' WHERE FIRSTNAME='Mugambo'
Result:
1 Row updated. Table contents:
FIRSTNAME
LASTNAME
AGE
COUNTRY
Rambo
Robert
25
Belgium
Mugambo
Kichidi
37
Norway
Nagashekar
Rao
47
India
Scenario 2: Update Customers from Belgium and Norway such that their age is 22
We can use a UPDATE statement as follows.
UPDATE CUSTOMER SET AGE=22 WHERE COUNTRY IN ('Belgium', 'Norway')
Result:
2 Rows updated. Table contents:
FIRSTNAME
LASTNAME
AGE
COUNTRY
Rambo
Robert
22
Belgium
Mugambo
Kichidi
22
Norway
Nagashekar
Rao
47
India
Note: if you want to update all the records in a TABLE, you can either exclude the WHERE clause or use WHERE 1=1.