Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python MySQL - Insert into Table
Next article icon

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 theUPDATEqueryaffects only the data,not the structure of the table.

Syntax

UPDATE tablename
SET column1 = "new_value", column2 = "new_value"
WHERE condition;

DATABASE IN USE:

python-mysql-update

Example 1: Update the Age of a Student

In this example, we'll update the age of a student named "Rishi Kumar" in theSTUDENTtable. We will change his age to23:

Python
importmysql.connector# Connecting to the Databasemydb=mysql.connector.connect(host="localhost",database="College",user="root",# Replace with your MySQL usernamepassword="your_password"# Replace with your MySQL password)# Create a cursor objectcs=mydb.cursor()# SQL query to update the age of a studentstatement="UPDATE STUDENT SET AGE = 23 WHERE Name = 'Rishi Kumar'"# Execute the update statementcs.execute(statement)# Commit the changes to the databasemydb.commit()# Disconnecting from the databasemydb.close()

Output:

python-mysql-update1
MySQL Update

Explanation:

  • UPDATEquery 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 withmydb.close() to release resources.

Example 2: Correcting the Spelling of a Student's Name

In this example, we will correct the spelling of the student's name from "SK Anirban" to "S.K. Anirban" in theSTUDENTtable:

Python
importmysql.connector# Connecting to the Databasemydb=mysql.connector.connect(host="localhost",database="College",user="root",password="your_password"# Replace with your MySQL password)# Create a cursor objectcs=mydb.cursor()# SQL query to update the name of the studentstatement="UPDATE STUDENT SET Name = 'S.K. Anirban' WHERE Name = 'SK Anirban'"# Execute the update statementcs.execute(statement)# Commit the changes to the databasemydb.commit()# Disconnecting from the databasemydb.close()

Output:

python-mysql-update2

Explanation:

  • UPDATEquery changes the name "SK Anirban" to "S.K. Anirban".
  • mydb.commit() ensures the changes are saved.
  • The connection is closed after the operation.

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp