Oracle:插入时不指定列
我有一个带有序列和自动增量列的触发器的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
剩下的就默认了!
The rest will be defaulted!
在 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.
我在第一次阅读时错过了这一部分:
所以我假设 PK 列使用序列填充,其他列都有默认值。鉴于此,我会这样做:
触发器将覆盖 NULL 值(如果由于某种原因没有覆盖,插入将会失败);其他列将使用默认值填充。
I missed this part on the first read:
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:
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.
是的。最好始终指定 INSERT 语句中要为其提供值的所有列。
Yes. And it's good practice to always specify all columns in an INSERT statement that you want to supply values for.
只有一种情况您不需要在 Oracle 中指定 COLUMNS,即当您插入为所有列指定值的行时,并且插入的顺序也与表定义的顺序相同。在所有其他情况下,需要指定列列表。
示例:
特别注意第二条插入语句 ->第一个参数“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 :
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.