How to know relations between tables
Asked Answered
C

9

23

I have a database in MySQL created by someone. I don't have any documentation of the database.

How can I know the relationship between the tables?

Is there any query or a procedure to generate a report so that it's easy to find the relations?

I can look into Schema information and manually figure it out, but it would be great if I could generate a relationship report.

Crinoline answered 15/2, 2016 at 12:0 Comment(4)
You may use mysql workbench and get ER diagram with all relationshipsKatlynkatmai
possible dupe of... #20855565Medicable
Relations are not necessarily available in the database - it is not uncommon in enterprise transactional systems for RI to be stripped out for performance purposes. Also, some highly customizable systems (PeopleSoft, SAP, Maximo) maintain RI in the application and have tables to define the relationships.Dormancy
This isn't clear. Do you want the FKs that were declared & are recorded in DBMS metadata, or do you mean guessing at ones would or should have been declared by looking at data?Fiver
S
16

The better way as programmatically speaking is gathering data from INFORMATION_SCHEMA.KEY_COLUMN_USAGE table as follows:

SELECT 
  `TABLE_SCHEMA`,                          -- Foreign key schema
  `TABLE_NAME`,                            -- Foreign key table
  `COLUMN_NAME`,                           -- Foreign key column
  `REFERENCED_TABLE_SCHEMA`,               -- Origin key schema
  `REFERENCED_TABLE_NAME`,                 -- Origin key table
  `REFERENCED_COLUMN_NAME`                 -- Origin key column
FROM
  `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE`  -- Will fail if user don't have privilege
WHERE
  `TABLE_SCHEMA` = SCHEMA()                -- Detect current schema in USE 
  AND `REFERENCED_TABLE_NAME` IS NOT NULL; -- Only tables with foreign keys

and another one is

select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS;
Soupandfish answered 15/2, 2016 at 12:12 Comment(2)
I'm using xamp is it will work if write the same query their?Crinoline
@Narasimha Maiya i haven't worked on xamp yet.you can try it on xamp .ThanksSoupandfish
K
20

You can get an overview in MySql Workbench by doing the steps below:

  1. Go to "Database" Menu option.
  2. Select the "Reverse Engineer" option.
  3. A wizard will be opened and will generate an EER Diagram which shows up
Katlynkatmai answered 15/2, 2016 at 12:11 Comment(0)
S
16

The better way as programmatically speaking is gathering data from INFORMATION_SCHEMA.KEY_COLUMN_USAGE table as follows:

SELECT 
  `TABLE_SCHEMA`,                          -- Foreign key schema
  `TABLE_NAME`,                            -- Foreign key table
  `COLUMN_NAME`,                           -- Foreign key column
  `REFERENCED_TABLE_SCHEMA`,               -- Origin key schema
  `REFERENCED_TABLE_NAME`,                 -- Origin key table
  `REFERENCED_COLUMN_NAME`                 -- Origin key column
FROM
  `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE`  -- Will fail if user don't have privilege
WHERE
  `TABLE_SCHEMA` = SCHEMA()                -- Detect current schema in USE 
  AND `REFERENCED_TABLE_NAME` IS NOT NULL; -- Only tables with foreign keys

and another one is

select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS;
Soupandfish answered 15/2, 2016 at 12:12 Comment(2)
I'm using xamp is it will work if write the same query their?Crinoline
@Narasimha Maiya i haven't worked on xamp yet.you can try it on xamp .ThanksSoupandfish
A
12

Try out SchemaSpy (http://schemaspy.sourceforge.net/):

SchemaSpy is a Java-based tool (requires Java 5 or higher) that analyzes the metadata of a schema in a database and generates a visual representation of it in a browser-displayable format.

Here is a screenshot of the HTML page of the sample output from http://schemaspy.sourceforge.net/sample/ :

Screenshot of the HTML page of the sample output from http://schemaspy.sourceforge.net/sample/

There is also a nice GUI if you do not want to use the command line: http://schemaspygui.sourceforge.net/

Both tools are open source and in my opinion very lightweight and easy to use. I used them several times when I was in situations that you described: To get an overview of the schema and even some details to dive deeper. (Take a look at the "Anomalies" report.)


Update

Be sure to check out the upcoming version of SchemaSpy at http://schemaspy.org

Apologist answered 11/3, 2016 at 20:39 Comment(0)
B
5

Do you have the SELECTs that use the database? That may be the best source of the relationships.

Bloody answered 6/3, 2016 at 22:11 Comment(0)
C
5

One more valuable option may be if you just install mysql workbench.( refers to) And try "Create EER models from database" .You will surely able to see relations among tables.

Carman answered 11/3, 2016 at 12:6 Comment(0)
B
4

You may take a look at information_scheme.KEY_COLUMN_USAGE table

As it is suggested there a quick way to list your FKs (Foreign Key references) using the KEY_COLUMN_USAGE view:

SELECT CONCAT( table_name, '.',
column_name, ' -> ',
referenced_table_name, '.',
referenced_column_name ) AS list_of_fks
FROM information_schema.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_SCHEMA = (your schema name here)
AND REFERENCED_TABLE_NAME is not null
ORDER BY TABLE_NAME, COLUMN_NAME;
Bach answered 15/2, 2016 at 12:9 Comment(0)
C
3

If you are using phpmyadmin then:

  1. Goto the database.
  2. Select the table and goto its structure.
  3. You'll find relation view at the bottom of your table structure.
Cherimoya answered 11/3, 2016 at 7:13 Comment(0)
G
0

Better you use Mysql workbench. There is an option for generating ER diagram. If you are using phpmyadmin select any table. There is structure tab from where you can see the table structure. Hopefully, this will help.

Gennagennaro answered 12/3, 2016 at 6:16 Comment(0)
U
-1

In phpMyAdmin (5.1.1), one have to select 'Tables' then search for 'Designer' where you can see the DB diagram. Export the schema to a PDF (search in the small icons on the left). The PDF should have a nice report of all the tables and their relationships.

Urina answered 28/6 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.