I am working in a project where database items are not deleted, but only marked as deleted. Something like this:
id name deleted
--- ------- --------
1 Thingy1 0
2 Thingy2 0
3 Thingy3 0
I would like to be able to define something like a UNIQUE constraint on the name
column. Seems easy, right?
Let's imagine a scenario in which "Thingy3" is deleted, and a new one is created (perhaps years later). We get:
id name deleted
--- ------- --------
1 Thingy1 0
2 Thingy2 0
3 Thingy3 1
...
100 Thingy3 0
From the user's point of view, he deleted an item and created a new one. Much like deleting a file, and creating a new file. So it's obvious to him that the new item is unrelated and unattached to any data connected to the old item.
That's already handled, since the DB only cares about the id
, and since the new item has an id
of 100 instead of 3, they are utterly different.
My difficulty arises when I want to prevent the user from creating another "Thingy3" item. If I had a UNIQUE constraint that only looked at items that aren't marked deleted
, then I would have solved one problem.
(Of course, then I'd have to deal with what happens when someone does an undo of the delete...)
So, how can I define that sort of a constraint?