Wednesday, 1 October 2014

Update records in a table

UPDATE query is used to update records in a table.

Syntax
UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];

id firstName lastName salary mailId
1 Krishna Ananda 100000 krishna@krishna.com
2 Arjun Dhanunjay 50000 arjun@arjun.com
3 Ptr Ptr 25000 ptr@ptr.com

1. Update the firstName as 'Madhav', where firstName is 'Krishna'.
mysql> UPDATE employee SET firstName="Madhav" WHERE firstName="Krishna";
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from employee;
+----+-----------+-----------+--------+---------------------+
| id | firstName | lastName  | salary | mailId              |
+----+-----------+-----------+--------+---------------------+
|  1 | Madhav    | Ananda    | 100000 | krishna@krishna.com |
|  2 | Arjun     | Dhanunjay |  50000 | arjun@arjun.com     |
|  3 | Ptr       | Ptr       |  25000 | ptr@ptr.com         |
+----+-----------+-----------+--------+---------------------+
3 rows in set (0.00 sec)

2. Update firstName as jaideep, lastName as Geera, where firstName is ptr.
mysql> UPDATE employee SET firstName="jaideep", lastName="Geera" WHERE firstName="Ptr";
Query OK, 1 row affected (0.09 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from employee;
+----+-----------+-----------+--------+---------------------+
| id | firstName | lastName  | salary | mailId              |
+----+-----------+-----------+--------+---------------------+
|  1 | Madhav    | Ananda    | 100000 | krishna@krishna.com |
|  2 | Arjun     | Dhanunjay |  50000 | arjun@arjun.com     |
|  3 | jaideep   | Geera     |  25000 | ptr@ptr.com         |
+----+-----------+-----------+--------+---------------------+
3 rows in set (0.00 sec)

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment