Sample Video Frame

Created by Zed A. Shaw Updated 2024-02-17 04:54:36

Exercise 9: Updating Data

You now know the CRD parts of CRUD, and I just need to teach you the Update part to round out the core of SQL. As with all the other SQL commands the UPDATE command follows a format similar to DELETE but it changes the columns in rows instead of deleting them.

UPDATE person SET first_name = 'Hilarious Guy'
    WHERE first_name = 'Zed';

UPDATE pet SET name = 'Fancy Pants'
    WHERE id=0;

SELECT * FROM person;

In the above code I'm changing my name to "Hilarious Guy", since that's more accurate. And to demonstrate my new moniker I renamed my Unicorn to "Fancy Pants". He loves it.

This shouldn't be that hard to figure out, but just in case I'm going to break the first one down:

What You Should See

I'm resetting the database with my code.sql script and then running this:

$ sqlite3 mydata.db < code.sql
# ... output cut ...
$ sqlite3 -header -column -echo mydata.db < ex9.sql 
UPDATE person SET first_name = "Hilarious Guy"
    WHERE first_name = "Zed";
UPDATE pet SET name = "Fancy Pants"
    WHERE id=0;

SELECT * FROM person;
id          first_name     last_name   age       
----------  -------------  ----------  ----------
0           Hilarious Guy  Shaw        37        

id          name         breed       age         dead      
----------  -----------  ----------  ----------  ----------
0           Fancy Pants  Unicorn     1000        0         
1           Gigantor     Robot       1           0         

I've done a bit of reformatting by adding some newlines but otherwise your output should look like mine.

Study Drills

Previous Lesson Next Lesson

Register for Learn SQL the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.