doubts pertaining to deletion of record in Table in Rdbms
posted 2 years ago
I'm having Student table which has the attributes like
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??
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.