Introduction to Create a Sequence in Oracle
As we discussed the schema objects in the previous session, so in this session that we are going to see one of the schema objects.
This is very important topic that most of the interview questions are comes from here.
At the end of this session, you will be very clear about the below topics related to the sequence:
NOTE: Answer the Interview Questions at the end of the article
What is Sequence in Oracle ?
Sequence is a numeric value generator which is used to generate the numbers in a sequential order by the incremental order. It is mostly used to generate the primary key value.
Importance of creating sequence in Oracle
Syntax for creating a sequence in oracle
CREATE SEQUENCE sequence_name
START WITH
MINVALUE
MAXVALUE
INCREMENT BY
NOCYCLE
NOCACHE
How to run a sequence in oracle?
Sequence can be run in a select command or else can be run using INSERT statement command, which is in DML.
INSERTING the SEQUENCE
Now, we are going to create a sequence for item and product code.
CREATE SEQUENCE contains the sequence name (_seq is optional, for identifying we are using this naming standard)
START WITH - It is used to start the first sequence from this number.
MINVALUE - When the Cycle completes MAXVALUE it will not moves to the START WITH value, it will moves to the MINVALUE only, this will takes place when we creating a sequence using CYCLE, instead of NOCYCLE.
MAXVALUE - It is the last point where the sequence ends if there is NOCYCLE , and some cases if it is in CYCLE condition means after this MAXVALUE point the sequence will generate the MINVALUE and again the cycle continuous.
INCREMENT BY - It is the incremental number that can give sequential number which are evenly distributed. For example: if it is increment by 2 means , it will be 2, 4, 6, 8, 10.......
For clear Understanding about CACHE and NOCACHE
Data Dictionary used in sequences
User_sequences : It is used to check the sequences are present or not, with the help of this we can able to check the Maximum value of running sequence in the LAST_NUMBER column.
To reset a sequence by using a alter command, it will works in latest version of Oracle.
ALTER SEQUENCE sequence_name RESTART START WITH 1;
How to alter the sequences in oracle ?
we can able to alter the sequence in oracle such as reset the sequence, increment the sequence value and altering the MINVALUE and MAXVALUE.
Using DROP keyword we can drop this sequences.
DROP sequence sequence_name;
SQL> DROP sequence id_seq;
Sequence dropped.
CONCLUSION
I hope the after learning this complete article, now you can able to create, alter and run a sequence in oracle database.
If there is any doubts related to the article means, kindly comment, or contact us in official mail.
INTERVIEW QUESTIONS
1, What is sequence in oracle and its data dictionary table used ?
2, Have you used sequence in your project ?
3, How to create a Negative Sequences ?
4, Syntax for creating a sequence ?
5, Difference between CYCLE and NOCYCLE ?
6, Difference between CACHE and NOCACHE ?
7, What is the maximum value limit can be given for sequences ?
8, How to reset a sequence in oracle and also alter commands uses in sequence ?
9, How to use a sequence in oracle ?
10, How to drop a sequence in oracle ?
Comments
Post a Comment
Thanks for commenting, we will always respond your comment.