473,386 Members | 2,078 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,386 software developers and data experts.

Referential Integrity - How to update a table?

I might have taken a wrong turn trying to do an update on a database
the way I did but maybe somebody can help me out cause I'm stuck..

I have to update 12 tables in a database in a client application of
our company.
Since there were so many changes I didn't want to go record by record
through it.
I included the 12 tables in a 2004.mdb and since everything else's
equal I wanted to delete all the records in the destination table and
insert them from the source table.

There are however many relations between the tables and 'very logical
but annoying' I'm not able to delete the records in the Dest table
because for instance there are records in the 'Order' table related to
the parts table...
I don't want to allow cascading deletes because after the update
things has to be the same as before..

Can I bypass this or is there anyone who has a 'workaround'?

Much appreciated,

Mike
oh.. here's the code:

For Each tdFrom In dbFrom.TableDefs
If Mid(tdFrom.Name, 1, 4) <> "MSys" Then

Set rstSource = dbFrom.OpenRecordset(tdFrom.Name)
Set rstDest = dbTo.OpenRecordset(tdFrom.Name)

If rstSource.RecordCount > 0 Then
rstSource.MoveFirst
End If
If rstDest.RecordCount > 0 Then
rstDest.MoveFirst
End If

Do Until rstDest.EOF
If rstDest.RecordCount >= 1 Then
rstDest.Delete
End If
rstDest.MoveNext
Loop

Do Until rstSource.EOF

With rstDest
.AddNew
For Each fldTo In rstDest.Fields
fldTo.Value = rstSource(fldTo.Name).Value
Next
.Update
End With

rstSource.MoveNext
Loop

'Update ListBox
frmNieuwAssorti.List1.AddItem (tdFrom.Name)
End If
Next
Nov 13 '05 #1
4 2291
Sorry pal, but your stuck. The DBMS will not let you violate referential
integrity. You must execute the deletes in the correct order before you
process the inserts. One way to do this is to create a big delete proc on
the server that takes the primary key of the parent table. Then you can
execute the "inserts" one by one from the client.

"Mike Dole" <m_******@hotmail.com> wrote in message
news:fd**************************@posting.google.c om...
I might have taken a wrong turn trying to do an update on a database
the way I did but maybe somebody can help me out cause I'm stuck..

I have to update 12 tables in a database in a client application of
our company.
Since there were so many changes I didn't want to go record by record
through it.
I included the 12 tables in a 2004.mdb and since everything else's
equal I wanted to delete all the records in the destination table and
insert them from the source table.

There are however many relations between the tables and 'very logical
but annoying' I'm not able to delete the records in the Dest table
because for instance there are records in the 'Order' table related to
the parts table...
I don't want to allow cascading deletes because after the update
things has to be the same as before..

Can I bypass this or is there anyone who has a 'workaround'?

Much appreciated,

Mike
oh.. here's the code:

For Each tdFrom In dbFrom.TableDefs
If Mid(tdFrom.Name, 1, 4) <> "MSys" Then

Set rstSource = dbFrom.OpenRecordset(tdFrom.Name)
Set rstDest = dbTo.OpenRecordset(tdFrom.Name)

If rstSource.RecordCount > 0 Then
rstSource.MoveFirst
End If
If rstDest.RecordCount > 0 Then
rstDest.MoveFirst
End If

Do Until rstDest.EOF
If rstDest.RecordCount >= 1 Then
rstDest.Delete
End If
rstDest.MoveNext
Loop

Do Until rstSource.EOF

With rstDest
.AddNew
For Each fldTo In rstDest.Fields
fldTo.Value = rstSource(fldTo.Name).Value
Next
.Update
End With

rstSource.MoveNext
Loop

'Update ListBox
frmNieuwAssorti.List1.AddItem (tdFrom.Name)
End If
Next

Nov 13 '05 #2
Hi Mike,

If you have a referential integrity set for the tables, then you cannot
bypass it unless you delete all the records from the related table(s). Why
are you trying to delete old records? Maybe you just need to update old
records with the new values?

--
Val Mazur
Microsoft MVP
"Mike Dole" <m_******@hotmail.com> wrote in message
news:fd**************************@posting.google.c om...
I might have taken a wrong turn trying to do an update on a database
the way I did but maybe somebody can help me out cause I'm stuck..

I have to update 12 tables in a database in a client application of
our company.
Since there were so many changes I didn't want to go record by record
through it.
I included the 12 tables in a 2004.mdb and since everything else's
equal I wanted to delete all the records in the destination table and
insert them from the source table.

There are however many relations between the tables and 'very logical
but annoying' I'm not able to delete the records in the Dest table
because for instance there are records in the 'Order' table related to
the parts table...
I don't want to allow cascading deletes because after the update
things has to be the same as before..

Can I bypass this or is there anyone who has a 'workaround'?

Much appreciated,

Mike
oh.. here's the code:

For Each tdFrom In dbFrom.TableDefs
If Mid(tdFrom.Name, 1, 4) <> "MSys" Then

Set rstSource = dbFrom.OpenRecordset(tdFrom.Name)
Set rstDest = dbTo.OpenRecordset(tdFrom.Name)

If rstSource.RecordCount > 0 Then
rstSource.MoveFirst
End If
If rstDest.RecordCount > 0 Then
rstDest.MoveFirst
End If

Do Until rstDest.EOF
If rstDest.RecordCount >= 1 Then
rstDest.Delete
End If
rstDest.MoveNext
Loop

Do Until rstSource.EOF

With rstDest
.AddNew
For Each fldTo In rstDest.Fields
fldTo.Value = rstSource(fldTo.Name).Value
Next
.Update
End With

rstSource.MoveNext
Loop

'Update ListBox
frmNieuwAssorti.List1.AddItem (tdFrom.Name)
End If
Next

Nov 13 '05 #3
Thanks for your help guys!

I didn't want to go through the database table by table, record by
record, check the existence, update the record it it existed, inserted
if not, delete if neccesary, etc, etc.

But at the end I:
- put the (empty) tables who gave me integrity problems in the 'source
database'.

made a script that:
- filled them 'TEMPORARY' with the records from the original database
- deleted these records in the original database
- deleted the records of the remaining tables and inserted the new
ones from my source database.
- Inserted the records from the Temporary tables back into the
original.
- Deleted the records in the temporary tables (in case our customer
would run the script twice..).

It works, you can run it multiple times, but the code's not a pretty
sight to look at..

Might have been easier going through them one at a time and do the
update thing,
why do I always want to take the shortcut when there isn't one??

Anyway if it works under all circumstances and it's stable I'm ok with
it.

It'll probably be the last vb6 app. because the program's will be
obsolete in a few months...
The last year I did all my projects in vb.net, doing the same thing
with datasets would be much easier I guess.
But that's out of the question considering the problems I would run
into with clients not having the framework and all...

Thanks again,

Mike
Nov 13 '05 #4

"Mike Dole" <m_******@hotmail.com> wrote in message
news:fd**************************@posting.google.c om...
| Thanks for your help guys!
|
| I didn't want to go through the database table by table, record by
| record, check the existence, update the record it it existed, inserted
| if not, delete if neccesary, etc, etc.
|
| But at the end I:
| - put the (empty) tables who gave me integrity problems in the 'source
| database'.
|
| made a script that:
| - filled them 'TEMPORARY' with the records from the original database
| - deleted these records in the original database
| - deleted the records of the remaining tables and inserted the new
| ones from my source database.
| - Inserted the records from the Temporary tables back into the
| original.
| - Deleted the records in the temporary tables (in case our customer
| would run the script twice..).
|
| It works, you can run it multiple times, but the code's not a pretty
| sight to look at..
|
For this or future tasks, you might look into a series of update,
append, and delete queries. If the records are identified with primary
keys, for each table you can run:

1. An update query, joining existing records in both tables, updating
all data values from source to the target.
2. An append query, left joining one table to the other, setting a where
condition of PrimaryKey Is Null in the target, which will append any new
records.
3. A delete query, right joining one to the other, where PrimaryKey Is
Null in the source, which will delete records in the target that no
longer exist in source.

Typically you would do the deletions in a table order so that dependent
data is deleted first (i.e. delete the orders, then the customers), and
do the appends in the reverse order (add the new customers, then add
their new orders).

(from comp.lang.basic.visual.database)

Nov 13 '05 #5

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

Similar topics

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...
6
by: Jeff North | last post by:
I'm using Microsoft SQL Server Management Studio Express 9.00.2047.00 and expriencing problems with setting referential integrity on a link table. The tables' schema is as follows:...
80
by: Andrew R | last post by:
Hi I'm creating a series of forms, each with with around 15-20 text boxes. The text boxes will show data from tables, but are unbound to make them more flexible. I want the form to be used...
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...
3
by: Wayne | last post by:
I've inadvertently placed this post in another similar newgroup, and I apologise if you get it twice. I'm building a database that consists of frontend and backend. Some of the lookup 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: 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:
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
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: 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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.