Python PostgreSQL – Delete Data
In this article, we are going to see how to delete data in tables from PostgreSQL using pyscopg2 module in Python. In PostgreSQL, DELETE TABLE is used to delete the data in the existing table from the database. It removes table definition and all associated data, indexes, rules, triggers, and constraints for that table. If the particular table doesn’t exist then it shows an error.
Here, we are using the accounts table for demonstration.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Now let’s drops this table, for we will use will psycopg2 module to connect the PostgreSQL and execute the SQL query in cursor.execute(query) object.
Example 1: Deleting all the data from the table
Here we are deleting all the table data using DELETE clause.
Syntax: DELETE FROM table_name
Example 2: Using where clause
Where clause is used as a condition statement in SQL, it is used to filter records.
Syntax: DELETE FROM table_name FROM WHERE condition