1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

How do I find out which columns are foreign keys in a table?

Discussion in 'SQL - Questions and Answers' started by Franky, Aug 31, 2006.

  1. Franky Guest

    Dialect: Oracle
    I need to know which columns in my table are foreign keys. If I use the 'desc table_name', it doesn't tell me what I want to know. Please help
  2. Dimitar Guest

    Dialect: Oracle
    I would suggest that you use a tool in your every day work. One such - TOra - is freely available under GPL license:
    http://tora.sourceforge.net/

    Best Regards,
    Dimitar
  3. charlesryoung New Member

    Dialect: Oracle
    Perhaps these two queries will help:
    SQL:

    select Parent.TABLE_NAME Parent_TABLE,
    Parent.CONSTRAINT_NAME Parent_CONSTRAINT,
    Child.TABLE_NAME Child_TABLE,
    Child.CONSTRAINT_NAME Child_CONSTRAINT,
    CHILD.Delete_Rule,
    CHILD.Status
    from All_CONSTRAINTS Child, All_CONSTRAINTS Parent
    where Child.R_CONSTRAINT_NAME = Parent.CONSTRAINT_NAME
    and Child.TABLE_NAME != Parent.TABLE_NAME
    and Child.TABLE_NAME = upper('&Child_Table_Name')
    ORDER BY Parent.TABLE_NAME


    SQL:

    select Parent.TABLE_NAME Parent_TABLE,
    Parent.CONSTRAINT_NAME Parent_CONSTRAINT,
    RTRIM(Child.TABLE_NAME) Child_TABLE, atc.comments AS Table_Desc,
    Child.CONSTRAINT_NAME Child_CONSTRAINT,
    RTRIM(col.COLUMN_NAME) AS Child_Col, col.POSITION AS Child_Col_Position
    ,CHILD.Delete_Rule, CHILD.Status
    from All_CONSTRAINTS Child, All_CONSTRAINTS Parent, sys.all_tab_comments atc, ALL_CONS_COLUMNS col
    where Child.R_CONSTRAINT_NAME = Parent.CONSTRAINT_NAME
    and Child.TABLE_NAME != Parent.TABLE_NAME
    and Parent.TABLE_NAME = upper('&Parent_Table_Name')
    AND Child.owner = atc.Owner(+)
    AND Child.TABLE_NAME = atc.table_name(+)
    AND col.owner = child.owner
    AND Child.constraint_name = col.constraint_name
    ORDER BY Child.TABLE_NAME

Share This Page