473,770 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2215
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_BeforeInse rt(Cancel As Integer)
If Me.Parent.NewRe cord 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******@islan d.net> wrote in message
news:_zQod.3188 69$%k.422@pd7tw 2no...
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!NameO fLinkMasterFiel d) Then
MsgBox "Enter Record In main Form First"
Me!NameOfLinkMa sterField.SetFo cus
End If

--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdata sheet.com
www.pcdatasheet.com
"Dave" <dm******@islan d.net> wrote in message
news:_zQod.3188 69$%k.422@pd7tw 2no...
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******@islan d.net> wrote in message
news:_zQod.3188 69$%k.422@pd7tw 2no...
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
1889
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 -----------------+-----------------------+----------- key_code | character varying(6) | not null date_key_issued | date |
2
276
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 "the record cannot be deleted because table sales includes related records" Cascade deletes and referential integrity right? Well no. The table I am deleting out of has NO referential integrity with ANY other table. I tried renaming and deleting...
1
3689
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 "access_log", the referential integrity triggers generate these queries: SELECT 1 FROM ONLY "public"."application_type" x
7
2469
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 integrity constraints which no longer work. I do not know how to disable referential integrity on a column in a table. I do not know how to view what Postgres thinks my referential integrity
5
4028
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 PostgreSQL database?
6
4718
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 -TranId -Calc Amount Table 1 (the amount is inserted into the transaction table) - Tb1Id
3
3776
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 to SQL Server's NOCHECK flag. I am able to create equivalent keys in the Relationships screen, as long as the "Enforce referential integrity" box is left unchecked. But when I try to create that relationship with an ALTER TABLE statement, I...
6
4503
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: tblContracts and tblPayments. tblContracts has an autonumber field called IDKey as its primary key. tblPayments also has an IDKey field (Integer datatype). The two tables are linked in a one-to-many relationship on the field IDKey with referential integrity...
2
3923
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 fields that have referential integrity linked to another table. I do not have the field in the table set as required, and I do not want to require the entry. If the record is filled out completely and added to the table, you can go back to the...
0
10231
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.