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
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…
A Binary Search Tree (BST) is a type of binary tree that satisfies the following…