King17SAJ
King17SAJ

Reputation: 5

Finding Foreign Key

This question was asked to me recently, any suggestions are welcome.

There is a form containing company details of say 5 companies.>> C_ID, C_Name, c_Address. (Stored in Table Company)

Below there is a grid view which displays employee records and has insert functionality. the fields of grid view are say>> E_ID, E_Name, E_Address. (Stored in Table Employee)

Now this employee has to be mapped with one of the 5 companies. But there is no reference available. How to find the Foreign key? How to maintain relationship between the two Tables?

Upvotes: 0

Views: 50

Answers (2)

kirk
kirk

Reputation: 1007

Add a new field in Employees Table named "C_ID" and make it a FK(foreign key) to be mapped in Company table..

after adding the field, right click Employees table click design. On the design right click and click relationship. Add new relationship, click Tables and Columns Specifical, on the dropdown choose Customer table and choose C_ID.

Upvotes: 1

StuartLC
StuartLC

Reputation: 107247

You'll need to add the column to map the relationship, and add the foreign key to enforce referential integrity for this relationship.

If the employee can be linked to only one company at a time:

(e.g. in Sql Server):

ALTER TABLE Employee ADD CompanyID INT;
ALTER TABLE Employee ADD CONSTRAINT FK_Employee_Company 
      FOREIGN KEY(Company_ID) REFERENCES  Company(C_ID);

If the employee can be many to many to a company, e.g. part time, contractor, or tracking a history across multiple companies, then you will need a new junction table between employee and company to model this relationship.

Upvotes: 0

Related Questions