Updating Tables SQL & Starwars memes ⭐️ 🚀 ⚔️

Embrace the Darkside of Pepsi-Cola Products 🥤

Introduction

Hello fellow readers! Welcome to another exciting blog about updating tables in SQL. As per usual, I’ll break down the statement and explain what each part of it does. This allows us to gain more insight into what we are learning. I’ll also be including a bunch of fun star wars memes to keep things fun! If you’re new to writing SQL statements. No worries, I have some handcrafted blogs, which should get you up to speed on this topic.

My Cool Blogs about SQL 😎 
Sadly, I can relate… 😢

Let’s Dive in 🐋

Imagine we have a Ships of Starwars database. It holds critical information about all the super cool ships we see in the movies and tv shows. Yet as curators of this super cool website, we see a valid contributor has entered erroneous data about the Imperial Era Tie Fighter( you know the one from New Hope). This data could clearly upset our devoted userbase, so we jump on this problem like a team of Jawas striping the razor crest for parts(you know, our old friend Mando’s ship).

Let take a look at our Tie Fighter data…

Imperial_Tie_Fighter

Yeah, this definitely has faster acceleration 🙄 , more like “4,100”. Good thing we caught this. People would have reached for their blasters! Now how do we fix this careless error? How about a super cool SQL statement that updates!

It should look like this …

UPDATE Star_fighters
SET max_acceleration = 4,100 <-- 👾
WHERE id = 7

Nice, but what does this actually do? Like most SQL statements, we start with a keyword. Since we want to change the data that leaves in a particular table, we use the “UPDATE” keyword, followed by the table's name. We then use “SET” to indicate we would like to change this column to “4,100”. Easy right?

Finally, we use the “WHERE” clause to indicate which row we would like to change using the Primary Key. 👍

Our code

Cool but…. how do I update multiple columns? Let’s say we wanted to update all Mandalorian-made ships under the gun_ships table. We need to please our fans, and so we make the prudent decision to change the armaments value from the ridiculous term“space lasers” to the more respectable term “laser cannons.

Let’s write an SQL statement like this….”

UPDATE gun_ships
SET armaments= laser cannons
WHERE origin = Mandalorian <-- Note the Where clause 👾

We must use the WHERE clause here. If not included, we are telling the database to change ALL gunships’ armaments to laser cannons. Using the origin attribute allows us to target ALL MANDALORIAN gunships.

Conclusion

Ooooh… that’s not good. 🚀

Congrats, you know how to Update with SQL! 🥳 I can sense the force is strong with you. It’s a simple statement but one you really want to understand before attempting to update. Please Stay tuned 📺 for the next article on DELETING with SQL. It should be fun!

Happy coding 🧑‍🚀

Links

SQL update - https://www.w3schools.com/sql/sql_update.asp

Career Changer, Software Engineer & Web Developer