This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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

doubts pertaining to deletion of record in Table in Rdbms

RabiDas Sharma
Ranch Hand

Joined: Nov 18, 2013
Posts: 68
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

Joined: Aug 22, 2010
Posts: 3606

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.
I agree. Here's the link:
subject: doubts pertaining to deletion of record in Table in Rdbms
Similar Threads
org.hibernate.AnnotationException: No identifier specified for entity
CRUD operations in Hibernate on 2 tables
Hibernate POJOs -- association or no-association ?
I need a bit of explanation about bidirectional relation between two entity beans.
Constraint Violation Question