I have a PostgreSQL database with lots of tables of the same structure, 36 columns in total:
CREATE TABLE some_schema.some_table (
id integer NOT NULL DEFAULT nextval('some_schema.id_seq'::regclass),
col2,
col3,
col4,
[...],
col35,
mi_prinx integer NOT NULL DEFAULT nextval('some_schema.mi_prinx_seq'::regclass),
CONSTRAINT some_table_pkey PRIMARY KEY (mi_prinx)
)
In many instances I will have to insert records from another table with the same structure:
INSERT INTO some_schema.some_table (col2,col3...col35)
SELECT col2,col3...col35
FROM some_schema.another_table_with_same_structure;
Is there a way of doing this without having to list all columns that don't have a default value? I think I can use DEFAULT VALUES
somehow but I can't get the syntax right based on the documentation.
Best Answer
No you either insert all the columns using the
*
expansion,Or, you insert just the listed columns
You can also use
DEFAULT
to be explicit, but no one does that. It's in the spec however, Each column not present in the explicit or implicit column list will be filled with a default value, either its declared default value or null if there is none. Being explicit looks like this.DEFAULT COLUMNS
sets all columns to their respective default. From the docs onDEFAULT COLUMNS
,