Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

PostgreSQL TRUNCATE TABLE statement

10 September 2020

PostgreSQL TRUNCATE TABLE statement

PostgreSQL TRUNCATE TABLE statement is used to remove (truncate) all records from a table or set of tables in PostgreSQL. It performs the same function as the DELETE operator, without the WHERE operator.

Warning: If you truncate a table, the TRUNCATE TABLE operator cannot be overridden if it is not in a transaction that was not committed.

The syntax for TRUNCATE TABLE statement in PostgreSQL

TRUNCATE [TABLE] [ONLY] [schema_name.]table_name [*] [,...]
[ RESTART IDENTITY | CONTINUE IDENTITY ]
[ CASCADE | LIMIT] ;

Parameters and arguments of the statement

  • TABLE – Optional. The truncate operator will behave identically regardless of whether you specified a TABLE or not.
  • ONLY – Optional. If specified, the only table_name will be truncated. If not specified, table_name and all its child tables will be truncated.
  • schema_name – The name of the scheme to which the table belongs.
  • Table_name – The table that you want to be truncated.
  • * – Optional. If specified, table_name and all his child tables will be truncated.
  • RESTART IDENTITY – Optional. If specified, all sequences in the truncated tables will be reset.
  • CONTINUE IDENTITY – Optional. If specified, all the sequences in the truncated tables will be left and will not be reset. This is the default behavior.
  • CASCADE – Optional. If specified, all tables that have an external key reference to table_name will also be truncated.
  • RESTRICT – Optional. If specified, all tables that have an external key reference to table_name will not be truncated if they were not listed in the TRUNCATE TABLE operator. This is the default behavior.

Note:

  • Before you can truncate the table, you must have the necessary privileges such as TRUNCATE.
  • DELETE triggers for a table do not start during truncation.

You cannot truncate a table that is referenced by a foreign key unless the TRUNCATE command lists all tables in relation to the foreign key.

The entire truncated row storage will be restored without VACUUM.

In PostgreSQL, the truncation of a table is a quick way to remove records from a table because you don’t need to scan the table to do so. Truncation of a table is also much easier than removing a table and recreating it.

An example of using the TRUNCATE TABLE operator in PostgreSQL

For example:

TRUNCATE ONLY products;

In this example, the table with the name of the products will be truncated and all records from this table will be deleted.

This would be equivalent to the next DELETE operator in PostgreSQL

DELETE FROM products;

Both of these operators will remove all data from the products table, and since the ONLY keyword was used in the TRUNCATE command, no descendant tables will be truncated. It is always safer to specify the ONLY keyword to avoid accidentally truncating the descendant tables.

Several tables

Next, let’s look at how to truncate several tables at once.
For example:

TRUNCATE ONLY products, inventory;

In this example, the tables with the names of products and inventory will be truncated. Again, we have enabled the ONLY keyword so that no descendant tables are truncated.

Resetting columns of identifiers

By default, when you truncate a table, the ID columns continue numbering from where it stopped. You can specify PostgreSQL to automatically reset all identifier columns when you truncate a table by enabling the RESTART IDENTITY option.
For example:

TRUNCATE ONLY inventory

RESTART IDENTITY;

In this example, the identifier columns in the inventory table will return to their original values. This is useful if you have a primary key field that you want to restart with 1.

PostgreSQL Tutorial for Beginners – PostgreSQL TRUNCATE TABLE Statement

 
Tags: , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...