Using PHPMyAdmin to create relationships

Open PHPMyAdmin and log in. Click on the database which contains firsttable and secondtable (creating tables using PHPMyAdmin). Click on the Designer tab at the top (if you don't have one your server may not support relationships and InnoDB). You should get view of the tables something like this:

PHPMyAdmin Designer view

If the tables are just blue bars click on the small arrow icon top left of each table (sometimes twice) and it will expand to show the fields.

You can move the tables around by dragging them. Then press the Save position button in the toolbar at the top.

Click on the Create relation button in the toolbar (fourth from the left in the picture above). It should prompt you to Select referenced key. It means click on the primary key on the one side of the relationship (cdReference in firsttable). Once done it prompts you to Select the foreign key. This is the identical field but in the other table (the many side of the relationship). A small dialog should appear asking you to choose what to do for delete and change. This is all about referential integrity and you should choose RESTRICT and CASCADE:

PHPMyAdmin relationship options

If the dialog has no drop down boxes you will need to check the list of possible problems below and try again so press cancel for now.

If any of this is a mystery you should have read the page on relationships more carefully!

Click OK in the dialog and the two tables should be joined by a relationship. If not check:

PHPMyAdmin inserting related records

Once the relationship is set up MySQL will refuse to let you enter tracks in secondtable unless a matching CD is in firsttable. Try entering tracks for CDs which do exist and which don't. Depending on the version of PHPMyAdmin you may have to check what CDs exist or you may be able to choose from drop downs like these. Try deleting CDs which have tracks and ones which do not.

submit to reddit Delicious Tweet