Oracle:插入时不指定列

发布于 2024-11-24 05:40:55 字数 409 浏览 2 评论 0原文

我有一个带有序列和自动增量列的触发器的 Oracle 表。 现在我想做一个插入。通常我应该写:

INSERT INTO table (column1, column2,...) VALUES (value1, value2)

但我只想插入一条没有默认值的记录。在 Oracle 中如何做到这一点?

`Mysql`: INSERT INTO table () VALUES ()

`Mssql`: INSERT INTO table default VALUES 

`Oracle:` INSERT INTO table (column1, column2,...) VALUES (default, default,...)

这是唯一的方法吗?我必须列出所有列吗?

I have an oracle table with a sequence and a trigger to autoincrement a column.
Now I want to make an insert. Normally I should write:

INSERT INTO table (column1, column2,...) VALUES (value1, value2)

but I just want to insert a record with no default values. How to do this in Oracle?

`Mysql`: INSERT INTO table () VALUES ()

`Mssql`: INSERT INTO table default VALUES 

`Oracle:` INSERT INTO table (column1, column2,...) VALUES (default, default,...)

Is this the only way? I have to list all columns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傲性难收 2024-12-01 05:40:55
INSERT INTO table (column1) VALUES (default);

剩下的就默认了!

INSERT INTO table (column1) VALUES (default);

The rest will be defaulted!

甜中书 2024-12-01 05:40:55

在 Oracle 中,您不必指定列,但不这样做将使您在表定义更改时容易引入错误。

您可以插入:

INSERT INTO t VALUES (value1, value2, value3);

假设表 t 有三列,

这样插入会更好并且更受支持:

INSERT INTO t (column1,column2,column3) VALUES (value1, value2, value3);

我不会使用 PL/SQL(如果你能帮助的话),因为你引入了从 PL/SQL 到 SQL 的上下文切换,然后再次回到 PL/SQL。

In Oracle you don't HAVE to specify the columns but not doing so will leave you open to introducing bugs as and when your table definition changes.

You could insert with:

INSERT INTO t VALUES (value1, value2, value3);

This assumes the table t has three columns

It is far better and supportable to insert with:

INSERT INTO t (column1, column2, column3) VALUES (value1, value2, value3);

I wouldn't use PL/SQL (if you can help it) as you introduce context switching from PL/SQL to SQL and then back to PL/SQL again.

三生殊途 2024-12-01 05:40:55

我在第一次阅读时错过了这一部分:

我有一个带有序列和自动增量触发器的 Oracle 表
一栏。

所以我假设 PK 列使用序列填充,其他列都有默认值。鉴于此,我会这样做:

INSERT INTO table (pk_column) VALUES (NULL);

触发器将覆盖 NULL 值(如果由于某种原因没有覆盖,插入将会失败);其他列将使用默认值填充。

I missed this part on the first read:

I have an oracle table with a sequence and a trigger to autoincrement
a column.

So I assume there is on PK column populated using the sequence, and the others all have default values. Given that, I would do this:

INSERT INTO table (pk_column) VALUES (NULL);

The trigger will override the NULL value (and if it doesn't for some reason, the insert will fail); and the other columns will be populated using defaults.

暮凉 2024-12-01 05:40:55

这是唯一的方法吗?我必须列出所有列?

是的。最好始终指定 INSERT 语句中要为其提供值的所有列。

Is this the only way? I have to list all columns?

Yes. And it's good practice to always specify all columns in an INSERT statement that you want to supply values for.

夏末的微笑 2024-12-01 05:40:55

只有一种情况您不需要在 Oracle 中指定 COLUMNS,即当您插入为所有列指定值的行时,并且插入的顺序也与表定义的顺序相同。在所有其他情况下,需要指定列列表。

示例:

create table tt (id number, name varchar2(30));
insert into tt values (1,'AC');
insert into tt values ('2',1);

特别注意第二条插入语句 ->第一个参数“2”以字符串形式提供,期望是 ID 列中的数字。这里进行必要的转换并在 ID 列中插入 2。但用户可能希望在 NAME 列中插入“2”,并插入 1 作为 ID。通过指定列列表,Oracle 可以帮助我们避免此类错误。

此外,现在很少有您不需要指定列列表的情况,因为几乎所有创建的表大多都有一个自动递增的唯一 ID 列,该列是通过触发器自动填充的,或者是因为 IDENTITY 列 并且您只提供其余列的值,这意味着必须提供这些列列表,否则

如果该唯一 ID 是由应用程序生成并插入的,例如通过随机数生成或 Hibernate 然后,因为所有列都会被插入,所以可能会避免列列表。

There is only one case where you don't need to specify COLUMNS in Oracle and that is when you are inserting rows with values specified for all columns and that too in same sequence as the definition of table. In all other cases one needs to specify column list.

Example :

create table tt (id number, name varchar2(30));
insert into tt values (1,'AC');
insert into tt values ('2',1);

Especially note 2nd insert statement -> First parameter '2' is provided as String and expectation is number in ID column. Here does necessary casting and inserts 2 in ID column. But user may have wanted to insert '2' in NAME column and 1 as ID. With specifying column list Oracles helps us to avoid such errors.

Also there will be rarely any situation now a days where you DON"T need to specify column list because almost all tables created mostly have an auto-incremented unique ID column which is auto-filled via Trigger or because of IDENTITY columns and you provide values of rest of the columns only that means those column list must be provided else it will result in error.

In case this unique ID is generated and inserted by an application say generated in java application via Random number generation or Hibernate then as all column would be inserted column list might be avoided.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文