How can we manage the gap in a primary key column created
by a sequence?
Ex:a company has empno as primary key generated by a
sequence and some employees leaves in between.What is the
best way to manage this gap?

Answers were Sorted based on User's Feedback



How can we manage the gap in a primary key column created by a sequence? Ex:a company has empno a..

Answer / poornima

using cursor we find the gap in sequence, but this method
is quite complicated and slow process...

so we use some analyical fuctions such as lead and lag as

{LEAD | LAG} (value_expression, offset, default)
OVER ([PARTITION BY expr] ORDER BY expr)

Is This Answer Correct ?    5 Yes 0 No

How can we manage the gap in a primary key column created by a sequence? Ex:a company has empno a..

Answer / harikanthkoneti

yes...yes........ you can do it simply by updating that
table using rownum yar. see my example below.
SQL> create table test(sno number(2),sname varchar2(20));

Table created.

now if you have the data in the table like follows.

SQL> select * from test;

SNO SNAME
---------- --------------------
1 sdf
3 sdf
6 ewr
7 rt
90 drt

now just do the following simple statement.


SQL> update test set sno=rownum;

5 rows updated.


now here is the o/p for eliminating the gap generated by the
sequence.

SQL> select * from test;

SNO SNAME
---------- --------------------
1 sdf
2 sdf
3 ewr
4 rt
5 drt
then do the commit. thats all....................
now enjoy with this coding


suppose if you want to allocate the with any kind of series
of values for updating the table just follow the below
procedure

SQL> update test set sno=sql.nextval;

NOTE:sql is the sequence.

this statement will updates ur table with
what ever you given with the start with value and it ll
increment by which value you had given in the increment by
clause.

Is This Answer Correct ?    1 Yes 3 No

Post New Answer

More Oracle General Interview Questions

primary key is foreign key for the same table?

2 Answers   Oracle,


Hi Every One , What is the table Space in Oracle Database ? waiting for answere and thankyou

8 Answers  


How to resolve name conflicts between variables and columns?

0 Answers  


List the parts of a database trigger.

0 Answers  


What is the difference between post-database commit and post-form commit?

0 Answers  






How many types of cluster table in Oracle?

0 Answers   MCN Solutions,


How to write a query with a right outer join in oracle?

0 Answers  


consider some table with 4 r 5 columns in that 1 col is DATE type. The data is like that,For each date in that col some 3 fields r there but all the records r having different data. Now i want to display all the columns by performing grouping on the date field ( as SELECTION Operator(*) cannot be used with group function having only one "group by clause". how to do this? can any one help me in finding out the solution plss?

1 Answers  


What is the difference between Sleep and Wait? (Java)

1 Answers   RBS,


Differentiate between pre-select and pre-query?

0 Answers  


How to run create database statement again?

0 Answers  


How to define an anonymous procedure without variables?

0 Answers  


Categories
  • Oracle General Interview Questions Oracle General (1789)
  • Oracle DBA (Database Administration) Interview Questions Oracle DBA (Database Administration) (261)
  • Oracle Call Interface (OCI) Interview Questions Oracle Call Interface (OCI) (10)
  • Oracle Architecture Interview Questions Oracle Architecture (90)
  • Oracle Security Interview Questions Oracle Security (38)
  • Oracle Forms Reports Interview Questions Oracle Forms Reports (510)
  • Oracle Data Integrator (ODI) Interview Questions Oracle Data Integrator (ODI) (120)
  • Oracle ETL Interview Questions Oracle ETL (15)
  • Oracle RAC Interview Questions Oracle RAC (93)
  • Oracle D2K Interview Questions Oracle D2K (72)
  • Oracle AllOther Interview Questions Oracle AllOther (241)