Sample Video Frame

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

Exercise 41: Updating with SQL

You now know the CR parts of CRUD, which leaves the Update and Delete operations. 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:

  • Start with UPDATE and the table you're going to update, in this case person.

  • Next use SET to say what columns should be set to what values. You can change as many columns as you want as long as you separate them with commas like first_name = "Zed", last_name = "Shaw".

  • Then specify a WHERE clause that gives a SELECT style set of tests to do on each row. When the UPDATE finds a match, it does the update and SETs the columns to how you specified.

Previous Lesson Next Lesson

Register for Learn More Python 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.