Search code examples
sqloracle-databaseselectconstraintsdata-dictionary

Oracle: Table with all constraints


I am trying to create a select statement, which allows me to get an overview over all of my foreign keys. It should look like this:

Table 1  | FK_Value  | Table 2 | FK_Value

I have figured out there must be a way with the "user_constraints" table but i need some help.


Solution

  • I think you need this:

    SELECT a.table_name, a.column_name, a.constraint_name, c.owner, 
           -- referenced pk
           c.r_owner, c_pk.table_name r_table_name, c_pk.constraint_name r_pk
      FROM all_cons_columns a
      JOIN all_constraints c ON a.owner = c.owner
                            AND a.constraint_name = c.constraint_name
      JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
                               AND c.r_constraint_name = c_pk.constraint_name
     WHERE c.constraint_type = 'R'
       AND a.table_name = :TableName
    

    Source: stackoverflow.com/questions/1729996/list-of-foreign-keys-and-the-tables-they-reference