Home » Tutorials » SQL » UPDATE Query

UPDATE Query

The SQL statement UPDATE is a crossbreed between INSERT and DELETE. It doesn’t exactly do either of them, but it has similar properties of each. UPDATE searches for whatever record or records that satisfy its WHERE condition. Then the update statement overwrites each record’s columns that it was told to. The way the developers created the SQL update statement was brilliant and much better than the INSERT statement. Remember INSERT had those two separate lines in parentheses. Take a look at how much better UPDATE looks:

Updating SQL Records

Our table before:

idusernamepasswordbirthday
1bobdole32secretP1984-06-01
2rustyMeerkatdigholes1995-09-15
Example
UPDATE table_name
SET username = ‘rustyNails’, password=’hammers’
WHERE username = ‘rustyMeerkat’
Result
idusernamepasswordbirthday
1bobdole32secretP1984-06-01
2rustyNailshammers1995-09-15

Boom! You just changed a username and password. A highly insecure way to do it, but for our purposes, it will do. First, we start off by declaring the state with UPDATE to tell SQL were going to change some existing data. Next, we use the keyword SET followed by a column name set to a value. Then, you can use a comma and repeat that for as many columns as you want to update in those records.

Oddly enough, we tell SQL which records to update at the very end with our WHERE condition. Dreams and hearts have been broken by this organization. Don’t ever, ever, ever leave off the WHERE at the end unless you want to update every record. That was one of the saddest few seconds of my life. I left off the where statement at work while putting it into production, and watched in slow motion as the data was overwritten. Luckily, we have an pretty awesome backup system. Please never ever do it and always remember to back up your data before attempting data changing queries.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "UPDATE Query". After Hours Programming. Accessed on March 16, 2024. https://www.afterhoursprogramming.com/tutorial/sql/update-query/.

  • Stewart, Suzy. "UPDATE Query". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/sql/update-query/. Accessed 16 March, 2024.

  • Stewart, Suzy. UPDATE Query. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/sql/update-query/.



Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.