简体   繁体   English

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

[英]Auto Increment RowID in oracle database using java

Ok say I have a table with two columns. 好吧我说我有一个有两列的表。 Entry_id and name. Entry_id和名称。 Entry_id is a ROWID NOT NULL. Entry_id是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. 基本上我只是希望它在每次放入新内容时都会增加。我如何在PreparedStatement中执行此操作。 I won't know the correct Entry_id bc it doesn't matter. 我不会知道正确的Entry_id bc无关紧要。 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. 因此,如果我可以将名称插入表中并且entry_id自动递增,那将是很好的。 Any idea how to do this? 知道怎么做吗?

A ROWID is a physical address for a row in a table. ROWID是表中行的物理地址。 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. 将ROWID用作键是没有意义的 - 如果将表从一个表空间移动到另一个表空间,如果执行导出和导入,如果发生行移动等,则ROWID将随时间而变化。等等有意义的是增加一个ROWID,因为结果很可能是无效的,因为它不再是实际行的物理地址,或者它不再是有效的物理地址。

If you want an auto-incrementing primary key in Oracle, you would declare the column as a NUMBER, not a ROWID. 如果要在Oracle中使用自动递增主键,则应将该列声明为NUMBER,而不是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语句中引用该序列的NEXTVAL

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 当然,您可以创建一个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;

Your INSERT statement could then omit the ENTRY_ID column, letting the trigger automatically populate it. 然后,您的INSERT语句可以省略ENTRY_ID列,让触发器自动填充它。

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

If you are happy with a database dependent way of doing this, then the usual approach is to use an oracle sequence . 如果您对数据库相关的方式感到满意,那么通常的方法是使用oracle序列

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();

I used the keywords "oracle" and "autoincrement". 我使用了关键字“oracle”和“autoincrement”。 Found this: http://situsnya.wordpress.com/2008/09/02/how-to-create-auto-increment-columns-in-oracle/ 发现这个: 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. 通过将触发器与序列结合使用,您不需要在插入中包含特定于oracle的构造,只需将entry_id的值保留在插入中的显式值列表之外。

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

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