Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Database Operations: UPDATE and DELETE in MySQL Using Python - Database Operations

Database Operations: UPDATE and DELETE in MySQL Using Python

Posted on December 24, 2024April 5, 2026 By Rishabh saini No Comments on Database Operations: UPDATE and DELETE in MySQL Using Python

Database Operations

Working with databases involves several fundamental operations: Create, Read, Update, and Delete (CRUD). Among these, the UPDATE and DELETE operations are crucial for maintaining and managing data effectively. In this post, we’ll explore how to perform these operations in MySQL using Python, accompanied by real-world examples.

Update Operation

To change the data in one or more table columns, use     the UPDATE-SET command. For precise updates, you must use the WHERE clause to target specific rows; otherwise, all rows in the table will be updated.

Database Operations


Download New Real Time Projects :-Click here

SQL Query for Update

The following SQL command updates the name column for the employee with id = 110:

UPDATE Employee SET name = 'alex' WHERE id = 110;

Python Example: Updating a Record

Below is a Python script that demonstrates the update operation using the mysql.connector library.

import mysql.connector  

# Create the connection object  
myconn = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="google",
    database="PythonDB"
)  

# Creating the cursor object  
cur = myconn.cursor()  

try:  
    # Updating the name of the employee whose id is 110  
    cur.execute("UPDATE Employee SET name = 'alex' WHERE id = 110")  
    myconn.commit()  # Save changes  
    print("Record updated successfully.")  
except:  
    myconn.rollback()  # Rollback in case of error  
    print("Error occurred while updating the record.")  

myconn.close()  

Key Points:

  • A WHERE clause should always be used to prevent inadvertent modifications.
  • Handle exceptions to ensure data integrity.
  • Commit changes using myconn.commit() to make them permanent.

Delete Operation

To eliminate particular records from a table, use the DELETE FROM statement. Like the UPDATE operation, a WHERE clause is critical to prevent accidental deletion of all rows.

Database Operations

SQL Query

 for Delete

The following SQL command deletes the employee record with id = 110:

DELETE FROM Employee WHERE id = 110;

Python Example: Deleting a Record

Here’s a Python script demonstrating the delete operation:

import mysql.connector  

# Create the connection object  
myconn = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="google",
    database="PythonDB"
)  

# Creating the cursor object  
cur = myconn.cursor()  

try:  
    # Deleting the employee details whose id is 110  
    cur.execute("DELETE FROM Employee WHERE id = 110")  
    myconn.commit()  # Save changes  
    print("Record deleted successfully.")  
except:  
    myconn.rollback()  # Rollback in case of error  
    print("Error occurred while deleting the record.")  

myconn.close()  

Key Points:

  • The WHERE clause can be used to target particular rows.
  • Commit changes to ensure the deletion is finalized.
  • Always validate inputs to avoid unintentional deletions.

Best Practices for UPDATE and DELETE

  1. Backup Data: Always take a backup of critical data before performing updates or deletions.
  2. Test Queries: Run queries in a test environment to confirm the desired outcome.
  3. Use Transactions: Employ transactions (myconn.commit() and myconn.rollback()) to manage operations safely.
  4. Sanitize Inputs: Protect against SQL injection by using parameterized queries.

PHP PROJECT:- CLICK HERE


database operations sql
database operations in mysql
database operations examples
types of database operations
database operations in dbms
database operations in python
database operations in sap abap
database operations crud
database
crud
database operations in sql
database operations pdf

Post Views: 816
Python Tags:apache cassandra database operations, c++ database, CRUD Operations, crud operations in database, database, database management system, database operation, database operations, database php, databases, javafx database operations, nosql database operations, php delete operation in mysql database, relational databases, simplify your database operations, update database operation in visual c++ mysql, what is database

Post navigation

Previous Post: Fetcher App Using Python and Tkinter
Next Post: AI in Web Development

More Related Articles

Python Tkinter Label Python Tkinter Label: A Complete Guide Python
Python Inheritance: A Deep Dive - Python Inheritance Python Inheritance: A Deep Dive Python
How to Render web page into Django How to render web page into Django Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. Python Decorators: A Comprehensive Guide
  2. How to Calculate Distance between Two Points using GEOPY
  3. Rock, Paper, Scissors Game
  4. Fetcher App Using Python and Tkinter
  5. How to Install Django: Step-by-Step Guide
  6. Python Tkinter Canvas: A Guide to Structured Graphics in Python

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Blog Site In PHP And MYSQL With Source Code || Best Project
  9. Online Bike Rental Management System Using PHP and MySQL
  10. E learning Website in php with Free source code
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Real-Time Medical Queue & Appointment System with Django
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code

Most Viewed Posts

  • Top Large Language Models in 2025 (8,616)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,225)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,875)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme