473,385 Members | 1,736 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,385 software developers and data experts.

Referential Integrity failure -- any ideas?

I have always taken it for granted that once RI is in place, no orphan
records can be created, and that RI can't be put in place while orphans
exist, but today I came across a situation where that is not true. I am
having a lot of trouble believing my eyes, so I would be very grateful for
anyone's feedback on similar issues.

The situation is the usual Invoice/Invoice Details one: the db has RI in
place on the relatioinship between Invoices and InvoiceDetails so that there
should be no InvoiceDetail records without matching Invoice records;
cascading updates and deletes are enabled. Nevertheless, I have found 28
(out of 14459) detail records that refer to non-existent invoices. I had
always thought this was completely impossible, but there it is. I can clean
these up manually, of course, but I am now concerned about how the situation
might have arisen, and what I can do to prevent it in the future.

Does anyone have any ideas?

Many thanks.
Nov 13 '05 #1
3 2202
Yes, Dave, this is a VERY important issue.

When you create a relation with Referential Integrity, Access stops you from
entering a foreign key value that has no match in the main table's primary
key. It does NOT force you to enter a foreign key value! This is by design,
consistent with database theory, and sometimes very useful (see below).
However, it is a major trap that so *many* people fall into.

To prevent this, you must explicitly set the Required property of your
foreign key field to Yes, so a null is not accepted.

This is one of 6 issues addressed in article:
Common errors with Null
at:
http://members.iinet.net.au/~allenbrowne/casu-12.html

In practice, the records with foreign keys make it into your table when a
user has the main form (invoice) at a new record, and enters a record in the
subform (invoice detail). Setting the Required property of the foreign key
prevents that, but the user does not get the error message until they have
finished entering the subform record and Access then refused to save it. You
might like to use the BeforeInsert event of the form to give them the
message at the beginning:
Private Sub Form_BeforeInsert(Cancel As Integer)
If Me.Parent.NewRecord Then
Cancel = True
MsgBox "Enter the main form record first."
End If
End Sub
As an example of where you might actually want a null foreign key, are there
ever times when you need to write out an invoice, but don't know who the
customer is (sometimes called a cash invoice)? If so, you would want to
allow a record in the Invoice table where the CustomerID is null. In most
cases (like your invoice/invoice details example), a null foreign key is bad
data, and so you need to explicitly block this.

In Jet 4 (Access 2000 and later), there's a little known feature that allows
you to cascade foreign keys to null. As an example, if you have products in
categories, you could set it up so that if you delete a category, any
products in that category are not deleted but have their CategoryID field
set to Null (i.e. they are now uncategorized). Unfortunately, the feature
does not show up in the interface and cannot be programmed with DAO, so you
have to use ADOX code to get this to work.

HTH

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Dave" <dm******@island.net> wrote in message
news:_zQod.318869$%k.422@pd7tw2no...
I have always taken it for granted that once RI is in place, no orphan
records can be created, and that RI can't be put in place while orphans
exist, but today I came across a situation where that is not true. I am
having a lot of trouble believing my eyes, so I would be very grateful for
anyone's feedback on similar issues.

The situation is the usual Invoice/Invoice Details one: the db has RI in
place on the relatioinship between Invoices and InvoiceDetails so that
there
should be no InvoiceDetail records without matching Invoice records;
cascading updates and deletes are enabled. Nevertheless, I have found 28
(out of 14459) detail records that refer to non-existent invoices. I had
always thought this was completely impossible, but there it is. I can
clean
these up manually, of course, but I am now concerned about how the
situation
might have arisen, and what I can do to prevent it in the future.

Does anyone have any ideas?

Many thanks.

Nov 13 '05 #2
In a subform, Access allows a null value for the LinkChild field in a
subform. If your LinkChild field is a Long data type, you should have
noticed that when you create the table the subform is based on, Access
assigns a 0 as the default value for what becomes the LinkChild field. If
you leave the default as 0, you will get an error message if you try to
create a record in the subform with no record in the main form because the
LinkChild field will have a value of 0 and Access does not permit that.
However, if you delete the default value of 0 in the table and then enter a
record in the subform with no record in the main form, Access does not stop
you because Access allows a null value in the LinkChild field. I don't like
to see a 0 in the LinkChild field so I delete the default value of 0. To
prevent the problem you are seeing, I put the following code in the OnEnter
event of the subform control:
If IsNull(Me!NameOfLinkMasterField) Then
MsgBox "Enter Record In main Form First"
Me!NameOfLinkMasterField.SetFocus
End If

--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdatasheet.com
www.pcdatasheet.com
"Dave" <dm******@island.net> wrote in message
news:_zQod.318869$%k.422@pd7tw2no...
I have always taken it for granted that once RI is in place, no orphan
records can be created, and that RI can't be put in place while orphans
exist, but today I came across a situation where that is not true. I am
having a lot of trouble believing my eyes, so I would be very grateful for
anyone's feedback on similar issues.

The situation is the usual Invoice/Invoice Details one: the db has RI in
place on the relatioinship between Invoices and InvoiceDetails so that there should be no InvoiceDetail records without matching Invoice records;
cascading updates and deletes are enabled. Nevertheless, I have found 28
(out of 14459) detail records that refer to non-existent invoices. I had
always thought this was completely impossible, but there it is. I can clean these up manually, of course, but I am now concerned about how the situation might have arisen, and what I can do to prevent it in the future.

Does anyone have any ideas?

Many thanks.

Nov 13 '05 #3
Thanks to Allen and PC Datasheet for those prompt and eye-opening replies.
I"ve been working with Access for quite a while, but have been, until now,
blissfully ignorant of those facts. Much appreciated!

Dave Macmurchie

"Dave" <dm******@island.net> wrote in message
news:_zQod.318869$%k.422@pd7tw2no...
I have always taken it for granted that once RI is in place, no orphan
records can be created, and that RI can't be put in place while orphans
exist, but today I came across a situation where that is not true. I am
having a lot of trouble believing my eyes, so I would be very grateful for
anyone's feedback on similar issues.

The situation is the usual Invoice/Invoice Details one: the db has RI in
place on the relatioinship between Invoices and InvoiceDetails so that there should be no InvoiceDetail records without matching Invoice records;
cascading updates and deletes are enabled. Nevertheless, I have found 28
(out of 14459) detail records that refer to non-existent invoices. I had
always thought this was completely impossible, but there it is. I can clean these up manually, of course, but I am now concerned about how the situation might have arisen, and what I can do to prevent it in the future.

Does anyone have any ideas?

Many thanks.

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sharon Cowling | last post by:
Hi, my problem is this: I have a table called faps_key the unique identifier being key_code: taupo=# \d faps_key Table "faps_key" Column | Type | Modifiers...
2
by: Bob Rogers | last post by:
I seem to have gotten myself into a bit of a pickle. I am trying to delete a record from a table attached to a form, and it complains about not being able to delete a record in a table because...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
7
by: Jimmie H. Apsey | last post by:
Referential Integrity on one of our production tables seems to have been lost. I am running Postgres 7.1.3 embedded within Red Hat kernel-2.4.9-e.49. Within that I have a table with referential...
5
by: Geisler, Jim | last post by:
So, as far as I know, PostgreSQL does not have any way of verifying the loss of referential integrity. Are there any recommended methods or utilities for checking referential integrity in a...
6
by: heyvinay | last post by:
I have transaction table where the rows entered into the transaction can come a result of changes that take place if four different tables. So the situation is as follows: Transaction Table...
3
by: moskie | last post by:
Is there a way to run an alter table statement that adds a constraint for a foreign key, but does *not* check the existing data for refrential integrity? I'm essentially looking for the equivalent...
6
by: CPAccess | last post by:
How do I maintain referential integrity between a main form and a subform, each based upon different (but joined with integrity enforced) table? Here's the situation: I have two tables:...
2
by: ApexData | last post by:
Access2000, using a continuous form. I’m getting a message that say “you cannot add or change a record because a related record is required in table Employee”. This occurs in all my combobox...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.