Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Truncate Tables in PostgreSQL

Use the TRUNCATE TABLE command to delete all the data from the specified table in PostgreSQL Database.

  • To DELETE command will also delete all data from the table, but it may take more time if a table being deleted has a large amount of data. The TRUNCATE command deletes large amount of data faster than the DELETE command.
  • The TRUNCATE command deletes all rows without scanning the rows, so it's faster than the DELETE statement.
  • TRUNCATE statement frees up space occupied by rows immediately rather than performing subsequent VACUUME operations.

Syntax:

TRUNCATE [TABLE] <table_name>

The following command will remove all the data from the employee table.

Example: Delete All Rows from Table
TRUNCATE TABLE employee;
--or 
TRUNCATE employee;

Along with removing all data from a table, to reset identity column values, use the RESTART IDENTITY parameter.

The following command will remove all data from the employee table and reset the sequence associated with emp_id column.

Example: Reset ID Column
TRUNCATE TABLE employee RESTART IDENTITY;

If a table you are trying to truncate a table that has a foreign key reference from another table, then by default TRUNCATE statement will not allow you to truncate a table and will raise an error.

To remove data from the parent table and all of its child tables (having foreign key references with parent table), you need to use the CASCADE option along with the TRUNCATE statement.

For example, the following command will remove all rows from the department table and all other tables that have references to the department table through foreign key constraints.

Example: Cascade
TRUNCATE TABLE department CASCADE;

We can also use RESTART IDENTITY with CASCADE to reset the identity column value.

Example: Cascade with Reset ID Column
TRUNCATE TABLE department RESTART IDENTITY CASCADE;

Note: the TRUNCATE with CASCADE option should be used very carefully, as you might end up removing data from the tables that you are not intended to.

Remove data from multiple tables:

PostgreSQL allows using the TRUNCATE command to delete all data from multiple tables. Specify the comma-separated table names with the TRUNCATE TABLE command.

TRUNCATE TABLE employee, department;
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.