Using SQL UPDATE statement
SQL UPDATE statement is another very important statement used to update the value of an existing record in the database table.
+----+--------+---------+------+-----------+ | ID | FNAME | LNAME | AGE | COUNTRY | +----+--------+---------+------+-----------+ | 1 | AKSHAY | KUMAR | 12 | INDIA | | 2 | JONTY | RHODES | 40 | CHINA | | 3 | JOHNY | DEPP | 45 | INDIA | | 4 | JESSIE | PINKMAN | 25 | AUSTRALIA | | 5 | JESSIE | JACKSON | 22 | CANADA | | 6 | NICOLE | KIDMAN | 50 | CANADA | | 7 | GEORGE | CLOONEY | NULL | USA | +----+--------+---------+------+-----------+
SQL UPDATE statement syntax and example -
Suppose we want to change the country name for CUSTOMER NICOLE (ID - 6), we will write the following statement -UPDATE CUSTOMER SET COUNTRY = 'USA' WHERE ID = 6;
mysql> SELECT * FROM CUSTOMER; +----+--------+---------+------+-----------+ | ID | FNAME | LNAME | AGE | COUNTRY | +----+--------+---------+------+-----------+ | 1 | AKSHAY | KUMAR | 12 | INDIA | | 2 | JONTY | RHODES | 40 | CHINA | | 3 | JOHNY | DEPP | 45 | INDIA | | 4 | JESSIE | PINKMAN | 25 | AUSTRALIA | | 5 | JESSIE | JACKSON | 22 | CANADA | | 6 | NICOLE | KIDMAN | 50 | USA | | 7 | GEORGE | CLOONEY | NULL | USA | +----+--------+---------+------+-----------+Here we can see that country column value has been updated to USA. Most of the time UPDATE statement is used with where clause to specify the row for which we want to update the record.
Note : If you will not use where clause you will end up updating all the records in the table.
-- Warning ! All records will get updated UPDATE CUSTOMER SET COUNTRY = 'USA';
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment