Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

PostgreSQL Create Table: Create the structure of a table dup_countries similar to countries


3. Write a sql statement to create the structure of a table dup_countries similar to countries.

Sample Solution:

Code:

CREATE TABLE dup_countries AS 
(SELECT * 
FROM countries)  
WITH NO DATA;

Output:

postgres=# CREATE TABLE dup_countries AS (
postgres(# SELECT *
postgres(# FROM countries)
postgres-# WITH NO DATA;
SELECT 0

Here is the command to see the structure of the created table :

postgres=# \d dup_countries
           Table "public.dup_countries"
    Column    |         Type          | Modifiers
--------------+-----------------------+-----------
 country_id   | character varying(3)  |
 country_name | character varying(45) |
 region_id    | numeric(10,0)         |

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a SQL statement to create a simple table countries, including columns country_id,country_name and region_id which already exist.
Next: Write a SQL statement to create a duplicate copy of countries table, including structure and data by name dup_countries.

What is the difficulty level of this exercise?