Finding Primary Table for a Foreign Key

Qasim Khan

In the company I work at, the database is massive. We have a lot of tables and sometimes when I am using SQL to search for something i.e. what status a particular Purchase Order is in. I often find that the column the status is stored in only contains a number (therefore the column is a foreign key).

Q: I'd like to know how to find the table where this foreign key referring to. Without using the diagram as its massive and I can't tell the relationships.

e.g.

Table: Purchase Orders
Column: PO_Status
PO_Status only contains number 1-10.

The name and description of the status is stored in another table and the number 1-10 is referring to that record in that other table. I want to know what that table name is.

This will help save a lot of time. Please Help!

Dessma

You can interrogate Oracle's "metadata" :

SELECT c.table_name, c.constraint_name, c2.table_name "REFERENCED_TABLE"
  FROM all_constraints c
 INNER JOIN all_constraints c2
    ON c.r_constraint_name = c2.constraint_name
 WHERE c.table_name = 'YOUR_TABLE_NAME';

Also, keep in mind that some value restrictions can be enforced using CHECK instead of implying the presence of a FK.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mapping of primary key as foreign key to another table

From Dev

create a foreign key on a primary key of another table

From Dev

Use of Primary Key as Foreign Key in Foreign Key Table

From Dev

How to refer primary key as Foreign to various table

From Dev

Primary And Foreign key mapping between view and table

From Dev

Join on foreign key of table with the max primary key of that table

From Dev

mysql join table where foreign key is primary key of same table

From Dev

Update primary key in one table which is foreign key in another table

From Dev

Can a unique key ( not a primary key) be a foreign key to other table?

From Dev

SELECT a non ID column in a foreign key table (TABLE B) based on the foreign key in the primary table (TABLE

From Dev

Understanding how a Primary Key of one Table can be a Foreign Key too

From Java

Can a foreign key refer to a primary key in the same table?

From Dev

SQL create table primary key and foreign key syntax

From Dev

How to add data if the primary key is used as foreign key in the same table?

From Dev

Count the occurrences of a foreign key that references a primary key in the same table

From Dev

Can a foreign key refer to the primary key of its own table?

From Dev

is it ok for composite primary key to have foreign key refer to parent table

From Dev

Storing a foreign key for the main primary key in each table

From Dev

is it ok for composite primary key to have foreign key refer to parent table

From Dev

UPDATE set table with 3 different tables foreign key and primary key

From Dev

reference a foreign key to a primary key within the same table

From Dev

MySQL - foreign key constrained by primary key in same table, error #1452

From Dev

PrImary key or foreign key for a m-to-n relation table

From Dev

MySQL - Inserting Primary Key from one table to another (Foreign Key)

From Dev

Composite foreign key made up of part of primary key in parent table

From Dev

I cannot send primary key of one table to another as foreign key?

From Dev

How to insert Primary Key value of the primary table to the Foreign Key column of the child table in MySQL?

From Dev

EF6: Composite Primary Key Field as Foreign Key (ALTER TABLE statement conflicted with the FOREIGN KEY constraint)

From Dev

Finding Foreign Key

Related Related

  1. 1

    Mapping of primary key as foreign key to another table

  2. 2

    create a foreign key on a primary key of another table

  3. 3

    Use of Primary Key as Foreign Key in Foreign Key Table

  4. 4

    How to refer primary key as Foreign to various table

  5. 5

    Primary And Foreign key mapping between view and table

  6. 6

    Join on foreign key of table with the max primary key of that table

  7. 7

    mysql join table where foreign key is primary key of same table

  8. 8

    Update primary key in one table which is foreign key in another table

  9. 9

    Can a unique key ( not a primary key) be a foreign key to other table?

  10. 10

    SELECT a non ID column in a foreign key table (TABLE B) based on the foreign key in the primary table (TABLE

  11. 11

    Understanding how a Primary Key of one Table can be a Foreign Key too

  12. 12

    Can a foreign key refer to a primary key in the same table?

  13. 13

    SQL create table primary key and foreign key syntax

  14. 14

    How to add data if the primary key is used as foreign key in the same table?

  15. 15

    Count the occurrences of a foreign key that references a primary key in the same table

  16. 16

    Can a foreign key refer to the primary key of its own table?

  17. 17

    is it ok for composite primary key to have foreign key refer to parent table

  18. 18

    Storing a foreign key for the main primary key in each table

  19. 19

    is it ok for composite primary key to have foreign key refer to parent table

  20. 20

    UPDATE set table with 3 different tables foreign key and primary key

  21. 21

    reference a foreign key to a primary key within the same table

  22. 22

    MySQL - foreign key constrained by primary key in same table, error #1452

  23. 23

    PrImary key or foreign key for a m-to-n relation table

  24. 24

    MySQL - Inserting Primary Key from one table to another (Foreign Key)

  25. 25

    Composite foreign key made up of part of primary key in parent table

  26. 26

    I cannot send primary key of one table to another as foreign key?

  27. 27

    How to insert Primary Key value of the primary table to the Foreign Key column of the child table in MySQL?

  28. 28

    EF6: Composite Primary Key Field as Foreign Key (ALTER TABLE statement conflicted with the FOREIGN KEY constraint)

  29. 29

    Finding Foreign Key

HotTag

Archive