Sample Video Frame
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.
View Source file sql/ex9.sql Only
UPDATE person SET first_name = "Hilarious Guy"
WHERE first_name = "Zed";
UPDATE pet SET name = "Fancy Pants"
WHERE id=0;
SELECT * FROM person;
SELECT * FROM pet;
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 caseperson
.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 likefirst_name = "Zed", last_name = "Shaw"
.Then specify a
WHERE
clause that gives aSELECT
style set of tests to do on each row. When theUPDATE
finds a match, it does the update andSETs
the columns to how you specified.
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.