I have created a sequence but its not inserting ids in sequence order.
For Ex:
First I have created one set of record seq number generated as 1, 2, 3, 4
Again I have created another set of records seq started from 8, 9, 10
For 3rd time I have created another set of records seq id got generated as 5, 6, 7
(which is not correct, I want the seq id to be continued as 11, 12, 13)
So 5, 6, 7 is wrong, I need 11, 12, 13 to be generated
What's wrong in my below sequence create query?
CREATE SEQUENCE "LEASE_REPAYMENT_SEQ"
MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1
START WITH 146724 CACHE 20 NOORDER NOCYCLE NOKEEP NOSCALE GLOBAL ;
If you check the sequence definition, you'll see that you define it as NOORDER.
The Oracle Documentation says
Specify NOORDER if you do not want to guarantee sequence numbers are generated in order of request. This is the default.
So what you see is an expected bahaviour and I assume your database is a RAC instance (as this effect can be observed on RAC only).
Having said that, there are good reasons to allow this small dis-order of the assigned IDs (which is caused by the caching as each RAC instance gets it own cache size to work with).
The positive side of this apprach is that there is no need to synchronize the sequence between the instances - a task that could produce a big overhead.
Related
Is the number 9999999999999999999999999999 can be written better?
I have multiple queries with that value.
CREATE SEQUENCE "INVOICES_SEQ" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 21 CACHE 20 NOORDER NOCYCLE ;
I don't believe you need to write out the maxvalue. Use NOMAXVALUE, which caps at the 28-digit number.
You could also replace MINVALUE 1 with NOMINVALUE.
ORACLE DOCS:
NOMAXVALUE Specify NOMAXVALUE to indicate a maximum value of 10 27th power
for an ascending sequence or -1 for a descending sequence. This is the
default.
NOMINVALUE Specify NOMINVALUE to indicate a minimum value of 1 for
an ascending sequence or -10 26th power for a descending sequence. This is the
default.
CREATE SEQUENCE "invoices_seq" NOMINVALUE NOMAXVALUE INCREMENT BY 1 START WITH 21 CACHE 20 NOORDER NOCYCLE;
It is good practice to specify only non-default settings.
All of those values except the starting value are the defaults and therefore don't need to be stated (and in my view shouldn't be), so the same sequence can be created more concisely using
create sequence invoices_seq start with 21;
For the more general case in your question title, 9999999999999999999999999999 is 1e28 - 1.
For the documentation, I know connect by can be used together with prior, in hierarchical queries. But quite often I see it is used to generate numbers:
SQL> select level from dual connect by level < 10;
LEVEL
----------
1
2
3
4
5
6
7
8
9
9 rows selected
SQL>
How it works here ? I mean how it generate 1, then 2, and so on. I can not see a hierarchy here, and connect by is not being used with prior. This has confused me for a long time.
The query does a linear recursive call.
Level is fake column generated by the recursion which tells the recursion depth of current iteration. The connect by clause does not need to refer to prior, it is condition just like any other which tells 'If this row has level smaller than 10, union it to the result set'
In oracle SQL, a sequence is dfined like so :
CREATE SEQUENCE sup_seq
MINVALUE 1
MAXVALUE 999
START WITH 5
INCREMENT BY 1
CACHE 20;
What is the point of having the "START WITH" part ? If we have a minimum value , doesn't that imply that we start with that value? What is useful regarding START WITH ?
Normally, it would be pretty unusual to specify both MINVALUE and START WITH when defining a sequence and to have those values be different. Personally, I don't think that I've ever explicitly specified a MINVALUE since I find START WITH to be more self-explanatory.
One case where two values would be useful would be a sequence that is set to cycle. For example
CREATE SEQUENCE sup_seq
MINVALUE 1
MAXVALUE 999
START WITH 5
INCREMENT BY 1
CACHE 20
CYCLE;
would start with 5 (the start with), generate values through 999 (the maxvalue), and then restart at 1 (the minvalue). That's not a particularly common use case but it's entirely possible.
I started to create a sequence in Oracle. While going through the oracle documentation I got this prototype
Create Sequence SeqTest5
Start With 0
Increment by 1
Min value 0
Maxvalue 8
NoCycle --I got to know we can give this as 'Cycle' which will again
-- Repeat the loop. But my doubt is why cannot we specify number of
-- Loops. Such as Cycle 20
NoCache --I got to know we can give certain buffer lenght via cache
Can you Please explain me why cannot we declare it as I have tried it and got this error
1 create sequence seqtest4
2 cache 30,
3* cycle 20,
SQL> /
cache 30,
*
ERROR at line 2:
ORA-00933: SQL command not properly ended
For Example:-
TNUM
0
1
4
2
3
5
6
7
8
This 0-8 should write 10 times and stop.
You can't specify the number of cycles; just whether or not you want to cycle. The CREATE SEQUENCE syntax is here.
There are a few problems with your CREATE SEQUENCE above:
The commas - they don't belong; just get rid of them.
Specifying CYCLE 20 - you can specify CYCLE or NOCYCLE only. The default is NOCYCLE.
If you specify CYCLE you must also specify a MAXVALUE.
Addendum: Question updated with actual requirement, which is to count 1-8 ten times. Here's how to do it without sequences; it's based on an often-used Oracle trick for generating number sequences:
SELECT LEVEL FROM DUAL CONNECT BY LEVEL <= 8
The statement above will output the numbers 1 through 8, in order. To repeat it ten times you need to do another "1 through 10" counter, then cross join it to the "1 through 8", then make sure it orders correctly. This complicates thing a bit, which can be seen in the final answer:
SELECT SeqCounter FROM (
SELECT SeqCounter, CycleCounter FROM (
SELECT LEVEL AS SeqCounter FROM DUAL CONNECT BY LEVEL <= 8)
CROSS JOIN (
SELECT LEVEL AS CycleCounter FROM DUAL CONNECT BY LEVEL <= 10)
) ORDER BY CycleCounter, SeqCounter
The statement above will give the output requested in the question.
Wouldn't this be best handled with a modulo function?
CREATE SEQUENCE seqtest5 START WITH 0 INCREMENT BY 1 MINVALUE 0 MAXVALUE 80 NOCYCLE;
SELECT mod(seqtest5.nextval, 9) from dual;
0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, ...
ORA-08004: sequence SEQTEST5.NEXTVAL exceeds MAXVALUE and cannot be instantiated
The basic syntax for creating a sequence generator for oracle database table attribute is:
CREATE SEQUENCE customers_seq
START WITH 1000
INCREMENT BY 1
NOCACHE
NOCYCLE;
I would like to know what does the NOORDER clause in the SEQUENCE syntax do? If I include the NOORDER clause, will it give me a sequence keeping the increment value random???
This is important when running in parallel server mode or in a cluster. ORDER guarantees that the sequences numbers are ordered.
Example of “noorder” sequence in 10g RAC:
Session 1 on node-A: nextval -> 101
Session 2 on node-A: nextval -> 102
Session 1 on node-B: nextval -> 121
Session 1 on node-B: nextval -> 122
Session 1 on node-A: nextval -> 103
Session 1 on node-A: nextval -> 104
NOORDER in combination with CACHING sequences results in this behavior. ORDER effectively makes the CACHE setting unused. With caching set to 10 when a session gets a sequences it takes 10 sequences into it's cache.