Understanding Primary And Foreign Keys In Microsoft Access
Sun 21st March 2010
Access is designed as a simple way to build databases for people who are not necessarily technically minded. You do not have to learn an extensive and complicated coding language to create or manage the database. It has an advantage over excel as statistics can be outputted along with reports at the touch of a button via a professional front-end you can create through the use of simple wizards. It is far easier to manage and protect the data in Access than in Excel and can provide a professional front end to your data allowing complete data entry control. There are a few concepts it is important to understand before designing and building the database. One such concept is the use of Primary and Foreign Keys.
Data is stored in access databases in tables. A table is a list of rows and columns containing data. There are usually lots of tables per database. Imagine you needed to build a simple database of customers. The first table you would build is the Customer table, which we will call 'Tbl_Customer.' You must break down the data to its simplest form and not attempt to put all the information into one large table. Consider only the simplest information about the customer. In this table each Customer will have its own record, one record only per customer. Do not be tempted to put all the customer's sales etc in this table as the table will be far too large and impossible to manage. You will also have more than one record per customer and as this table is going to be used to define each customer in the database, that is unacceptable. In Tbl_Customer will be the first name, surname, first line address, second line address, Town, County, Postcode and an ID number that will be an automated number unique to the customer. This number is called a Primary Key. The most important thing to remember is that this number is always unique to each record. No two records will ever have the same number as a Primary Key.
One advantage of having a Primary Key per record is that this number can be placed in other tables as a 'Foreign Key.' If you were to create a table containing all the sales records, there would be a lot of sales information. It would be impractical to have to repeatedly add customer names and addresses in this second table. It would be a waste of space, disorganised and unnecessarily repetitive. It would also lead to duplicates since spelling mistakes on the name, for example, may lead to the same customer being entered several different times. When a report is created to show the sales per customer, one customer may have several different totals due to such errors. Database cleansing to eradicate such problems is a difficult and expensive process so it is preferable to use the Primary Key and Foreign Key method to link tables as a preventative measure.
The Foreign Key is essentially the Primary Key from one table placed in another table in order to join them. When creating a report to see how many sales there are per customer, access will look at a sales record, see that there is a Foreign Key and look in the relating customer table at the matching number to obtain the customer details. Data only needs to be entered once. When creating a front end for the users to enter the sales, a field can be placed on a form, which will automatically show the customer names, and the user can simply choose from the list. Behind the scenes, access will have placed the Primary Key for the selected customer in the Foreign Key field on the sales table.
The Primary Key is therefore vital to define individual records. In reality this is vital when there are likely to be extremely large databases containing thousand if not millions of records. The Foreign Key method allows cross-referencing of tables and records, which is particularly important in modern databases, which can contain thousands of separate tables. The cross-referencing and joining of the tables create relationships between the tables, which is what makes a relational database possible and practical.
You have permission to publish this article for free providing the "About the Author" box is included in its entirety.
Do not post/reprint this article in any site or publication that contains hate, violence, porn, warez, or supports illegal activity.
Do not use this article in violation of the US CAN-SPAM Act. If sent by email, this article must be delivered to opt-in subscribers only.
If you publish this article in a format that supports linking, please ensure that all URLs and email addresses are active links, without the rel='nofollow' tag.
Software Training London Ltd. owns this article. Please respect the author's copyright and above publication guidelines.
If you do not agree to these terms, please do not use this article.
T&D Glazing and Installation Limited Office Administrator Elisabeta Lidia Dajicu
"The course was very useful and efficient.
The trainer was a great in explaining in detail everything we were unclear. Now everything is more organised and structured in my mind regarding the Outlook skills.
Many thanks"
Arcadis Engineer Tim Walker
"Excellent training course!
A great training course that had taken me from complete beginner to confident Power BI user in only two days. The in-depth knowledge and genuine interest from the trainer, Jens, ensured that every step taken was understood.
It is also impressive how well the company has adapted to virtual training. The system that has been adopted ensured that the course was able to run as smoothly as if we were in the classroom.
The reference material and access to the support forum will be very useful for continued development."
Bechtle Print Specialist Tori West
"I really enjoyed the pace and the flexibility of the course.
I think there are some members of the course that could have been less involved and the trainer could have facilitated this."