使用java在oracle数据库中自动递增RowID

发布于 2024-11-08 12:51:08 字数 201 浏览 0 评论 0原文

好吧,假设我有一个有两列的表格。 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 技术交流群。

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

发布评论

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

评论(3

花间憩 2024-11-15 12:51:08

ROWID 是表中行的物理地址。使用 ROWID 作为键是没有意义的——如果将表从一个表空间移动到另一个表空间、进行导出和导入、发生行移动等,ROWID 会随着时间而变化。增加 ROWID 是有意义的,因为结果很可能是无效的,要么它不再是实际行的物理地址,要么它不再是有效的物理地址。

如果您想要 Oracle 中的自动递增主键,您可以将该列声明为 NUMBER,而不是 ROWID。然后,您将创建一个序列对象

CREATE SEQUENCE entry_id_seq
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

并在 INSERT 语句中引用该序列的 NEXTVAL

INSERT INTO entry( entry_id, name )
  VALUES( entry_id_seq.nextval, :1 );

当然,您可以创建一个 before-insert 触发器来从然后

CREATE OR REPLACE TRIGGER get_entry_id
  BEFORE INSERT ON entry
  FOR EACH ROW
IS
BEGIN
  SELECT entry_id_seq.nextval
    INTO :new.entry_id
    FROM dual;
END;

,您的 INSERT 语句可以省略 ENTRY_ID 列,让触发器自动填充它。

INSERT INTO entry( name )
  VALUES( :1 );

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

CREATE SEQUENCE entry_id_seq
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

and reference the NEXTVAL of that sequence in your INSERT statement

INSERT INTO entry( entry_id, name )
  VALUES( entry_id_seq.nextval, :1 );

Of course, you could create a before-insert trigger to populate the primary key from the sequence

CREATE OR REPLACE TRIGGER get_entry_id
  BEFORE INSERT ON entry
  FOR EACH ROW
IS
BEGIN
  SELECT entry_id_seq.nextval
    INTO :new.entry_id
    FROM dual;
END;

Your INSERT statement could then omit the ENTRY_ID column, letting the trigger automatically populate it.

INSERT INTO entry( name )
  VALUES( :1 );
木森分化 2024-11-15 12:51:08

如果您对依赖数据库的方法感到满意,那么通常的方法是使用 oracle 序列

创建存在于数据库中的序列后,您的代码将类似于

p = conn.prepareStatement("insert into mytable (entry_id, name) values (mysequence.next_val,?)");
p.setString(1,"My Name");
p.executeUpdate();

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

p = conn.prepareStatement("insert into mytable (entry_id, name) values (mysequence.next_val,?)");
p.setString(1,"My Name");
p.executeUpdate();
(り薆情海 2024-11-15 12:51:08

我使用了关键字“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.

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