Sitemap

How to Connect Your Tables with Foreign Keys in phpMyAdmin

2 min readAug 20, 2023

--

  1. First, create more than 1 table to connect
Press enter or click to view image in full size

2. Select one of them, then go to the Structure tab, and select the Relation view button.

Press enter or click to view image in full size

3. Fill in the details

Press enter or click to view image in full size
  • Fill in the Constraint name
    This constraint name helps you easily identify and manage your database's constraints.
    Ex: “fk_notes_users”
  • Select ON DELETE & ON UPDATE
    These clauses define what actions should be taken when a referenced row in the parent table is deleted or updated.

CASCADE: When the parent row is deleted/changed, the child rows are also automatically deleted/changed.

SET NULL: The foreign key values in the child rows are set to NULL when the parent row is deleted/changed.

NO ACTION: The database system prevents the deletion/update of the parent row if associated child rows exist.

Get Andrew Surya’s stories in your inbox

Join Medium for free to get updates from this writer.

RESTRICT: Similar to NO ACTION, prevents deletion/update if associated child rows exist.

I recommend ON DELETE clauses to prevent unintended actions

  • Select a column from the selected table to establish a connection with another table using foreign keys
Press enter or click to view image in full size
  • Adjust Database, Table, and Column values based on your connection.

Here, I connect my “users” table with the “remember_notes” tables using user_id as the foreign key.

Press enter or click to view image in full size

This view is available in [your_database] page -> More tab -> Designer

Press enter or click to view image in full size

You can also create the connection from the Designer View.

I hope it helps!

References:

  1. [Youtube Video]How to create a foreign key in phpMyAdmin
  2. [Webpage]MySQL official website

I just found out about this today and want to save it and share it with more people!~

--

--

Andrew Surya
Andrew Surya

Written by Andrew Surya

Write for me. Share for Everyone