File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes doubts pertaining to deletion of record in Table in Rdbms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "doubts pertaining to deletion of record in Table in Rdbms" Watch "doubts pertaining to deletion of record in Table in Rdbms" New topic
Author

doubts pertaining to deletion of record in Table in Rdbms

RabiDas Sharma
Ranch Hand

Joined: Nov 18, 2013
Posts: 69
Hi everyone,
I'm having Student table which has the attributes like
student_name,id,address etc.
And another Result table that has attributes like
student_id(foreign key references to Student id),test_id(foreign key references test_id in test table) and Mark.

Now my question is, if I delete Student from student table then his result should get deleted i.e, record corresponding to his student_id
in result table should get deleted automatically.
How can I do that??

thanks in advance.
with regards


Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

I've moved the question to our JDBC forum, where it fits better.

Generally, there are two approaches to this problem:

1) Implement the logic in your application (in Java). You're obviously looking for a different solution, but I think that this one has its advantages too - nothing in your applications gets deleted without you explicitly making it so. Also, in some databases this might perform better than the other option, but it will probably be noticeable for large number of deleted records only.

2) Create a foreign key constrain with the CASCADE DELETE option. Most databases support this construct. If not sure about how to do it, I'd suggest looking up FOREIGN KEY or CASCADE DELETE in your database documentation. This will ensure that the child records will get deleted when the parent table record is deleted.

Also, in most databases it is a good idea to index the foreign key of the child table. It improves the performance of deletes from the parent table.
 
 
subject: doubts pertaining to deletion of record in Table in Rdbms