Home » mysql » Alter table command to add foreign key

Alter table command to add foreign key

We have two tables company and location. First table manages company details and second table stores location detail of company. one company can span in multiple location.
Initially we have created table for company and location as below.

checking the description of both table

Now We find that one company can exists in multiple location so there is one to many relationship between company to location and to access location detail of company we have to create foreign key reference of company to location table.
We have already create location table so without deleted it again we will alter table to add foreign key refrence of company.
For adding new foreign key column we have to first add a column to location table then we will make it foreign key.
Before proceeding lets see the syntax of alter table command

Syntax

Checking location description you will find that a company_id field is added to location table and it is reffeering to company tables

To show detailed description you can use

Managed by MochaHost - Fast & Easy WordPress Hosting