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 😎 SQL Select- https://ineedmoreplates.medium.com/sql-select-tables-ninja-turtles-d655adc9d5a8SQL Create - https://ineedmoreplates.medium.com/create-sql-tables-panda-memes-8f10973a3859SQL Insert - https://ineedmoreplates.medium.com/sql-insert-into-a-table-parrot-memes-a355232e81b1
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_FighterLength = 6.3Width = 6.4Height = 7.5Depth = 7.5 Max_acceleration. 3,100 <-- I sense a distrubance in the force 👾 MGLT = 100 Max_atmospheric_speed = 1,200Maneuver_rating. 96–100Engine = “P-s4 twin ion engines (rated 150 KTU) P-w401 ion maneuvering jets (2)”
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 …
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 codeUPDATE Star_fighters
SET max_acceleration = 4,100
WHERE id = 7-------------------------------------------------------------syntaxUPDATE table_name
SET column1 = value1, column2 = value2, ...
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….”
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.
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 🧑🚀
SQL update - https://www.w3schools.com/sql/sql_update.asp