Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Delete Data in PostgreSQL
In PostgreSQL, deleting data from tables is a common operation that can be accomplished using various SQL commands. The most commonly used commands are `DELETE`, `TRUNCATE`, and `DROP`. Each serves a specific purpose and has different implications for how the data is removed from the database.
Using the DELETE Command
The `DELETE` command is used to remove rows from a table based on a specific condition. It's one of the most frequently used methods for deleting data. Below is the basic syntax:
DELETE FROM table_name WHERE condition;
For example, to delete a specific record from the "users" table where the user ID is 1, you would use the following SQL:
DELETE FROM users WHERE user_id = 1;
If you omit the `WHERE` clause, all rows in the table will be deleted, so use this command with caution.
Using the TRUNCATE Command
If you want to delete all rows from a table without logging individual row deletions, the `TRUNCATE` command is a more efficient option. This command also does not fire triggers or return the number of rows affected. Here's the syntax:
TRUNCATE TABLE table_name;
This command is faster than `DELETE` for large tables, but it cannot be rolled back if not executed within a transaction.
Using CASCADE
If your table has foreign key constraints and you want to delete a row that is referenced by other tables, you can use the `CASCADE` option. This ensures that the related rows in the dependent tables are also deleted automatically. Here is an example:
DELETE FROM parent_table WHERE id = 1 CASCADE;
Be cautious when using `CASCADE`, as it may result in the deletion of related data from other tables.
Important Considerations
- Always make sure to back up your data before performing any deletion operations.
- The `DELETE` command is slower than `TRUNCATE` because it logs each row deletion.
- When using `CASCADE`, make sure you're aware of the cascading effects on related tables.
In summary, PostgreSQL offers multiple ways to delete data, each with its own use cases. Understanding these commands will help you perform efficient and safe data removal in your databases.