Update a table based on StudentId value in MySQL?


Let us first create a table −

mysql> create table DemoTable633 (StudentId char(2) not null primary key,StudentName varchar(100));
Query OK, 0 rows affected (1.06 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable633 values('10','Chris');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable633 values('20','Sam');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable633 values('30','David');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable633;

This will produce the following output −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
| 10        | Chris       |
| 20        | Sam         |
| 30        | David       |
+-----------+-------------+
3 rows in set (0.00 sec)

Let us now update the above table based on StudentID using UPDATE −

mysql> update DemoTable633 set StudentName='Bob' where StudentId='20';
Query OK, 1 row affected (0.13 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Let us check the table records once again −

mysql> select *from DemoTable633;

This will produce the following output −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
| 10        | Chris       |
| 20        | Bob         |
| 30        | David       |
+-----------+-------------+
3 rows in set (0.00 sec)

Updated on: 23-Aug-2019

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements