Update only a single column value in MySQL

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable1605
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(20),
   -> StudentCountryName varchar(20)
   -> );
Query OK, 0 rows affected (0.48 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1605(StudentName,StudentCountryName) values('Adam','AUS');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable1605(StudentName,StudentCountryName) values('John','US');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable1605(StudentName,StudentCountryName) values('Bob','UK');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1605;

This will produce the following output −

+-----------+-------------+--------------------+
| StudentId | StudentName | StudentCountryName |
+-----------+-------------+--------------------+
|         1 | Adam        | AUS                |
|         2 | John        | US                 |
|         3 | Bob         | UK                 |
+-----------+-------------+--------------------+
3 rows in set (0.00 sec)

Following is the query to update only a single column value in MySQL −

mysql> update DemoTable1605 set StudentName='Chris' where StudentCountryName='UK';
Query OK, 1 row affected (0.09 sec)
Rows matched: 1  Changed: 1 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1605;

This will produce the following output −

+-----------+-------------+--------------------+
| StudentId | StudentName | StudentCountryName |
+-----------+-------------+--------------------+
|         1 | Adam        | AUS                |
|         2 | John        | US                 |
|         3 | Chris       | UK                 |
+-----------+-------------+--------------------+
3 rows in set (0.00 sec)
raja
Published on 16-Dec-2019 12:10:21
Advertisements