Tutorialspoint

1 Answer
Sharon Christine

For this, use UPDATE and REPLACE() in MySQL. Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable(StudentName,StudentCountryName) values('John','US');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql select *from DemoTable;

This will produce the following output −

+-----------+-------------+--------------------+
| StudentId | StudentName | StudentCountryName |
+-----------+-------------+--------------------+
|         1 | John        | US                 |
+-----------+-------------+--------------------+
1 row in set (0.00 sec)

Following is the query to update multiple columns −

mysql> update DemoTable
   -> set StudentName=replace(StudentName,'John','Chris'),
   -> StudentCountryName=replace(StudentCountryName,'US','UK');
Query OK, 1 row affected (0.69 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Let us check the table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+-------------+--------------------+
| StudentId | StudentName | StudentCountryName |
+-----------+-------------+--------------------+
|         1 | Chris       | UK                 |
+-----------+-------------+--------------------+
1 row in set (0.00 sec)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.