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.
If you are using phpmyadmin then:
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.
Try out SchemaSpy (http://schemaspy.sourceforge.net/):
Here is a 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
The better way as programmatically speaking is gathering data from
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
table as follows:and another one is
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.
You can get an overview in MySql Workbench by doing the steps below: