How to add a 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;

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 add a foreign key in phpMyAdmin : First table with a field for a foreign key referencing an identifier in another table
First table with a field for a foreign key referencing an identifier in another table
How to add a foreign key in phpMyAdmin : Second table to be referenced by a foreign key
Second table to be referenced by a foreign key
How to add a foreign key in phpMyAdmin : Inserting a foreign key in phpMyAdmin web interface
Inserting a foreign key in phpMyAdmin web interface
How to add a foreign key in phpMyAdmin : Foreign key successfully added in phpMyAdmin
Foreign key successfully added in phpMyAdmin
How to add a foreign key in phpMyAdmin : Foreign key displayed with table field during entry insertion
Foreign key displayed with table field during entry insertion
How to add a foreign key in phpMyAdmin : Selection of the field to display for foreign key references
Selection of the field to display for foreign key references

Photo gallery

  • How to add a foreign key in phpMyAdmin : First table with a field for a foreign key referencing an identifier in another table First table with a field for a foreign key referencing an identifier in another table
  • How to add a foreign key in phpMyAdmin : Second table to be referenced by a foreign key Second table to be referenced by a foreign key
  • How to add a foreign key in phpMyAdmin : Inserting a foreign key in phpMyAdmin web interface Inserting a foreign key in phpMyAdmin web interface
  • How to add a foreign key in phpMyAdmin : Foreign key successfully added in phpMyAdmin Foreign key successfully added in phpMyAdmin
  • How to add a foreign key in phpMyAdmin : Foreign key displayed with table field during entry insertion Foreign key displayed with table field during entry insertion
  • How to add a foreign key in phpMyAdmin : Selection of the field to display for foreign key references Selection of the field to display for foreign key references

Similar articles

Comments (12)

Joan Shaw, 2018-08-19
この情報を読んで素晴らしい時間を過ごして、良い仕事を続けてください
Jon Valdez, 2018-08-19
ขอบคุณมากข้อมูล
Larry Hart, 2018-08-19
ง่ายชัดเจนและอธิบายได้ดีขอบคุณมาก
Kimberly Jenkins, 2018-08-19
Ótimo site, por favor, continue indo
Alice Alexander, 2018-08-19
Eso funcionó bien para mí, no hay necesidad de buscar más
Tinkeragea, 2018-08-19
Nagyszerű oldal, kérem folytassa
randopassionA, 2018-08-19
안녕하세요, 귀하의 기사를보고 그것이 내 문제를 해결하는 데 도움이 감사합니다.
mamanoursec, 2018-08-19
هذا جيد بالنسبة لي ، لا حاجة للنظر إلى أبعد من ذلك
histologit, 2018-08-19
Nevaru ticēt, ka beidzot atradu risinājumu, tas jau ilgu laiku bija murgs, tagad tas ir atrisināts
Pyperermatto, 2018-08-19
Sekeping maklumat yang besar, terima kasih kerana berkongsi
tehnikovx, 2018-08-19
Tidak percaya saya akhirnya mendapati penyelesaian itu, ini adalah mimpi ngeri untuk masa yang lama, kini diselesaikan
LokubusaS, 2018-08-19
Vet ikke hvordan du skal takke deg, men det er rent geni, takk

Leave a comment