Python MySQL - Update Query Last Updated : 30 Jun, 2025 Comments Improve Suggest changes Like Article Like Report The UPDATE query in SQL is used to modify existing records in a table. It allows you to update specific columns' values in one or more rows of a table. It's important to note that the UPDATE query affects only the data, not the structure of the table.SyntaxUPDATE tablenameSET column1 = "new_value", column2 = "new_value"WHERE condition;tablename: The name of the table where the data will be updated.SET: Specifies the columns and their new values.WHERE: Determines which rows will be updated. If omitted, all rows in the table will be updated.DATABASE IN USE: Example 1: Update the Age of a StudentIn this example, we'll update the age of a student named "Rishi Kumar" in the STUDENT table. We will change his age to 23: Python import mysql.connector # Connecting to the Database mydb = mysql.connector.connect( host="localhost", database="College", user="root", # Replace with your MySQL username password="your_password" # Replace with your MySQL password ) # Create a cursor object cs = mydb.cursor() # SQL query to update the age of a student statement = "UPDATE STUDENT SET AGE = 23 WHERE Name = 'Rishi Kumar'" # Execute the update statement cs.execute(statement) # Commit the changes to the database mydb.commit() # Disconnecting from the database mydb.close() Output:MySQL UpdateExplanation:UPDATE query modifies the AGE column for the student with the name "Rishi Kumar".mydb.commit() ensures that the changes are saved to the database.connection is closed with mydb.close() to release resources.Example 2: Correcting the Spelling of a Student's NameIn this example, we will correct the spelling of the student's name from "SK Anirban" to "S.K. Anirban" in the STUDENT table: Python import mysql.connector # Connecting to the Database mydb = mysql.connector.connect( host="localhost", database="College", user="root", password="your_password" # Replace with your MySQL password ) # Create a cursor object cs = mydb.cursor() # SQL query to update the name of the student statement = "UPDATE STUDENT SET Name = 'S.K. Anirban' WHERE Name = 'SK Anirban'" # Execute the update statement cs.execute(statement) # Commit the changes to the database mydb.commit() # Disconnecting from the database mydb.close() Output:Explanation:UPDATE query changes the name "SK Anirban" to "S.K. Anirban".mydb.commit() ensures the changes are saved.The connection is closed after the operation. Comment More info V vanshikagoyal43 Follow Improve Article Tags : Python Python-mySQL Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 5 min read Recursion in Python 6 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 5 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like