UrbanPro
true

Learn Oracle Database from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Interesting thing about Sequences in Oracle

Gavi Y.
19/12/2016 0 0

Let's not get into the very basic things like what is sequence?, What is Cache/Nocache, Cycle/NoCycle etc.

My topic is something very interesting which many of the beginners might not have noticed. My question to you before you read further, let's say we have sequence in one session and its Currval is 10 and you query for the Currval of the same sequence in another session. will you get 10 or something else? Pls note that Sequences retain their values across sessions. i.e they are global in nature, at least till 11g and by default global even in 12c. Before you read further answer this question to yourself and see the truth below.

Let's create a sequence inside the schema A,

CREATE SEQUENCE Test_Seq
MINVALUE 1
START WITH 1
INCREMENT BY 1;

Grant select on Test_Seq to B; ---Another Schema

--Now run this under schema A

Select Test_Seq.nextval from dual;  --- You will get 1

Select Test_Seq.Currval from dual; --- So it's 1

-- Now run this under schema B

Select A.Test_Seq.nextval from dual;  --- You will get 2

Select A.Test_Seq.Currval from dual;  --- You will get 2

--Now under Schema A

Select Test_Seq.Currval from dual;  --- What did u answer? It's actually 1, not 2

Select Test_Seq.nextval from dual;  --- You will get 3 ofcourse as expected.

--Now under Schema B

Select A.Test_Seq.Currval from dual;  --- You will get 2, not 3.

Select A.Test_Seq.Nextval from dual;  ---you will ofcourse get 4 as expected

So guys, be aware of this fact while using Currval pseudo column. So, is there a way to find out the Currval without actually incrementing the sequence by calling Nextval?

Simple solution is to check this view- Select * from user_sequences for Schema A

and Select * from All_sequences for Schema B in this example. Last_number column gives you the Sequence value. This works fine with NoCACHE option. 

Homework: Recreate the above example of Sequence with CACHE option which is default also and then run the same set of above Currval and NextVal queries and check the above mentioned views.

Hope, you enjoyed it. Take care boys. Bye

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

SQL Subqueries - Used in various ways
If you master the concepts of subqueries, you would gain a lot of flexibility in writing complex SQL queries. Let's not get into Correlated subqueries, let us see the different versions of nested subquery...

Introduction to Performance tuning
What is Performance in general terms? Throughput per Input. For Eg: we keep saying, this bike has so and so much of mileage and all that. That is one of the parameters in measuring the performance of...

Oracle Architecture - Introduction
As a Oracle developer or a DBA, you should know the in & out of it’s Architecture. Let’s keep it simple first and explore later. Oracle is a software or a program, so like any other program,...

Some Interesting and important built in packages in Oracle
DBMS_JOB : To schedule jobs. DBMS_SCHEDULER: Extended the functionalities of DBMS_JOB. UTL_SMTP: To send mails. UTL_HTTP: To open Web APIs (URLs). DBMS_LOB: Very interesting and useful. Programmatic...

Interview questions based on "level", a pseudocolumn
1. Write a query to get the below output, 1 11 21 31 .. .. 91 2 12 22 32 .. .. 92 3 13 23 33 .. .. 93 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 10 20 30 40 ...
X

Looking for Oracle Database Classes?

The best tutors for Oracle Database Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Oracle Database with the Best Tutors

The best Tutors for Oracle Database Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more