How to create foreign key - phptpoint

How to create foreign key

/ins>

PHP MySQL Foreign Key

What is foreign key

  • A column is called foreign key when it refers to the primary key column of another table.
  • More than one(multiple) tables can be linked to each other by one or more common fields, called foreign keys.
  • foreign keys make it possible to create one-to-one or one-to-many relationships between different tables, and combine data from multiple tables.

Before defining foreign key we have to create Primary key table.
in this example we are creating a table “users” with 4 fields. first field “u_id” is primary key.

How to define Primary key

Output

How to define foreign key

Output



In this example we have create a table “fees” with 5 fields.
in this table “fee_id” is primary key and u_id is foreign key which refers primary key of first table(users).