Databases

Lesson 5 - UPDATE and DELETE

Periodic table

Lesson Overview

Make revisions to the Periodic table as it was in 1900 by using the UPDATE and DELETE keywords.

Modifying Data with UPDATE and DELETE

The UPDATE statement allows you to modify existing records in a table, while the DELETE statement allows you to remove records. Both are powerful commands that should be used with caution.

Task: Update the Periodic Table

The periodic table from 1900 is missing some modern discoveries. Let's update it:

  1. View the periodic table before updates:Periodic table before
  2. Update an element's atomic weight:
    UPDATE elements SET atomic_weight = 1.008 WHERE element = 'Hydrogen'
  3. Delete elements that were later disproved:
    DELETE FROM elements WHERE element = 'Ether'
  4. View the periodic table after updates:Periodic table after
⚠️ Important: Always Use WHERE Clauses

When using UPDATE or DELETE, always include a WHERE clause to specify which records to modify. Without it, you'll update or delete ALL records in the table!

-- DANGEROUS! This deletes ALL records
DELETE FROM elements;

-- SAFE: This deletes only specific records
DELETE FROM elements WHERE element = 'Ether';

Lesson Summary

In this lesson, we've learned how to use UPDATE to modify existing records and DELETE to remove records from a database. These operations are essential for maintaining and correcting data in your databases.