7
I had a case in my service these days when it was not possible to delete a record because it was referenced by the table of log
. Logically what generated a call for the IT area to solve.
But that case got me thinking. Although by default I avoid deleting records, usually inactive the record, in which case I need to delete a record. So in this case I need to delete the logs.
Instead of me being totally without log log, it would be better if I create the log tables without Fks, and thus be able to delete the logs without generating error of foreign key
?
Or rather, there would be a way for me to check FK only at the time I enter or update the table records?
The doubt is interesting, but the answer is probably "only depends on each case". I’ve seen great systems that don’t use FK for absolutely nothing, because the application knows how to handle the data. On the other hand, log if it’s just log, it shouldn’t be tied to anything. Probably you are talking about these cases that for lack of using a temporal DB architecture, everything that is edited is logged into a separate table (I find it poor, but often it is what is done).
– Bacco
The question I would ask for DBA is another: why use FK on such a table instead of just storing the ID without forcing a relationship? You might as well keep the original ID and not do it as FK.
– Bacco
@Bacco not making her a FK doesn’t make sense. Because if a record is removed, all entries in the log table that referred to it will have an ID that has no meaning. And, depending on how the Ids of the records are generated, there is a risk of a new record assuming an ID of another already deleted record.
– Gabriel Candez
@Gabrielcandez doesn’t make sense in the specific scenario you imagined, in others can make sense. As I said in the first comment, it depends on each case. If it can delete the user, there may be no problem with the orphaned ID. It may well be that the log information is important to check things that do not depend on the name of the person who did it. Only the real case can tell what makes sense or not.
– Bacco