views:

86

answers:

4

Lets say I have the following 2 tables in a database:

[Movies] (Scheme: Automatic)
----------------------------
MovieID
Name

[Comments] (Scheme: Manual)
----------------------------
CommentID
MovieID
Text

The "Movies" table gets updated by a service every 10 minutes and the "Comments" table gets updated manually by the users of the database.

Normally you'd just create a simple foreign-key relationship between the two tables with cascading updates and deletes but in this case I want to be able to keep the manually entered data even if the movie it refers to gets deleted (the update service isn't that reliable). This should only be a problem in one-to-many releationships from an automatic table to a manual table. How would you separate the manual and the automatically populated parts of the database?

I was planning to add a foreign-key that isn't maintaining referencial integrity and only cascades updates, not deletions. But are there any pitfalls I should be aware of by doing it this way? I mean, except the fact that I might end up with some of the manual data that doesn't actually reference anything.

Edit / Clarification:

Just to clarify. The example tables are totally made up. In reality the DB will contain objects like servers, applications, application notes, versions numbers etc. Server related information will be populated automatically but some application details will be filled in manually. It could be information like special configurations and such. Even if the server record gets deleted the application notes on that server are still valuable and shouldn't be deleted.

+3  A: 

I'd suggest you use an import table that gets updated by the service and then populate the movies tables from that. Then you get to keep movies that are deleted in the movies table. Possible tagging them as deleted or obsolete, but you'd still be able to keep them for historical purposes.

Dan Sydner
I think you're right it is probably better to mark deleted as such instead of actually deleting them. This will also make the logic in my business entities somewhat simpler and easier to autogenerate. Thanks.
JohannesH
+1  A: 

I think you should use a soft delete for that scenario. I don't think you want to have comments you don't know which movie they belong to.

eglasius
I've added a clarification to the question, the tables aren't actually "movies" and "comments". I should have mentioned that in the original question, sorry.
JohannesH
+1  A: 

Agree; an example route would be to copy the movies table and add a status field which indicates each record's present state (live/checking/deleted). Then the autoimport should go into a temporary table, set the status of all movies to 'checking', then use the temporary table to update the real movies table, setting the movie status to live when it's found in the temporary table. Once complete, set any movie which still has a status of 'checking' to deleted, since they weren't found in the autoimport. At the application end, select any movie which doesn't have status = deleted.

Alistair Knock
Thanks for the suggestion. (+1)
JohannesH
+1  A: 

"I was planning to add a foreign-key that isn't maintaining referencial integrity and only cascades updates, not deletions."

Since you appear to be using surrogate keys, updates will not be relevant to foreign elements. Additionally, since you do not care about orphaning data, then why use the referential constraint at all? You use constraints to ensure that something exists, which you do not appear to require in this situation.

dba4life
You're probably right about the updates. The reason to still include the fk is that I'm using them to autogenerate business entities. But I don't actually think it will behave correctly when data is missing. I will probably just add a "deleted" column like the other answers suggest.
JohannesH