How to set foreign key in phpmyadmin
Adding a foreign key in phpMyAdmin is pretty simple, but the option might be difficult to find.
Open the table in which you want to add the foreign key. Go to the tab Structure, in which you will find a sub menu called Relation View. There, select to column which will hold the foreign key, and the table and column referenced by this key.
As the menus might be pretty difficult to find, refer to below screenshots.
It is also possible to add a foreign key with following SQL code :
ALTER TABLE `table1` ADD FOREIGN KEY (`FK_table2`) REFERENCES `table2`(`ID`) ON DELETE RESTRICT ON UPDATE RESTRICT;
Create foreign key phpmyadmin
Creating a foreign key allow for several options, including making sure that a column only contains entries existing in another table, possibility to prevent deletion of entries in the other table, or to cascade the deletion to referenced tables.
But one of the most useful options, in the phpMyAdmin interface, is to get a dropdown list with entries of the other table, when inserting entries in a table containing foreign keys.
It is even possible to decide which value is displayed by default from a table, when this is one is referenced by a foreign key in another table, as per below example.
How to use foreign key in PHPMyAdmin
In the foreign keys constraints section of a table, an indexed column can be set as a foreign key, simply using the graphical user interface.
How to remove foreign key in PHPMyAdmin
Open the table in which you want to remove a foreign key in the PHPMyAdmin interface. Go to structure > relation view, and there, click on drop in the line corresponding to the foreign key you want to delete relationship in PHPMyAdmin.
PHPMyAdmin relation view missing
The relation view is located under the Structure tab name, after a table has been selected from the PHPMyAdmin graphical user interface.