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
%load_ext sql
%env DATABASE_URL=postgresql://itversity_sms_user:sms_password@localhost:5432/itversity_sms_db
%%sql
SELECT user_id,
user_password
FROM users
# 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()
%%sql
SELECT user_id, user_password FROM users