SQL UPDATE Statement
- The UPDATE statement updates data values in a database.
- UPDATE can update one or more records in a table.
- Use the WHERE clause to UPDATE only specific records.
The SQL UPDATE syntax
The general syntax is:
- UPDATE table-name SET column-name = value, column-name = value, ...
To limit the number of records to UPDATE append a WHERE clause:
- UPDATE table-name SET column-name = value, column-name = value, ... WHERE condition
SQL UPDATE Examples
1) Normal update or without where condition update
mysql> update
person set age=25;
Query OK, 4 rows
affected (0.02 sec)
Rows matched: 5
Changed: 4 Warnings: 0
mysql> update
person set sex='M';
Query OK, 1 row
affected (0.02 sec)
Rows matched: 5
Changed: 1 Warnings: 0
mysql> select
*from person;
+-----+--------+------+----------------------+------+------------+
| sno | name | age
| email | sex | phone |
+-----+--------+------+----------------------+------+------------+
| 1 | subham |
25 | NULL | M | NULL |
| 2 | nunu |
25 | NULL | M | NULL |
| 3 | chayan |
25 | NULL | M | NULL |
| 4 | debo |
25 | NULL | M | NULL |
+-----+--------+------+----------------------+------+------------+
2) update with where condition
mysql> update
person set sex='F' where sno='4';
Query OK, 1 row
affected (0.01 sec)
Rows matched: 1
Changed: 1 Warnings: 0
mysql> select
*from person;
+-----+--------+------+----------------------+------+------------+
| sno | name | age
| email | sex | phone |
+-----+--------+------+----------------------+------+------------+
| 1 | subham |
25 | NULL | M | NULL |
| 2 | nunu |
25 | NULL | M | NULL |
| 3 | chayan |
25 | NULL | M | NULL |
| 4 | debo |
25 | NULL | F | NULL |
+-----+--------+------+----------------------+------+------------+
5 rows in set (0.00
sec)
Comments
Post a Comment