使用具有默认值的列将多行插入 table

insert multiple rows into table with column that has default value

我在 PostgreSQL 中有一个 table,其中一列有默认值。
table 的 DDL 是:

CREATE TABLE public.my_table_name
(int_column_1 character varying(6) NOT NULL,
 text_column_1 character varying(20) NOT NULL,
 text_column_2 character varying(15) NOT NULL,
 default_column numeric(10,7) NOT NULL DEFAULT 0.1,
 time_stamp_column date NOT NULL);

我正在尝试在单个查询中插入多行。在这些行中,我有一些行具有 default_column 的值,而我有一些行没有任何 default_column 的值,并且希望 Postgres 使用这些行的默认值。

这是我尝试过的:

INSERT INTO "my_table_name"(int_column_1, text_column_1, text_column_2, default_column, time_stamp_column) 
VALUES
(91,'text_row_11','text_row_21',8,current_timestamp),
(91,'text_row_12','text_row_22',,current_timestamp),
(91,'text_row_13','text_row_23',19,current_timestamp),
(91,'text_row_14','text_row_24',,current_timestamp),
(91,'text_row_15','text_row_25',27,current_timestamp);

这给了我一个错误。所以,当我尝试插入时:

INSERT INTO "my_table_name"(int_column_1, text_column_1, text_column_2, default_column, time_stamp_column) 
    VALUES (91,'text_row_12','text_row_22',,current_timestamp), -- i want null to be appended here, so i left it empty. 
--error from this query is: ERROR:  syntax error at or near ","

INSERT INTO "my_table_name"(int_column_1, text_column_1, text_column_2, default_column, time_stamp_column) 
    VALUES (91,'text_row_14','text_row_24',NULL,current_timestamp), 
-- error from this query is: ERROR:  new row for relation "glycemicindxdir" violates check constraint "food_item_check"

那么,我该如何解决这个问题;当我有值时插入值,或者当我没有值时让 Postgres 插入默认值?

使用default关键字:

INSERT INTO my_table_name
  (int_column_1,  text_column_1,  text_column_2,  default_column,  time_stamp_column) 
VALUES
  (91, 'text_row_11', 'text_row_21', 8      , current_timestamp), 
  (91, 'text_row_12', 'text_row_22', default, current_timestamp), 
  (91, 'text_row_13', 'text_row_23', 19     , current_timestamp), 
  (91, 'text_row_14', 'text_row_24', default, current_timestamp), 
  (91, 'text_row_15', 'text_row_25', 27     , current_timestamp);