Categories: python

Delete Query – MySQL

You must use the DELETE FROM statement to delete records from a MySQL table. To remove specific records, use the WHERE clause in conjunction with it.

Syntax:

Following is the syntax of the DELETE query in MYSQL −

DELETE FROM table_name [WHERE Clause]

Note: that the WHERE clause in the preceding syntax is used to specify the condition to pinpoint a specific record that you want to delete. If you do not specify a condition, all the table’s records will be deleted.

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

import mysql.connector

# Connecting to the Database
mydb = mysql.connector.connect(
host ='localhost',
database ='School',
user ='root',
)

cursor = db.cursor()
## defining the Query
query = "DELETE FROM students WHERE ROLL = 21"

## executing the query
cursor.execute(query)

## final step to tell the database that we have changed the table data
db.commit()

If the above code runs without an error then it means that the row with roll = 21 is deleted successfully.

Note: also read about Update Query-MySQL

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

5 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

6 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

11 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

11 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

12 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 year ago