foreign key

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Access Training and help » Foreign Key

Foreign Key

resolvedResolved · Low Priority · Version 2003

Valerie has attended:
Access Introduction course

Foreign Key

What is the purpose of a foreign key and how do you use it?

RE: Foreign Key

Hi Valerie,

I have been working through some posts I can anser working from the top of the list so answered Sara's almost identical question first, so you may want to see my answer there. As you ask how to use it I will continue from my answer to Sara here.

So foreign key is the key in another table that provides the link to 'this' one, hence the name foreign.

Establish in your mind which table will be the parent and which one the child, ie which one will have more than one record related to each in the other. Which ever has the many records will have the foreign key. To use it link it to the primary key in the one side of the relationship.

So for albums and tracks, create an Albums table with a field AlbumTitle and save the table letting access create a primary key for you. Re-open it and (in design view) re-name the ID field to Album ID. Then close and save this table.
Create a Tracks table and create your own field called AlbumID and set this to Number type (leaving it in the default Long Integer type). Create another field in the tracks table called TrackTitle (as the default text type). Close and save the tracks table and let access create a primary key for this table for you.

Go to relationships view. Right click and add the two tables to the view (then close the little add in table window). So you see your two tables. Albums has AlbumID as its primary key and Tracks has AlbumID as foreign key.

Drag one AlbumID field on to the other (any order) and a link dialogue window appears, Select enforce integrity and the click create. The link line will apear and the symbols '1' and the infinity symbol will show which side is the 'one' (1) and which side is the many (infinity) of the relationship.

If you place the mouse pointer carefully exactly on the link line you will be able to right click and select Edit Relationship and for the case of albums and tracks I would go to Join Type and select Option 2 so that you can enter albums before you enter track information, or just not have any track listed for some albums if you choose but you will still want to see those albums listed.

In raw table form you can enter one or more album to start with, then in the tracks table enter the album id to which the track belongs then enter the title of the track. If you try entering a track first access wont let you get away with that because it wants to ensure for your sake that each track knows to which album it belongs so there must be an album to relate to first. (Like you have to have a parent for a child to be born). The enforcement you have set with Enforce Integrity has done this. The enforcement is still present when you create a form to enter it but your form design would prpably lead you to enter album title first as you would place that field near the top perhaps. I would recommend having the tracks as a tabular or datasheet subform within the album singular form. (Eg Choose Justified form when creating the album form).

If you understand the above or part of it, you will be more prepared for the intermediate course then I was!

Regards, Mark (delegate)

RE: Foreign Key

Dear Valarie

Thank you for attending Access Introduction course!! I hope you enjoyed the course and benefited from it.

Mark has kindly provided a detailed explanation for which I would like to thank Mark.

In Brief the foreign key is the field in a table that helps in relating the two records in two tables that have relationships. The field that is a primary Key in one table becomes the foreign key in the other table.

If you need to understand relating tables and normalisation I recommend that you attend our Intermediate course where these as well as many more topics are covered.

Please refer to the following link for the course topics:

https://www.stl-training.co.uk/access-courses-in-london-2003-intermediate.php

I hope this can be clear from the example below:

One department can have many employees but one Employee can only be in one dept at one time. So there should be One-to-many relationship between the two tables called Employees and Department.

Employee table will have a primary key called Employee ID
Department table will have a primary key called Department Code.

But in Employee table you will also have the Department code to specify which employee is in what dept. So the dept. code in the Employees table will be the foreign key.

If you are familiar with the process of establishing relationship then you may know that in the relationships window you'll drag the Primary key in the departments table and drop it on the foreign key of the Employees Table i.e. department code (primary key) in the department table to the Department code (foreign Key) in the employees table.

I hope this has answered your query.

If this has answered your query then I would request you to please mark the question as resolved!! If not and you have a specific question related to this then please post it as a new question and we should be able to provide you the solution for it!!

Kindest Regards


Rajeev Rawat
MOS Master Instructor 2000 and 2003
MCAS Master Instructor 2007

I hope this has answered your query.

If this has answered your query then I would request you to please mark the question as resolved!! If not and you have a specific question related to this then please post it as a new question and we should be able to provide you the solution for it!!

Kindest Regards


Rajeev Rawat
MOS Master Instructor 2000 and 2003
MCAS Master Instructor 2007


 

Access tip:

Hiding Multiple Table columns

If you want to hide non-adjacent Access table columns. In Datasheet view, open the table that contains the columns you want to hide.

On the Format menu, click Unhide Columns.
In the Unhide Columns dialog box, clear the check box next to the name of each column you want to hide.
Click Close.

This method makes having to use the Hide Columns command repeatedly unnecessary

View all Access hints and tips


Server loaded in 0.1 secs.