Update Data in Postgres

Brett Cassette
InstructorBrett Cassette
Share this video with your friends

Social Share Links

Send Tweet

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.

[00:00] 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.

[00:27] 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.

[00:41] 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.

[01:01] 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.

[01:25] 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.