Reference to composite primary key in Access 2007

倖福魔咒の 提交于 2019-12-18 08:50:30

问题


I have looked around and found some questions similar but they were for SQL Server instead.

Here is a small database structured I have create just to show you the relationships I want to model. Basically it's quite simple, each year has 12 periods and an instance of period-year cannot occur twice (period 9 year 2012 cannot occur more than once ever).

So I thought that the best way to model this would be to have a table period with only one field with values from 1-12, a table year following the same logic (2011,2012...) and since it is an N-to-N relationship I have created the period_by_year table which joins them to be used by rpt_maintenance_kpi. Now the tricky part is that in order to make each combination unique, I have made the both period_no and year_no part of a composite primary key. This solves the problem elegantly in my opinion, but then I am stuck on how to reference this composite primary key from the rpt_maintenance_kpi (or any other table for that matter). I have tried making two joins but this does not seem to work (creates a second rpt_maintenance_kpi table and I believe this will not do what I want to do).

So how could I handle a foreign key to a composite primary key ?

Many thanks in advance.


回答1:


Create the Year or Period relationship with Maintenance, then either double-click the relationship line to edit the relationship or drag the second part (year or period accordingly) to Maintenance and choose Yes when asked if you want to edit the relationship. You can now add the second line, like so:




回答2:


Composite keys are of course helpful and solve your problems, but if you really want to avoid composite key joins, another alternate way of accomplishing what you require is to slightly re-design your table structure. As end reports are always sought based on month & year combination, it would help to have a MthYear dimension (or table). The following can be the entries in that table - with a yymm format:

1301 1302 1303 ... ... ... 1312 1401 1402 ... ... etc....

You can have further attributes in the same table in more descriptive form like second field could be Jan-13, Feb-13....etc.

The MthYear field can be your primary field which can be connected to MthYear in your report maint table. This will not only avoid using composite primary keys but also help you to filter only year or month if you require using wildcard characters in your queries or reports. Hope this is helpful to you......Arvind



来源:https://stackoverflow.com/questions/11730619/reference-to-composite-primary-key-in-access-2007

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!