Deleting Data from Table

Let us understand how data is deleted using DELETE statement.

  • Delete statement will have optional WHERE clause where we specify the condition to filter for rows that need to be deleted.

  • As part of the WHERE clause generally we will have indexed column for performance reasons.

One need to have decent database and SQL skills to be comfortable with all types of application development. Feel free to Master SQL using Postgresql as target database using this course or playlist.

%run 05_function_get_database_connection.ipynb
Copy to clipboard
%load_ext sql
Copy to clipboard
%env DATABASE_URL=postgresql://itversity_sms_user:sms_password@localhost:5432/itversity_sms_db
Copy to clipboard
%%sql 

SELECT user_id, 
    user_password 
FROM users
Copy to clipboard
# DELETE FROM users WHERE user_password IS NULL OR user_id = 4

cursor = sms_connection.cursor()
query = ("""
    DELETE FROM users
    WHERE user_password IS NULL OR user_id = %s
""")

cursor.execute(query, (4,))
sms_connection.commit()
sms_connection.close()
Copy to clipboard
%%sql 

SELECT user_id, user_password FROM users
Copy to clipboard