Skip to Main Content
  • Questions
  • Relation between sequence and column (not the autoincrement problem)

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, John.

Asked: September 15, 2006 - 10:45 am UTC

Last updated: November 05, 2012 - 8:40 am UTC

Version: 9.1.i

Viewed 10K+ times! This question is

You Asked

Hi, I need to make a function, which receive
1. Table name
2. Column name

And it give me the sequence name of the sequence that i have made for autoincrement that field (i did it with a trigger).

In postgres i can have that relation, I did't find the way to solve this in oracle.

and Tom said...

there is no such "tie in" between the two.


A sequence is entirely separate and distinct from the table - the sequence is a standalong object.


Now, that said, you can:

ops$tkyte%ORA10GR2> create table t ( x int primary key, y int );

Table created.

ops$tkyte%ORA10GR2> create sequence s;

Sequence created.

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> create trigger t_trigger
2 before insert on t for each row
3 begin
4 if (:new.x is null )
5 then
6 select s.nextval into :new.x from dual;
7 end if;
8 end;
9 /

Trigger created.

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> select referenced_name
2 from user_dependencies
3 where referenced_type = 'SEQUENCE'
4 and type = 'TRIGGER'
5 and name in (select trigger_name
6 from user_triggers
7 where table_name = 'T'
8 and table_owner = user)
9 /

REFERENCED_NAME
----------------------------------------------------------------
S


use the dependencies tables to find all of the sequences statically referenced by any of the triggers on a given table.

Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thankz!!!

John Peterson, September 15, 2006 - 2:24 pm UTC

This is what i was looking,thankz

why

scott leblanc, September 16, 2006 - 12:26 am UTC

I'm curious to know - why it is necessary to keep the table and the sequence separate objects.

We often are left scratching our heads wondering whether the CONTACT_ID_SEQ sequence populates AGREEMENT.CONTACT_ID or INVOICE_H.CONTACT_ID. Ultimately our issue is poor naming conventions or inconsistent methods to populate the table.

…….I cannot think of a reason to keep table and sequence objects separate.




Tom Kyte
September 16, 2006 - 2:38 pm UTC

because it could populate BOTH.

In Oracle, with sequences, they are general purpose surrogate key generators. They have never been associated to or limited to a single column.

BA, November 03, 2012 - 3:30 pm UTC

how about option to set sequence.nextval as column default?
Tom Kyte
November 05, 2012 - 8:40 am UTC

wait just a little bit of time... coming soon to a theater near you !

Another way

Ali, July 31, 2015 - 9:41 pm UTC

select t.table_name, d.referenced_name as sequence_name, d.REFERENCED_OWNER as "OWNER",c.COLUMN_NAME
from user_trigger_cols t , user_dependencies d, user_tab_cols c
where d.name = t.trigger_name
and t.TABLE_NAME = c.TABLE_NAME
and t.COLUMN_NAME = c.COLUMN_NAME
and d.referenced_type = 'SEQUENCE'
and d.type = 'TRIGGER'

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library