SQL Truncate Table With Examples

SQL Truncate Table

Assume a case when we want to have the database but we would like to delete the data of the table. In such cases, TRUNCATE command is used to delete just the data from the table. The table structure will remain as it is.

Syntax for SQL TRUNCATE

We will try to understand how the TRUNCATE command works in the following databases.

  1. MySQL
  2. PostgreSQL
  3. SQL Server

MySQL Truncate Table

Let us first create a Teacher table and we will insert data in the table as follows.

Create Table: –

Insert Data: –

Let us validate if the table creation and data insertion worked fine using the following query.

 

SQL_Insert_into_select

Table After SQL Insert

Now we will TRUNCATE the data of the table using the following command.

 

MySQL-Truncate-Table

MySQL Truncate Table

We will recheck if the data still exists in the table using the following query.

 

MySQL-Truncate-Table

MySQL Table After Truncate

PostgreSQL Truncate Table

We will create the same table in PostgreSQL table

Create Table: –

Let us validate if the table creation and data insertion worked fine using the following query.

 

PostgreSQL-Table-after-Insert

PostgreSQL Table After Insert

Now we will TRUNCATE the data of the table using the following command.

 

PostgreSQL Truncate Table

We will recheck if the data still exists in the table using the following query.

 

PostgreSQL-Table-after-Insert

PostgreSQL Table After Truncate

SQL Server Truncate Table

We have already created the table and have inserted the data using following commands.

Create Table: –

Insert Data: –

Let us validate if the table creation and data insertion worked fine using the following query.

 

SQL-Server-Table-after-Insert

SQL Server Table After Insert

Now we will TRUNCATE the data of the table using the following command.

 

SQL Server Truncate Table

We will recheck the data in the table using the following query.

SQL Server Table After Truncate

By admin

Leave a Reply

%d bloggers like this: