Solved

Dynamically add the primary key to related tables in Access

Posted on 2014-03-21
6
529 Views
Last Modified: 2014-03-24
Hui Experts,

I have about 6-8 tables in an Access 2010 database that relate to the same primary key.  Let's just say that all the tables relate to suppliers and SupplierID is the primary key in all the tables.

When I add a new record to the master table (ie add a new supplier to the Supplier List) I also want the ID to be added in all the other related tables and queries.

How do I do this and what's it called?  Does this have anything to do with "cascade updating".  Also, if I want to add a new table based on the list of all existing SupplierID numbers, do I have to export and re-import as a new table?

I know this is a rookie question.  Thanks for your help.

-Todd
0
Comment
Question by:thutchinson
  • 3
  • 2
6 Comments
 
LVL 34

Expert Comment

by:PatHartman
ID: 39945794
The short answer is that's not how relational databases work.

It is the "foreign key" (primary key of tblA stored in tblB as a pointer back to tblA) that links all the data.  There is no way to know what rows of tblB should contain a new value added to tblA.

I think you are experiencing a problem with queries that is caused by using the wrong type of join.  The default join type is INNER.  That means you are requesting all rows of tblA that have matching rows in tblB.  So, what happens for example if you join Customers to Orders and a Customer has not yet placed an Order?  The recordset will not contain any rows for the new Customer.  If you wanted to see all customers whether or not they had placed an order, you would use a LEFT join.  The left join says - give me all rows in tblA and also any matching rows in tblB.  So, the columns from tblB would exist but be null for any tblA rows without a tblB record.  The third join type is RIGHT and is the same as LEFT but from the other direction.  When you create a query using the QBE and draw the join line, Access pops up a dialog with three options.  Read them carefully and choose the option that will produce the results you are looking for.

If you have defined relationships in the BE by using the Relationships window, Access will automatically draw join lines when you create new queries using QBE but it will use the default join type of INNER so you will have to double click on the join line to change the join type or switch to SQL View and change it by typing.  If you think that the majority of your joins would be LEFT or RIGHT rather than INNER, you can specify a default join type in the Relationship window and when Access automatically creates new joins for you, it will use this default to specify the join type.
0
 

Author Comment

by:thutchinson
ID: 39946380
Yes, I understand how the joins and the relationships work.  I did not express my question well.  I'm so sorry.   Thanks for the thoughtful response.

I need to think about what I'm trying to accomplish here and restate the question.  I will close this an award the points by Monday if I decide to abandon this question.
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39946501
I forgot to answer the last question.
Also, if I want to add a new table based on the list of all existing SupplierID numbers, do I have to export and re-import as a new table?
Again, this is not how relational databases work.  Select queries and tables are interchangeable for most purposes.  Use queries with selection criteria to isolate subsets of data.  Do not create new tables.  When you create new tables, you will also have to create new forms and queries.

It sounds like you have several 1-1 relationships.  Although there is nothing wrong with that per se, it is unusual so make sure you have created them for the right reasons.  Most people resort to 1-1 relationships because they have too many columns to fit into a single table.  This is a symptom of having one or more repeating groups and the schema should be normalized to remove them.

Have a great weekend.
0
 

Author Comment

by:thutchinson
ID: 39950402
Yes, I have 4 tables with the same primary key (SupplierID) in one-to-one relationships.  Whenever I found that I needed to collect a new type of data, I started a new table.

You are saying that I should have one mega table with many columns.  It makes sense as long as I can remember what I'm storing in all those columns.
0
 
LVL 34

Accepted Solution

by:
PatHartman earned 500 total points
ID: 39950759
One table is easier to work with so unless you need to split off the columns for some particular reason, leave them all together.  Here are some reasons to split.
1. Security - some data is more sensitive than other
2. Sparseness - some data occurs only rarely and so to avoid lots of empty rows, you can put those columns in a separate table.  Since null columns take up only enough room to hold the record lengh field, there is really no technical advantage to this.
3. Some large data fields are used rarely so you want to keep them out of normal processing.

If you have "lots" of columns and keep adding more, be very certain that you are not adding repeating groups.  Those need to go into "many-side" tables.

The Left Join I suggested earlier will solve the problem of working with sparse 1-1 relationships.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now