Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Update Data in Postgres


    Data changes. In some applications, it changes all the time! In this lesson we’ll learn how to update a single record or many at once.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Now let's update our data. Let's say we're going to update our movies. We're going to set the count stars equal to one. Let's also select all from movies after that. We'll see every single movie got updated where its count stars are now one because we didn't limit this in any way. If we want to, we can use the where clause here.

    Let's say the name is "Kill Bill." I'm sure people were sad that I rated that solely before. Let's give it a five star. Name doesn't exist. That's a mistake I made here. Let's say where title equals Kill Bill.

    It's important to notice that we did in fact get this error. Title is Kill Bill. We have five stars now, that limited to just one movie and before we limit it to multiple movies or all of the movies. We can prove that we can limit to a subset of the movies here but more than one.

    Where the count stars is one, let's give this a three maybe. We see "Funny People" and "Blazing Saddles" now have three stars. Obviously, we can use any field here. If we want it to say where the release date is greater than, we keep using 1975, so this will be everything except Blazing Saddles.

    We'll set the count stars equal to zero. Pretty simple. That's a basic update. Remember we're always limiting to the where clause. Something that's helpful before you actually run an update is to look at what that's going to be. If we write this as a select statement, we can see which movies we're going to update before we actually issue the update command.