MySQL insert a value to specific row and column


Let us first create a table −

mysql> create table DemoTable1569
   -> (
   -> StudentId varchar(10),
   -> StudentName varchar(20)
   -> );
Query OK, 0 rows affected (3.05 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1569 values('John_12','John Smith');
Query OK, 1 row affected (0.38 sec)
mysql> insert into DemoTable1569 values('David_321','Carol Taylor');
Query OK, 1 row affected (0.50 sec)
mysql> insert into DemoTable1569 values('David_989','David Miller');
Query OK, 1 row affected (0.24 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1569;

This will produce the following output

+-----------+--------------+
| StudentId | StudentName  |
+-----------+--------------+
| John_12   | John Smith   |
| David_321 | Carol Taylor |
| David_989 | David Miller |
+-----------+--------------+
3 rows in set (0.00 sec)

Here is the query to insert a value to specific row and column.

mysql> update DemoTable1569 set StudentId='Carol_321' where StudentName='Carol Taylor';
Query OK, 1 row affected (0.26 sec)
Rows matched: 1  Changed: 1 Warnings: 0

Let us check the table records once again.

mysql> select * from DemoTable1569;

This will produce the following output

+-----------+--------------+
| StudentId | StudentName  |
+-----------+--------------+
| John_12   | John Smith   |
| Carol_321 | Carol Taylor |
| David_989 | David Miller |
+-----------+--------------+
3 rows in set (0.00 sec)

Updated on: 13-Dec-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements