Home/Curriculum/Lesson 16
Advanced4 min read

Updating and Deleting Data

Understanding Data Modification

Relational databases are dynamic environments. While inserting data adds new records to a table, managing a database lifecycle heavily relies on modifying existing records and removing obsolete ones as business requirements change. SQL provides two distinct Data Manipulation Language statements for these tasks: UPDATE and DELETE. Because both commands permanently alter the data stored in your tables, you must use them with precision to avoid unintended data loss or corruption.

Updating Data

The UPDATE statement modifies one or more existing rows within a table. A standard update operation requires three components: the table name, the column to be modified along with its new value, and a filter condition to identify the specific rows to change.

You specify the new values using the SET clause.

POSTGRESQL CODE SNIPPET
UPDATE customers
SET email = 'newemail@example.com'
WHERE customer_id = 105;

In this example, the database engine locates the row where the identifier is 105 and updates the email column with the provided string. If you omit the WHERE clause, the database will unconditionally update the email column for every single row in the table, which is rarely the intended behavior and can cause massive data corruption.

Updating Multiple Columns

You can update multiple columns in a single statement by separating the column assignments with commas within the SET clause.

POSTGRESQL CODE SNIPPET
UPDATE employees
SET job_title = 'Senior Analyst', salary = 75000
WHERE employee_id = 402;

This query efficiently modifies two distinct fields for the specified employee in a single transaction. When updating numeric data types, you simply provide the raw mathematical value. Text and date fields, however, must be strictly enclosed in single quotes. Attempting to update a column with an incompatible data type will result in an execution error.

Removing Column Values

If you need to remove the data from a specific column without deleting the entire row, you do not use the DELETE statement. Instead, you use the UPDATE statement to set that specific column's value to NULL.

POSTGRESQL CODE SNIPPET
UPDATE customers
SET phone_number = NULL
WHERE customer_id = 105;

This operation preserves the customer's record but strips the existing phone number data from the row. This technique is only permitted if the table's underlying schema allows NULL values for that specific column.

Deleting Data

The DELETE statement removes entire rows from a database table. Unlike the update operation, which alters specific columns within a row, a delete operation eliminates the complete record.

POSTGRESQL CODE SNIPPET
DELETE FROM inventory
WHERE product_id = 'P-12';

This statement instructs the database engine to locate the row matching the specified product ID and remove it entirely. As with the update statement, the WHERE clause is strictly required to target specific records. If you execute a DELETE statement without a WHERE clause, the database will delete every row in the table, leaving you with an empty structural shell.

When working with relational databases, deleting data can sometimes trigger errors due to referential integrity. If you attempt to delete a record that is currently referenced by a foreign key in another table, the database system will block the deletion to prevent orphaned records.

Guidelines for Safe Modifications

Because modifying data can permanently alter your organization's records, you should follow strict safety guidelines when writing your queries.

  • Always write and test your WHERE clause using a SELECT statement first. This ensures you are targeting the exact rows you intend to modify before you execute the actual modification command.
  • Never execute an UPDATE or DELETE statement without a WHERE clause unless you are absolutely certain you want to modify or erase every row in the table.
  • Utilize primary key columns in your WHERE clauses whenever possible. Filtering by unique identifiers guarantees that you only affect the specific row you intend to change, preventing accidental modifications to rows that happen to share similar data.

Apply these modification techniques carefully to maintain accurate and up-to-date information within your database tables.

Advertisement