473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

database structure:primary key debate

hi all
I have a question on database design concerning the hot topic of primary keys.
Here is the scenario:

I have two lookup tables:
TABLE:tbl_Clinic------------------------these are clinic
Primary Key
CliniID ClinicName
1 Clinic 1
2 Clinic 2


TABLE:tbl_Indicators-----indicators recorded by each clinic
Primary Key
Indicator_ID IndicatorName
1 IND 1
2 IND 2
3 IND 3

i then have thse two options to collect indicator data for
each clinic. A clinic can only collect one indicator in a
specified period say a month.


Below are my two options: FacilityData1 and FacilityData2
FacilityData1:has a composite primary key of CliniID and Indicator_ID

FacilityData1
Primary Key
CliniID Indicator_ID Number
1 IND 1 1
1 IND 2 2
1 IND 3 6
2 IND 1 10
2 IND 2 5
2 IND 3 8


FacilityData2: has an auto generated primary key named DataID
FacilityData2
DataID CliniID Indicator_ID Number
1 1 IND 1 1
2 1 IND 2 2
3 1 IND 3 6
4 2 IND 1 10
5 2 IND 2 5
6 2 IND 3 8



Business Rule: i want to avoid duplicate entries where say
Clinic 1 collects IND1 twice in a particular period say a month.

Which of the options is best.
My opinion:
FacilityData1 avoids duplicates but introduces a composites keys which is discouraged
in many books and articles i have read.

FacilityData2 uses a single unique primary key but can introduce duplicates.

any comments????
Aug 19 '08 #1
3 1558
FishVal
2,653 Expert 2GB
Indexes are good alternative to primary key constraints.

Though to meet your business rules (avoid duplicates within a certain time period) you'd better use VBA validation routine on input and/or on table data, unless you will design a separate table storing those time periods.

Kind regards,
Fish
Aug 19 '08 #2
Stewart Ross
2,545 Expert Mod 2GB
Hi. Perhaps for simplification you have left out some details, but since there is no date listed in either of your facility 1 and 2 choices these do not reflect the 'multiple collections within a time period' part of your post.

The clinic ID is a foreign key carried forward from your first table, and the indicator ID from your indicator table. You will need these regardless of introducing an artificial primary key on your facility 1 or 2 table.

In some circumstances the use of a composite key limits what you can do (for example, it is trickier to work with multiple key components if you want to use combo boxes to select particular records on a form than to use single-component keys) - but in my opinion you should use what is most likely to remain correct given future expansion and development of your tables. In this case, if the candidate key for your table is a composite one then use it.

Autonumbers are often used as primary keys, but as they can be added to tables in ways which bear no relation to the contents or uses of the tables I do not myself support using them just to avoid using a composite key. It is inevitable that in any application of any size composite keys will arise (for example every link table used to decompose many-many relationships has a composite key). Using an autonumber key in such circumstances would make no sense, unless there is an advantage to be gained that outweighs the apparent breach of normalisation involved.

There is a considerable difference between data validation (ensuring that the values entered are correct in range, value and type for your application, and that the values satisfy any business rules defined) and table normalisation (ensuring that the tables are optimally structured to minimise data redundancy and to eradicate update anomalies).

In the example you mention, two collections for a clinic within a certain period will not violate any key constraints at all as long as the collection dates are different, no matter how you choose the primary key of that table. Implementing a business rule like "no two collections within X period" involves decisions which restrict what would otherwise be valid data, and this has little or nothing to do with the value or nature of the primary key.

As FishVal points out, this is a data validation matter usually dealt with (in Access) through bespoke VBA coding.

-Stewart
Aug 19 '08 #3
youmike
69
I'd go one step further than Stewart and recommend that you use VBA rather than a macro. VBA is generally more robust, easier to debug and more flexible if you want to build your own error handling in.

Whilst totalling tables often appear to violate normalisation rules, they can speed the process of giving the user what he/she wants dramatically. Users mostly want to see summarised information, not raw data. I tend to keep such routines in stand-alone databases and copy the code to where it's needed, then adjust as needed.

One final point: it is usually good practice to limit the use of such totalling tables to creation of summary reports and not try to have users update the information they contain, just because they may violate normalisation rules. Badly normalised tables are snakes that will bite you just when you don't want them to!
Aug 20 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: James Johnson | last post by:
I have a question about creating a simple database. Here are the 2 tables. They are linked by the primary/foreign key 'client_id'. I think that I understand that part. I have 'KEY...
5
by: Geoff Cayzer | last post by:
At http://www.blueclaw-db.com/tips_tricks.htm I came across a section which is included below and was hoping for some comment on the article. -------------- Almost never use this auto-number...
25
by: Colin McKinnon | last post by:
Hi all, There's lots of DB abstraction layers out there, but a quick look around them hasn't turned up anything which seems to met my requirements. Before I go off and write one I thought I'd...
6
by: asadikhan | last post by:
Hello, I have a bit of a design issue around this application I am developing, and I just want to run it through some of the brains out here. So I have a table called ErrorCheck which...
3
by: vicky | last post by:
Hi All, Can u please suggest me some books for relational database design or database modelling(Knowledgeable yet simple) i.e. from which we could learn database relationships(one to many,many to...
2
by: Hetal | last post by:
I searched online and went through the forums as well, but i could not find a way to capture the database primary key violation exception. Any help will be much appreciated. Thanks, Hetal
3
Fary4u
by: Fary4u | last post by:
Hi i've got Product ID is Autonumber in MS Access database & Primary Key & it's save the record into the database but only problem is how to find that following record save into database...
4
by: umairin | last post by:
hi, i am new to programming and database. please help when ever i am submitting the i am getting the ERROR "Violation of PRIMARY KEY constraint 'PK_property'. Cannot insert duplicate key in...
2
by: cday119 | last post by:
Can someone help me out with this one. I am trying to insert a row of data into an access database. The primary key is set to auto increment. It will work once but the next time you try an insert...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.