Set all the columns of a MySQL table to a particular value with a single query


Let us first create a table −

mysql> create table DemoTable
(
   ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientName varchar(40),
   ClientAge int,
   ClientCountryName varchar(40)
);
Query OK, 0 rows affected (0.57 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(ClientName,ClientAge,ClientCountryName) values('Chris',25,'US');
Query OK, 1 row affected (0.33 sec)
mysql> insert into DemoTable(ClientName,ClientAge,ClientCountryName) values('Bob',55,'UK');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable(ClientName,ClientAge,ClientCountryName) values('David',45,'AUS');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+------------+-----------+-------------------+
| ClientId | ClientName | ClientAge | ClientCountryName |
+----------+------------+-----------+-------------------+
|        1 | Chris      | 25        | US                |
|        2 | Bob        | 55        | UK                |
|        3 | David      | 45        | AUS               |
+----------+------------+-----------+-------------------+
3 rows in set (0.00 sec)

Here is the query to set all the columns of a MySQL table to a particular value −

mysql> update DemoTable
   set ClientName='Sam',ClientAge=48,ClientCountryName='AUS' where ClientId=2;
Query OK, 1 row affected (0.22 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 −

+----------+------------+-----------+-------------------+
| ClientId | ClientName | ClientAge | ClientCountryName |
+----------+------------+-----------+-------------------+
|        1 | Chris      |        25 | US                |
|        2 | Sam        |        48 | AUS               |
|        3 | David      |        45 | AUS               |
+----------+------------+-----------+-------------------+
3 rows in set (0.00 sec)
raja
Published on 10-Oct-2019 15:39:20
Advertisements