'How to find sequnce name which associated to table with table name

How to find sequnce name which is associated to mytable in Oracle sql Select * user_sequwnces where table_name =tablename



Solution 1:[1]

If you created a table using the IDENTITY clause, then the sequence name will aligned with the object ID of the table, eg

    SQL> create table t ( x int generated as identity);
    
    Table created.
    
    SQL> select object_id from user_objects
      2  where object_name = 'T';
    
     OBJECT_ID
    ----------
       4282429
    
    SQL> select sequence_name
      2  from   user_sequences
      3  where sequence_name like '%4282429%';
    
    SEQUENCE_NAME
    -----------------------------------------------------
    ISEQ$$_4282429

But if you just use a sequence of your own as a default, there is no such relationship, eg

SQL> create sequence blah;

Sequence created.

SQL> create table t1 ( x int default blah.nextval, y int );

Table created.

SQL> create table t2 ( x int default blah.nextval, y int );

Table created.

SQL> create table t3 ( x int default blah.nextval, y int );

Table created.

You can even drop the sequence ... and that only would become an issue later when you try to use it, eg

SQL> drop sequence blah;

Sequence dropped.

SQL> insert into t1 (x,y) values (0,0);

1 row created.

SQL> insert into t1 (y) values (0);
insert into t1 (y) values (0)
          *
ERROR at line 1:
ORA-02289: sequence does not exist

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Connor McDonald