Oracle Sequences

I use Sequence to generate an identifier for me, but I use this sequence from my Java application. So let's say, for example, my last identifier is 200.

If I add an entry with .sql, using 201 as an identifier instead of seq.nextval. What happens when a java application calls seq.nextval? Is the sequence smart enough to check the maximum number available, or will it just return 201?

+3
source share
5 answers

It will simply return 201, since the sequence does not know what the numbers are used for.

. , (. Oracle CREATE SEQUENCE )

+3

"" , .

201, , .

+2

201.

nextval JDBC, :

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select seq.nextval from dual");
rs.next();
int yourId = rs.getInt(1);

// then use yourId to do the insert

, , .

0

, nextval Java-, :

  • , , node. node ;
  • node, . , "" ( );
  • ;
  • nextval .

, .

Oracle , , , 201 , 201, , .

0

It is not recommended to mix values ​​generated sequentially with manual inserts, because then everything is mixed.

I'm not sure if this helps in your situation, but remember that you can query the current value of the sequence (with seq.currval or similar) so that you can check if the table already exists due to manual insertion and, if necessary, request more one next

0
source

Source: https://habr.com/ru/post/1708036/


All Articles