Difference between revisions of "Updating and Deleting MySQL Data"
m (Text replacement - "<google>ADSDAQBOX_FLOW</google>" to "<htmlet>adsdaqbox_flow</htmlet>") |
m (Text replacement - "<google>BUY_MYSQL</google>" to "<htmlet>mysql</htmlet>") |
||
Line 8: | Line 8: | ||
− | < | + | <htmlet>mysql</htmlet> |
Revision as of 22:13, 1 February 2016
Previous | Table of Contents | Next |
Inserting Data into a MySQL Database | Retrieving Data From a MySQL Database |
Purchase and download the full PDF and ePub versions of this MySQL eBook for only $9.99 |
Once data has been added to a MySQL database table, it is invariably necessary to delete or update some or all of that data. In this chapter we will cover the issue of updating and deleting data in table rows.
Updating Database Data
The UPDATE statement requires a few items of information in order to operate. Firstly, it needs the name of the table that is to be updated. Secondly, it needs to know which columns are to be updated, and the new values for those columns. Finally, it needs information about which specific rows are to be updated (controlled by the WHERE keyword). The WHERE keyword is probably the most important part of the statement to remember. Without a WHERE condition, the update will be applied to every row in the table.
As with most things in life, this is best demonstrated through an example. The following SQL statement is designed to change the product description column of a row in a table where the prod_id column is equal to 12134.
UPDATE products SET prod_desc = 'Size 10 Red Shoe' WHERE prod_id = 12134;
In the above example, the SET section of the statement identifies the column to be updated and the value to which it is to be changed. The WHERE section of the statement identifies the row, or rows to be updated.
It is also possible to update multiple columns with a single UPDATE statement. This requires the use of multiple column = value expressions in UPDATE statement. For example, we can easily extend our previous example to update the prod_name and prod_desc columns of our products database table:
UPDATE products SET prod_desc = 'Size 10 Red Shoe', prod_name = 'Big Red Shoes' WHERE prod_id = 12134;
Ignoring Update Errors
If an UPDATE statement is designed to update multiple rows, and an error is encountered attempting to update some of those rows, the entire update is canceled and any rows that had been changed are reverted to their original values. Use of the IGNORE keyword will cause the update to continue, simply skipping any rows which presented a problem:
UPDATE IGNORE products SET prod_desc = 'Size 10 Red Shoe', prod_name = 'Big Red Shoes' WHERE prod_id = 12134;
Deleting Database Data
Either all rows, or only specific rows in a table can be deleted using the SQL DELETE statement. Once again, this statement can, and indeed should, be used in conjunction with the WHERE clause. Omission of the WHERE keyword will result in all rows in a table being deleted. In fact, it is often even recommended that the criteria to be used for the WHERE filter first be tested with a SELECT statement to ensure it does exactly what you intended.
The following SQL statement removes a row from our products table where the prod_id field is equal 12134:
DELETE FROM products WHERE prod_id = 12134;
Keep in mind when using the DELETE statement to remove all rows in a table that only the rows are removed, and that the table itself remains. To delete the table and its contents use the DROP TABLE SQL statement:
DROP TABLE products;
Once again, use this statement is caution. Once a table has been deleted it is gone for good (unless you have performed a recent backup).
Summary
In this chapter of MySQL Essentials we explored how to update and delete rows in a table. We have also learned about the importance of using WHERE keyword to control which rows get updated or deleted when the statement is executed.
<google>BUY_MYSQL_BOTTOM</google>
Previous | Table of Contents | Next |
Inserting Data into a MySQL Database | Retrieving Data From a MySQL Database |