When are circular references acceptable in database?
Theoretical and practical, any help is appreciated.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Records which point to other records are useful in a database. Sometimes these records form a cycle. This might still be useful. The only real annoyance in practice is avoiding violating the constraints.
For example, if you have a user and transaction table, the user might have a pointer to his last transaction. You need to insert the transaction first, then update the
last_transaction_idto the correct value. While both these records exist you can’t erase them, because theuser.last_transaction_idpoints totransaction.idandtransaction.user_idpoints touser.id. This implies that a user with no transactions has a nulllast_transaction_id. It also means that you have to null that field before you can delete the transaction.Managing these foreign key constraints is a pain but it certainly is possible. There may be problems that arise if you add constraints to the database later which introduce new circular dependencies. You have to be careful in this situation. However, as long as one of the records in the cycle has a nullable foreign-key field, the cycle can be broken and the records can be deleted. Updates are not usually a problem as long as you insert the records in the right order.