From the course: SQL Essential Training

Updating data - SQL Tutorial

From the course: SQL Essential Training

Updating data

“

- Now the UPDATE statement is used to modify existing data in a table. Now the update is generally used with the WHERE clause. The WHERE clause is used to specify the particular role of data that you want to update. Now without the WHERE clause the update statement will actually update every single row in a table. So including the WHERE clause is pretty important when it comes to updating your data. Now let's take a look at how we would update the record that we just placed in our table, which was to update the artist table with the artist Bob Marley. The management at WSDA music has just sent word that they actually made an error. Instead of Bob Marley they actually wanted to add his son Damien Marley. So let's actually see how we would update our record and perform this update. Now the first step we would want to do is go back to our browse data tab and remind ourselves what actually was that artist ID that was added when we added our new record. And if we look down here we see that Bob Marley is having an artist's ID of 276. So with this, let's go back to our execute SQL tab and construct our DML statement. The first thing we'd need to do is indicate the keyword UPDATE. And now we want say we want update the artist table. The next keyword we need is SET. S-E-T And now we want to set the actual field name of the artist table to the updated value. And this we know is the artist's name and we want to update this particular field with a text value. And again, when it comes to text we put that between a pair of single quotes and as management indicated we want to put Damien Marley. Now at this point if we were to run this statement it would actually update every single name in our artist table with the name Damien Marley. Which why, again I'll stress that including the WHERE clause is very important when it comes to updating data. So here, let's include the WHERE. And we want to say where the artist ID is equal to 276. Let's remind ourselves again that that number indeed is 276 for Bob Marley. Let's jump back over to execute SQL and run this statement. Alright, once again, we don't get a result. We do see in our messages pane the message that we want to see which is a successful completion of our query. Now we can verify again by going over to our browse data jump back to record 276. And now Bob has been updated to Damien Marley. Well done.

Contents