使用java在oracle数据库中自动递增RowID
好吧,假设我有一个有两列的表格。 Entry_id 和名称。 Entry_id 是一个 ROWID NOT NULL。 本质上,我只是希望每次放入新内容时它都会增加。我如何在PreparedStatement 中执行此操作。我不知道正确的 Entry_id ,因为这并不重要。它应该每次都会增加。因此,如果我可以将名称插入表中并自动递增entry_id,那就太好了。知道如何做到这一点吗?
Ok say I have a table with two columns. Entry_id and name. Entry_id is a ROWID NOT NULL.
Essentially I just want it to increment every time something new is put in. How do i do this in the PreparedStatement. I won't know the correct Entry_id bc it doesn't matter. It should just increment everytime. So it would be nice if i could just insert the name into the table and entry_id increments automatically. Any idea how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ROWID 是表中行的物理地址。使用 ROWID 作为键是没有意义的——如果将表从一个表空间移动到另一个表空间、进行导出和导入、发生行移动等,ROWID 会随着时间而变化。增加 ROWID 是有意义的,因为结果很可能是无效的,要么它不再是实际行的物理地址,要么它不再是有效的物理地址。
如果您想要 Oracle 中的自动递增主键,您可以将该列声明为 NUMBER,而不是 ROWID。然后,您将创建一个序列对象
并在
INSERT
语句中引用该序列的NEXTVAL
当然,您可以创建一个 before-insert 触发器来从然后
,您的 INSERT 语句可以省略 ENTRY_ID 列,让触发器自动填充它。
A ROWID is a physical address for a row in a table. It does not make sense to use a ROWID as a key-- a ROWID will change over time if a table is moved from one tablespace to another, if you do an export and import, if row movement occurs, etc. And it does not make sense to increment a ROWID since the result would quite likely be invalid either in the sense that it would no longer be the physical address of an actual row or that it would not longer be a valid physical address.
If you want an auto-incrementing primary key in Oracle, you would declare the column as a NUMBER, not a ROWID. You would then create a sequence object
and reference the
NEXTVAL
of that sequence in yourINSERT
statementOf course, you could create a before-insert trigger to populate the primary key from the sequence
Your
INSERT
statement could then omit theENTRY_ID
column, letting the trigger automatically populate it.如果您对依赖数据库的方法感到满意,那么通常的方法是使用 oracle 序列。
创建存在于数据库中的序列后,您的代码将类似于
If you are happy with a database dependent way of doing this, then the usual approach is to use an oracle sequence.
After you create the sequence, which lives in the database your code would be along the lines of
我使用了关键字“oracle”和“autoincrement”。找到这个:http:// /situsnya.wordpress.com/2008/09/02/how-to-create-auto-increment-columns-in-oracle/
通过结合使用触发器序列中,您不需要在插入中包含特定于 Oracle 的构造,只需将entry_id 的值保留在插入中的显式值列表之外即可。
I used the keywords "oracle" and "autoincrement". Found this: http://situsnya.wordpress.com/2008/09/02/how-to-create-auto-increment-columns-in-oracle/
By using the trigger in conjunction with the sequence, you don't need to include oracle-specific constructs into your insert, you simply leave the entry_id's value out of the explicit list of values in the insert.