What is purpose of sequence ?
-----------------------------
In oracle you can use sequence to generate primary key
Syntax to create sequence
-------------------------
CREATE SEQUENCE STUDENTS_ID_SEQ INCREMENT BY 1 START WITH 1 MAXVALUE 10000 NOCACHE NOCYCLE;
Example:
create table students( sid number primary key, fname varchar2(100), lname varchar2(100));
Insert statements
------------------
Insert into students values(STUDENTS_ID_SEQ.nextval,'Anil','kumar'); Insert into students values(STUDENTS_ID_SEQ.nextval,'Sunil','kumar');
Syntax to get the sequence number is sequencename.nextval.
You can see that in the above insert statements
I call nextval to get the primary key which is automatically
generated by the sequence we created
When I created STUDENTS_ID_SEQ I said that sequence should start
with 1 and increment by 1
So when u call the nextval for first time it generates 1
If you call again it will generate 2
You can say increment as 2
Then the generated sequences will be 1,3,5...
If you don't know this.. you have to get the max student id and increment
it by one.... right ?
To just display the next sequence number you can execute
following query
select STUDENTS_ID_SEQ.nextval from dual;
Small Task....... :)
Try to generate nextval more than 10000 times
and see what happens........ ?
No comments:
Post a Comment