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 KEYorUNIQUE). - Both the foreign key and referenced columns must have identical data types.
- Open PHPMyAdmin and select the database.
- Click on the target table, then go to the Structure tab.
- Click the Relation view link (or Foreign keys tab in newer versions).
- In the Foreign key constraint section, click Add foreign key constraint.
- Select the column(s) that will act as the foreign key.
- In the References section, choose the referenced table and column from the dropdown menus.
- Configure the
ON DELETEandON UPDATEactions 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);