Best way to update a single column in a MySQL table?

MySQLMySQLi Database

To update a single column, use UPDATE and SET as in the below syntax −

update yourTableName set yourColumnName=yourValue;

Let us first create a table −

mysql> create table DemoTable1873
     (
     Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
     FirstName varchar(20)
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1873(FirstName) values('John');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1873(FirstName) values('Adam');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1873(FirstName) values('David');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1873(FirstName) values('Sam');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1873 ;

This will produce the following output −

+----+-----------+
| Id | FirstName |
+----+-----------+
|  1 | John      |
|  2 | Adam      |
|  3 | David     |
|  4 | Sam       |
+----+-----------+
4 rows in set (0.00 sec)

Here is the query to update a single column in a MySQL table −

mysql> update DemoTable1873 set FirstName='Robert';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1873 ;

This will produce the following output −

+----+-----------+
| Id | FirstName |
+----+-----------+
|  1 | Robert    |
|  2 | Robert    |
|  3 | Robert    |
|  4 | Robert    |
+----+-----------+
4 rows in set (0.00 sec)
raja
Published on 27-Dec-2019 06:11:45
Advertisements