VOOZH about

URL: https://www.geeksforgeeks.org/python/python-mysql-delete-query/

โ‡ฑ Python MySQL - Delete Query - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python MySQL - Delete Query

Last Updated : 28 Jul, 2025

In MySQL, the DELETE query is used to remove rows from a table based on a specific condition defined in the WHERE clause. Without a WHERE clause, the entire table will be emptied.

Syntax

DELETE FROM table_name WHERE condition;

  • table_name: The name of the table from which records are to be deleted.
  • condition: The condition that specifies which rows should be deleted.

Make sure you have created a database and an active MySQL server, if not then visit here to learn about it: Python MySQL CRETE Table

Deleting Data Based on a Condition

Hereโ€™s a program to connect to a MySQL database, create a table, insert data, and delete a row based on a specific condition (e.g., deleting a student by their roll number).

Explanation:

  • We create a table STUDENT in the geeks database and insert multiple student records.
  • The DELETE query removes the record where the ROLL number matches 1706256.
  • Changes are committed to the database with dataBase.commit(), ensuring that the deletion is saved.

Deleting Data Using a Parameterized Query

We can also use a parameterized DELETE query to remove records from the STUDENT table.

Syntax:

DELETE FROM TABLE_NAME WHERE ATTRIBUTE_NAME = ATTRIBUTE_VALUE

Below is a program to delete a query from the table in the database. 

Output:๐Ÿ‘ python-mysql-delete
Explanation:

  • We used a parameterized query for deletion to avoid SQL injection attacks.
  • The record with ROLL = 1706256 is deleted from the table.
Comment
Article Tags:
Article Tags: