What to do when I want to use database constraints but only mark as deleted instead of deleting?

后端 未结 10 1077
野的像风
野的像风 2020-12-31 20:55

I am working in a project where database items are not deleted, but only marked as deleted. Something like this:

id   name     deleted
---  -------  --------         


        
10条回答
  •  星月不相逢
    2020-12-31 21:25

    Instead of deleted column use end_date column. When user deletes a record add the current date in end_date column. Any records where end_date column is NULL are your current records. Define a unique constraint on two columns name and end_date. Due to this constraint you never have a scenario where valid record name is duplicated. Any time user wants to undelete a record, you need to set the end_date column to null and if this violates the unique constraint then you show a message user to user that the same name already exists.

提交回复
热议问题