简体   繁体   English

如何在不提供列名但不提供标识列的情况下插入

[英]How to insert without giving column names but not giving identity column

Table definition:表定义:

CREATE TABLE IF NOT EXISTS public.test
(
    "Id" integer NOT NULL GENERATED ALWAYS AS IDENTITY (INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1),
    "SomeColumn" character(100) COLLATE pg_catalog."default",
    CONSTRAINT test_pkey PRIMARY KEY ("Id")
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.test
    OWNER to postgres;

I am trying this query:我正在尝试这个查询:

INSERT INTO public.test VALUES ('testData');

But PostgreSQL throws this error:但是 PostgreSQL 抛出这个错误:

ERROR: invalid input syntax for type integer: "testData"错误:整数类型的输入语法无效:“testData”
LINE 1: INSERT INTO public.test VALUES ('testData'); LINE 1: INSERT INTO public.test VALUES ('testData');

I know this is valid in SQL Server.我知道这在 SQL Server 中有效。 Is there a way the achieve this behaviour in PostgreSQL?有没有办法在 PostgreSQL 中实现这种行为?

I do not want to specify the column names.我不想指定列名。 Columns are defined in the order, but the identity column does not exist in the query.列在顺序中定义,但标识列在查询中不存在。

I want to not give the column names我不想给出列名

That's a bad idea.这是个坏主意。 You should always specify the target columns for an INSERT statement.您应该始终为 INSERT 语句指定目标列。 Especially if you want to skip some, but not others.特别是如果你想跳过一些,而不是其他人。

However, if you insist on bad coding style, you can use the DEFAULT keyword然而,如果你坚持糟糕的编码风格,你可以使用DEFAULT关键字

INSERT INTO public.test VALUES (DEFAULT, 'testData');
INSERT INTO public.test VALUES (NULL, 'testData');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM