Update an entire row in MySQL?

MySQLMySQLi Database

To update an entire row in MySQL, use UPDATE command. You need to know the primary key column. The syntax is as follows to update an entire row.

UPDATE yourTableName SET yourColumnName1 = ’yourValue1’ ,yourColumnName2 = ’yourValue2’ ,
   yourColumnName3 = ’yourValue3’ ,.......................N
   WHERE yourPrimaryKeyColumnName = yourValue;

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table UpdateEntireRowDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> Age int,
   -> Marks int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.74 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into UpdateEntireRowDemo(Name,Age,Marks) values('Sam',23,78);
Query OK, 1 row affected (0.32 sec)

mysql> insert into UpdateEntireRowDemo(Name,Age,Marks) values('Mike',21,99);
Query OK, 1 row affected (0.16 sec)

mysql> insert into UpdateEntireRowDemo(Name,Age,Marks) values('Carol',26,80);
Query OK, 1 row affected (0.11 sec)

mysql> insert into UpdateEntireRowDemo(Name,Age,Marks) values('John',22,71);
Query OK, 1 row affected (0.16 sec)

mysql> insert into UpdateEntireRowDemo(Name,Age,Marks) values('Bob',29,89);
Query OK, 1 row affected (0.16 sec)

mysql> insert into UpdateEntireRowDemo(Name,Age,Marks) values('David',25,68);
Query OK, 1 row affected (0.20 sec)

mysql> insert into UpdateEntireRowDemo(Name,Age,Marks) values('Larry',31,91);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from UpdateEntireRowDemo;

The following is the output −

+----+-------+------+-------+
| Id | Name  | Age  | Marks |
+----+-------+------+-------+
|  1 | Sam   |   23 |    78 |
|  2 | Mike  |   21 |    99 |
|  3 | Carol |   26 |    80 |
|  4 | John  |   22 |    71 |
|  5 | Bob   |   29 |    89 |
|  6 | David |   25 |    68 |
|  7 | Larry |   31 |    91 |
+----+-------+------+-------+
7 rows in set (0.00 sec)

The following is the query to update an entire row in MySQL. Here, I am going to update the row with Id 5.

The query is as follows −

mysql> update UpdateEntireRowDemo
   -> set Name = 'James',Age = 19,Marks = 78
   -> where Id = 5;
Query OK, 1 row affected (0.12 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Now you can check the entire row is updated. The query is as follows −

mysql> select *from UpdateEntireRowDemo where Id = 5;

The following is the output −

+----+-------+------+-------+
| Id | Name  | Age  | Marks |
+----+-------+------+-------+
|  5 | James | 19   |    78 |
+----+-------+------+-------+
1 row in set (0.00 sec)

Let us see all the records from the table.

mysql> select *from UpdateEntireRowDemo;

The output displays that the entire row is successfully updated:

+----+-------+------+-------+
| Id | Name  | Age  | Marks |
+----+-------+------+-------+
|  1 | Sam   |   23 |    78 |
|  2 | Mike  |   21 |    99 |
|  3 | Carol |   26 |    80 |
|  4 | John  |   22 |    71 |
|  5 | James |   19 |    78 |
|  6 | David |   25 |    68 |
|  7 | Larry |   31 |    91 |
+----+-------+------+-------+
7 rows in set (0.00 sec)
raja
Published on 01-Feb-2019 17:31:52
Advertisements