Sometimes we may want to create the same table in a database without the data in it by using a different name or we want to create the table with a different name with the data.
There are several methods for this.
In this article, we will do the move operation by creating a new table with data and without data.
Let’s connect to the database and see the table we’re copying.
1 |
\dt+ |
You can use the following command to create the table above with a different name in the same database without data.
1 |
CREATE TABLE d1_new(new_table_name) AS TABLE d1(current_table_name)WITH NO DATA; |
After running the above command, when we check with command “\dt+” we see that it was created without data in it.
We can use the following command to copy the table with the data.
1 |
CREATE TABLE d1_replica(new_table_name) AS TABLE d1(current_table_name); |
The table was transferred with the data.
In addition, we can create a new table by taking part of the records in the table.
1 |
CREATE TABLE d1_condition AS SELECT * FROM d1 WHERE s BETWEEN 28056 AND 30000; |
As shown below, our table named d1_condition has been created.