UPDATE and DELETE: Examples with MySQL – Beginner Series.

Continuing with the MySQL Beginner Series Basics, this blog post will briefly visit both the UPDATE and DELETE SQL Data Manipulation Commands. Be sure to visit the MySQL Categories folder in the sidebar for all the posts in the series so far.

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

I will be using Xubuntu Linux 16.04.2 LTS (Xenial Xerus) and MySQL 5.7.20 for these exercises.


milivoj-kuhar-543074
unsplash-logoMilivoj Kuhar

Target Goals

  • UPDATE a row of data in the target table.
  • DELETE a row of data from the target table.

UPDATE is used to change or modify a row, or rows of data.
Basic single-table syntax to UPDATE a single record can resemble the below piece of code:

UPDATE table_name
SET column_name(s)
WHERE some_conditional [optional]
;

Important!
The WHERE clause is optional. However, if omitted, the UPDATE will modify, all rows of the table.


The DELETE statement is used to remove records from a table.
Basic single-table syntax can be:

DELETE FROM table_name
WHERE some_conditional [optional]
;

Important!
The WHERE clause is optional but does decide which rows, if any, to DELETE. Without it, all rows are removed!


Putting UPDATE Into Practice

A congratulation is in order. My (fictitious) friend, Jennifer Kelly has gotten married and I need to change or UPDATE her last name in the friends table.
First let’s get all of her current information stored as of now in the table with this SELECT query:

mysql> SELECT * FROM friends
    -> WHERE f_first_name = 'Jennifer'
    -> AND f_last_name = 'Kelly';
+-----------+--------------+-------------+--------------+-------+------------+
| friend_id | f_first_name | f_last_name | f_cell_phone | f_age | f_bday     |
+-----------+--------------+-------------+--------------+-------+------------+
|         4 | Jennifer     | Kelly       | 979-191-6652 |    39 | 1978-10-16 |
+-----------+--------------+-------------+--------------+-------+------------+
1 row in set (0.00 sec)

How can we UPDATE her last name to ‘Timie’?
By using the UPDATE command.
Have a look below.

mysql> UPDATE friends
-> SET f_last_name = 'Timie'
-> WHERE f_first_name = 'Jennifer';

That will do the trick right?
Well, yes it will. Probably not how you imagined though.
Check this out:

mysql> SELECT * FROM friends
    -> WHERE f_first_name = 'Jennifer';
+-----------+--------------+-------------+--------------+-------+------------+
| friend_id | f_first_name | f_last_name | f_cell_phone | f_age | f_bday     |
+-----------+--------------+-------------+--------------+-------+------------+
|         4 | Jennifer     | Kelly       | 979-191-6652 |    39 | 1978-10-16 |
|        17 | Jennifer     | Chelsey     | 443-479-4548 |    37 | 1980-10-30 |
+-----------+--------------+-------------+--------------+-------+------------+
2 rows in set (0.00 sec)

Hmm…
Apparently the friends table holds two records, both having the f_first_name value Jennifer.
Yikes!!!
That command could have easily updated both of these records (not the intended purpose).


A fantastic idea I’ve read about numerous times is, when performing an UPDATE or DELETE, always query what you intend to change or discard, with a SELECT statement first.
I strive to try to make this a personal practice.


To go through with this change in the table, for the intended record, all I have to do is slightly modify that original investigative SELECT query.
Here is one way to accomplish this UPDATE:

mysql> UPDATE friends
-> SET f_last_name = 'Timie'
-> WHERE f_first_name = 'Jennifer'
-> AND f_last_name = 'Kelly';
ERROR 1142 (42000): UPDATE command denied to user 'this_user'@'localhost' for table 'friends'

Recall from part 1 in the series, this_user was allowed to have SELECT and INSERT privileges only.
Time to change that.
Logging in with the root account, (not shown), I accomplish this with the below GRANT command:

mysql> GRANT UPDATE, DELETE
-> ON projects.*
-> TO 'this_user'@'localhost';
Query OK, 0 rows affected (0.07 sec)

Back to the task at hand, now that this_user has the needed permissions, I will re-run that same command:

mysql> UPDATE friends
-> SET f_last_name = 'Timie'
-> WHERE f_first_name = 'Jennifer'
-> AND f_last_name = 'Kelly';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Confirming the change with this SELECT query,

mysql> SELECT * FROM friends
    -> WHERE f_first_name = 'Jennifer'
    -> AND f_last_name = 'Timie';
+-----------+--------------+-------------+--------------+-------+------------+
| friend_id | f_first_name | f_last_name | f_cell_phone | f_age | f_bday     |
+-----------+--------------+-------------+--------------+-------+------------+
|         4 | Jennifer     | Timie       | 979-191-6652 |    39 | 1978-10-16 |
+-----------+--------------+-------------+--------------+-------+------------+
1 row in set (0.00 sec)

The record now has the updated f_last_name value.


To receive notifications for the latest post from “Digital Owl’s Prose” via email, please subscribe by clicking the ‘Click To Subscribe!’ button in the sidebar!
Be sure and visit the “Best Of” page for a collection of my best blog posts.


A Quick DELETE

Let’s choose a record to DELETE from the friends table:

mysql> SELECT * FROM friends;
+-----------+--------------+--------------+--------------+-------+------------+
| friend_id | f_first_name | f_last_name  | f_cell_phone | f_age | f_bday     |
+-----------+--------------+--------------+--------------+-------+------------+
|         1 | John         | Tom          | 318-767-8818 |    32 | NULL       |
|         2 | John         | Roberts      | 555-867-5309 |    36 | 1981-04-17 |
|         3 | Billy        | Bogus        | 777-383-2828 |    44 | 1973-08-03 |
|         4 | Jennifer     | Timie        | 979-191-6652 |    39 | 1978-10-16 |
|         5 | Mark         | Harmon       | 567-733-0833 |    28 | 1989-06-27 |
|         6 | Jeremy       | Hall         | 777-272-1717 |    40 | 1977-02-07 |
|         7 | Josh         | Remey        | 727-213-7119 |    40 | 1977-03-17 |
|         8 | Jenny        | Marcum       | 543-179-1548 |    37 | 1980-09-27 |
|         9 | Mike         | Comb         | 118-972-1919 |    30 | 1987-10-05 |
|        10 | Thurmon      | Docker       | 393-392-1010 |    25 | 1992-08-19 |
|        11 | Milton       | Hammerfast   | 318-272-0797 |    41 | 1976-05-11 |
|        12 | Nikki        | Vernon       | 548-498-9949 |    28 | 1989-01-31 |
|        13 | Avis         | Pearl        | 509-292-8787 |    64 | 1953-07-04 |
|        14 | Josh         | Otwell       | 318-282-2075 |    41 | NULL       |
|        15 | Jeremain     | Foreman      | 777-172-1917 |    40 | 1977-05-17 |
|        16 | Jimmy        | Hunwell      | 525-213-5119 |    40 | 1977-07-27 |
|        17 | Jennifer     | Chelsey      | 443-479-4548 |    37 | 1980-10-30 |
|        18 | Manny        | Combine      | 158-952-5915 |    30 | 1987-01-15 |
|        19 | Pervis       | Poser        | 193-312-1110 |    25 | 1992-04-29 |
|        20 | Misty        | Stedman      | 818-288-8787 |    41 | 1976-08-05 |
|        21 | Nikkoli      | Forrest      | 343-493-9343 |    28 | 1989-09-21 |
|        22 | Hertz        | Ruby         | 539-392-3783 |    64 | 1953-04-04 |
|        23 | Richard      | Freeman      | 522-152-1917 |    19 | 1996-11-17 |
|        24 | Harry        | Hogwort      | 525-213-5519 |    44 | 1973-11-27 |
|        25 | Johhny       | Christianson | 453-459-4548 |    37 | 1980-11-30 |
|        26 | Jake         | Hammond      | 858-852-5985 |    37 | 1980-12-15 |
|        27 | Jarvis       | Polly        | 173-712-7170 |    20 | 1997-12-29 |
|        28 | Andy         | Stafford     | 616-286-8687 |    61 | 1956-12-05 |
|        29 | Harold       | Homeney      | 243-423-9223 |    38 | 1979-11-21 |
|        30 | Jamie        | Opal         | 139-312-1713 |    66 | 1951-11-04 |
|        31 | NULL         | NULL         | NULL         |  NULL | NULL       |
+-----------+--------------+--------------+--------------+-------+------------+
31 rows in set (0.01 sec)

The row with friend_id value of 31 looks like a good candidate.
How can we accomplish removing this record?

mysql> DELETE FROM friends
-> WHERE friend_id = 31;
Query OK, 1 row affected (0.14 sec)

And re-querying the table:

mysql> SELECT * FROM friends;
+-----------+--------------+--------------+--------------+-------+------------+
| friend_id | f_first_name | f_last_name  | f_cell_phone | f_age | f_bday     |
+-----------+--------------+--------------+--------------+-------+------------+
|         1 | John         | Tom          | 318-767-8818 |    32 | NULL       |
|         2 | John         | Roberts      | 555-867-5309 |    36 | 1981-04-17 |
|         3 | Billy        | Bogus        | 777-383-2828 |    44 | 1973-08-03 |
|         4 | Jennifer     | Timie        | 979-191-6652 |    39 | 1978-10-16 |
|         5 | Mark         | Harmon       | 567-733-0833 |    28 | 1989-06-27 |
|         6 | Jeremy       | Hall         | 777-272-1717 |    40 | 1977-02-07 |
|         7 | Josh         | Remey        | 727-213-7119 |    40 | 1977-03-17 |
|         8 | Jenny        | Marcum       | 543-179-1548 |    37 | 1980-09-27 |
|         9 | Mike         | Comb         | 118-972-1919 |    30 | 1987-10-05 |
|        10 | Thurmon      | Docker       | 393-392-1010 |    25 | 1992-08-19 |
|        11 | Milton       | Hammerfast   | 318-272-0797 |    41 | 1976-05-11 |
|        12 | Nikki        | Vernon       | 548-498-9949 |    28 | 1989-01-31 |
|        13 | Avis         | Pearl        | 509-292-8787 |    64 | 1953-07-04 |
|        14 | Josh         | Otwell       | 318-282-2075 |    41 | NULL       |
|        15 | Jeremain     | Foreman      | 777-172-1917 |    40 | 1977-05-17 |
|        16 | Jimmy        | Hunwell      | 525-213-5119 |    40 | 1977-07-27 |
|        17 | Jennifer     | Chelsey      | 443-479-4548 |    37 | 1980-10-30 |
|        18 | Manny        | Combine      | 158-952-5915 |    30 | 1987-01-15 |
|        19 | Pervis       | Poser        | 193-312-1110 |    25 | 1992-04-29 |
|        20 | Misty        | Stedman      | 818-288-8787 |    41 | 1976-08-05 |
|        21 | Nikkoli      | Forrest      | 343-493-9343 |    28 | 1989-09-21 |
|        22 | Hertz        | Ruby         | 539-392-3783 |    64 | 1953-04-04 |
|        23 | Richard      | Freeman      | 522-152-1917 |    19 | 1996-11-17 |
|        24 | Harry        | Hogwort      | 525-213-5519 |    44 | 1973-11-27 |
|        25 | Johhny       | Christianson | 453-459-4548 |    37 | 1980-11-30 |
|        26 | Jake         | Hammond      | 858-852-5985 |    37 | 1980-12-15 |
|        27 | Jarvis       | Polly        | 173-712-7170 |    20 | 1997-12-29 |
|        28 | Andy         | Stafford     | 616-286-8687 |    61 | 1956-12-05 |
|        29 | Harold       | Homeney      | 243-423-9223 |    38 | 1979-11-21 |
|        30 | Jamie        | Opal         | 139-312-1713 |    66 | 1951-11-04 |
+-----------+--------------+--------------+--------------+-------+------------+
30 rows in set (0.01 sec)

Just like that, gone.
That’s how simple it is to DELETE a record, permanently, from a table.


Final Thoughts

UPDATE and DELETE are mighty SQL commands. It goes without saying, they should both be used with care due to their ability to permanently change or remove records.
This blog post highlights basic usage only, without any safeguards or failsafe in place (TRANSACTIONS, COMMIT, ROLLBACK come to mind). It is not intended as the “Be all, end all” reference for either command, just a mild introductory to get you started.
I encourage you to explore the official MySQL 5.7 Online Manual for all available options.


A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well. Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive notifications for the latest post from “Digital Owl’s Prose” via email, please subscribe by clicking the ‘Click To Subscribe!’ button in the sidebar!
Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell

Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.


Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided are performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

One thought on “UPDATE and DELETE: Examples with MySQL – Beginner Series.

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.