Database Reference
In-Depth Information
Summary
The
UPDATE
and
DELETE
statements are very useful for changing data in tables; they are
essential to managing a MySQL or MariaDB database. They have many possibilities for ef-
fecting changes to tables with ease. You can construct very complex SQL statements with
them to change precisely the data you want to change or to delete exactly the rows you
want to delete. However, it can be confusing and difficult at times. So be careful and learn
these SQL statements well.
If you're nervous at times about using the
UPDATE
and
DELETE
statements, it's because
you should be. You can change all of the rows in a table with one
UPDATE
statement, and
you can delete all of the rows in a table with one
DELETE
statement. On a huge database,
that could be thousands of rows of data changed or deleted in seconds. This is why good
backups are always necessary. Whenever using these two SQL statements, take your time
to be sure you're right before you execute them. While you're still learning especially, it
can be a good idea to make a duplicate of a table with its data using the
CREATE
TABLE...SELECT
statement before updating or deleting data. This SQL statement was
covered in
Essential Changes
.
This way if you make a major mistake, you can put the data
back as it was before you started.
Because of the problems you can cause yourself and others who will use the databases on
which you will work, practice using the
UPDATE
and
DELETE
statements. More than any
other chapter in this topic so far, you should make sure to complete the exercises in the
next section.