W3docs

How to create a foreign key in phpmyadmin

To create a foreign key in PHPMyAdmin, follow these steps:

To create a foreign key in PHPMyAdmin, follow these steps:

Prerequisites:

  • The referenced column must be indexed (PRIMARY KEY or UNIQUE).
  • Both the foreign key and referenced columns must have identical data types.
  1. Open PHPMyAdmin and select the database.
  2. Click on the target table, then go to the Structure tab.
  3. Click the Relation view link (or Foreign keys tab in newer versions).
  4. In the Foreign key constraint section, click Add foreign key constraint.
  5. Select the column(s) that will act as the foreign key.
  6. In the References section, choose the referenced table and column from the dropdown menus.
  7. Configure the ON DELETE and ON UPDATE actions if needed, then click Save.

That's it! You have successfully created a foreign key in PHPMyAdmin.

SQL equivalent:

ALTER TABLE your_table
ADD CONSTRAINT fk_name
FOREIGN KEY (your_column) REFERENCES referenced_table(referenced_column);