Wednesday, April 23, 2025

MySQL - UPDATE Data in Table

The UPDATE statement is used to modify existing data in a table. 


mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name      | Lastname | Telephone |
+----+-----------+----------+-----------+
|  1 | John      | Smith    |    111333 |
|  2 | Anastasia | TheGreat |    555777 |
|  3 | Samantha  | Fox      |    456456 |
| 10 | Larry     | TheFirst |    555888 |
| 11 | Bill      | Gates    |    444888 |
| 12 | Bill      | Gates    |    444888 |
| 13 | John      | Smith    |    111333 |
| 14 | Anastasia | Some     |    555444 |
+----+-----------+----------+-----------+
8 rows in set (0.00 sec)

mysql>

This query updates the Telephone field of the row in the friends table where id is equal to 1: 


mysql> UPDATE friends SET Telephone=000000
    -> WHERE (id=1);
Query OK, 1 row affected (0.11 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql>

The new value for the Telephone field is set to 000000.

The query returns a message indicating that one row was affected by the update, indicating that the update was successful. 


mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name      | Lastname | Telephone |
+----+-----------+----------+-----------+
|  1 | John      | Smith    |         0 |
|  2 | Anastasia | TheGreat |    555777 |
|  3 | Samantha  | Fox      |    456456 |
| 10 | Larry     | TheFirst |    555888 |
| 11 | Bill      | Gates    |    444888 |
| 12 | Bill      | Gates    |    444888 |
| 13 | John      | Smith    |    111333 |
| 14 | Anastasia | Some     |    555444 |
+----+-----------+----------+-----------+
8 rows in set (0.00 sec)

mysql>

This MySQL query updates the "Telephone" field of the row with "id=1" in the "friends" table to 999999: 


mysql> UPDATE friends SET Telephone=999999
    -> WHERE (id=1);
Query OK, 1 row affected (0.09 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql>

The UPDATE statement is used to modify or change existing records in a table.

In this case, the SET clause is used to specify the column to be updated, which is the "Telephone" field. The WHERE clause is used to identify the specific row to be updated, which is the row with "id=1".

When the query is executed, it updates the row with the specified ID and sets the "Telephone" field to the new value of 999999. The output indicates that one row was matched and changed, with no warnings. 


mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name      | Lastname | Telephone |
+----+-----------+----------+-----------+
|  1 | John      | Smith    |    999999 |
|  2 | Anastasia | TheGreat |    555777 |
|  3 | Samantha  | Fox      |    456456 |
| 10 | Larry     | TheFirst |    555888 |
| 11 | Bill      | Gates    |    444888 |
| 12 | Bill      | Gates    |    444888 |
| 13 | John      | Smith    |    111333 |
| 14 | Anastasia | Some     |    555444 |
+----+-----------+----------+-----------+
8 rows in set (0.00 sec)

mysql>

mysql> UPDATE friends SET Lastname='Gatezzz'
    -> WHERE (Name='Bill' AND Lastname='Gates');
Query OK, 2 rows affected (0.10 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql>

mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name      | Lastname | Telephone |
+----+-----------+----------+-----------+
|  1 | John      | Smith    |    999999 |
|  2 | Anastasia | TheGreat |    555777 |
|  3 | Samantha  | Fox      |    456456 |
| 10 | Larry     | TheFirst |    555888 |
| 11 | Bill      | Gatezzz  |    444888 |
| 12 | Bill      | Gatezzz  |    444888 |
| 13 | John      | Smith    |    111333 |
| 14 | Anastasia | Some     |    555444 |
+----+-----------+----------+-----------+
8 rows in set (0.00 sec)

mysql>

mysql> UPDATE friends SET Telephone=111111;
Query OK, 8 rows affected (0.11 sec)
Rows matched: 8  Changed: 8  Warnings: 0

mysql>

mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name      | Lastname | Telephone |
+----+-----------+----------+-----------+
|  1 | John      | Smith    |    111111 |
|  2 | Anastasia | TheGreat |    111111 |
|  3 | Samantha  | Fox      |    111111 |
| 10 | Larry     | TheFirst |    111111 |
| 11 | Bill      | Gatezzz  |    111111 |
| 12 | Bill      | Gatezzz  |    111111 |
| 13 | John      | Smith    |    111111 |
| 14 | Anastasia | Some     |    111111 |
+----+-----------+----------+-----------+
8 rows in set (0.00 sec)

mysql>

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...