Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Oracle table uses a sequence?

Having a sequence, I need to find out which table.column gets its values. As far as I know, Oracle doesn't keep track of this relationship. So, looking up for the sequence in source code would be the only way. Is that right?

Anyone knows of some way to find out this sequence-table relationship?

like image 546
mcrisc Avatar asked Mar 02 '10 13:03

mcrisc


People also ask

How do you check if a table has a sequence in Oracle?

You should use the query "select sequence_name from all_sequences;" to find out all sequences accessible to the schema user.

What is Oracle sequence table?

An Oracle Sequence is a database object, just like a table or view, that represents a sequence of integers that can be used by any table or view in the global database namespace. A Sequence's values can be accessed using the NEXTVAL, and CURRVAL pseudo-columns. A Sequence can be ascending or descending.

What is a sequence table?

A sort sequence table is an object that contains the weight of each single-byte graphic character within a specified coded character set identifier (CCSID). The system-recognized identifier for the sort sequence table object type is *TBL.

Why sequences are used in Oracle?

A sequence is an object in Oracle that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key.


2 Answers

The problem is that Oracle allows us to use one sequence to populate columns in several tables. Scenarios where this might be desirable include super-type/sub-type implementations.

You can use the dependencies in the data dictionary to identify relationships. For instance, if you use triggers to assign the values then this query will help you:

select ut.table_name
       , ud.referenced_name as sequence_name
from   user_dependencies ud
       join user_triggers ut on (ut.trigger_name = ud.name)
where ud.type='TRIGGER' 
and ud.referenced_type='SEQUENCE'
/

If you use PL/SQL then you can write something similar for TYPE in ('PACKAGE BODY', 'PROCEDURE', 'FUNCTION'), although you will still require some trawling through the source code to assign tables and sequences when you have multiple hits.

like image 151
APC Avatar answered Sep 21 '22 18:09

APC


In the database you can search all stored code in your schema like this:

select type, name, line, text
from all_source
where owner = 'MYSCHEMA'
and upper(text) like '%MYSEQ.NEXTVAL%';

In SQL Developer, there is a report to do this.

like image 42
Tony Andrews Avatar answered Sep 20 '22 18:09

Tony Andrews