• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

Referential Integrity

I've made some tables which I've defined correct referential integrities for. I've chosen Cascade to be able to delete an entire branch from the hierarchy, meaning if I delete the parent, all its children are also deleted.

This is where it gets problematic. If I try to dlete a parent with Delphi or in Database Desktop, i get the error message "Master has detail records. Cannot delete or modify". I've tried changing the Delete property for this in the Database Explorer, but get an error message saying something like "Cannot edit Delete operation".

How can this be done?

regards,

johan
0
sageryd
Asked:
sageryd
  • 5
  • 3
  • 2
  • +1
1 Solution
 
dotanCommented:
I don't think that setting the referential integrities on a table gives you the ability to do this kind of thing.
You should delete the records starting from the chiled table and work your way up to the master table.
0
 
sagerydAuthor Commented:
Then why is there an option for Cascaded Deletion in the Database Explorer?
0
 
sagerydAuthor Commented:
Well dotan, if Cascaded Deletion doesn't work, what's the easiest way of deleting the child records from the child table when deleting the master record?
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
aacrgCommented:
With which database are you working??
0
 
kretzschmarCommented:
hi sageryd,

you can do it with a query in the beforedelete event of your mastertable like

delete from childtable where MasterIdInChildTable = MasterIdInMasterTable

in code after preparing the sql-string do

query1.execute;
mastertable.Refresh;

meikl
0
 
sagerydAuthor Commented:
aacrq: Paradox.


meikl:
That's exactly what I've been trying to do, but I can't get my code to work properly, could you give an example?

johan
0
 
aacrgCommented:
I guess there are no such things like cascaded constraints...
0
 
sagerydAuthor Commented:
ok. But it would have been nice without having to make additional queries for deleting child records though!
0
 
kretzschmarCommented:
ok, another way

you've a detailtable and a mastertable linked together in a master-detail relationship, now do as follows:

procedure TForm1.MasterTableBeforeDelete(DataSet: TDataSet);
begin
  while Not(DetailTable.Eof) do
    DetailTable.Delete;
end;

meikl
0
 
kretzschmarCommented:
hello?
0
 
sagerydAuthor Commented:
I didn't mean that I don't want to use them queries, because they're really good and fast. Well. During these two days I've been running Delphi like mad, trying to get my program to work properly. And now, there are only a few things left to fix!

The biggest problem at the moment is getting that damn BDE to understand that I only have seven fields, as it brings up errors saying that all fields aren't entered! But that I guess I'll have to try and fix that myself!

Thanx anyway meikl!
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now