Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 179377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:23:47+00:00 2026-05-11T14:23:47+00:00

I am working in a project where database items are not deleted, but only

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:23:47+00:00Added an answer on May 11, 2026 at 2:23 pm

    You could add the id value to the end of the name when a record is deleted, so when someone deletes id 3 the name becomes Thingy3_3 and then when they delete id 100 the name becomes Thingy3_100. This would allow you to create a unique composite index on the name and deleted fields but you then have to filter the name column whenever you display it and remove the id from the end of the name.

    Perhaps a better solution would be to replace your deleted column with a deleted_at column of type DATETIME. You could then maintain a unique index on name and deleted at, with a non-deleted record having a null value in the deleted_at field. This would prevent the creation of multiple names in an active state but would allow you to delete the same name multiple times.

    You obviously need to do a test when undeleting a record to ensure that there is no row with the same name and a null deleted_at field before allowing the un-delete.

    You could actually implement all of this logic within the database by using an INSTEAD-OF trigger for the delete. This trigger would not delete records but would instead update the deleted_at column when you deleted a record.

    The following example code demonstrates this

    CREATE TABLE swtest (       id          INT IDENTITY,       name        NVARCHAR(20),       deleted_at  DATETIME   )   GO   CREATE TRIGGER tr_swtest_delete ON swtest   INSTEAD OF DELETE   AS   BEGIN       UPDATE swtest SET deleted_at = getDate()       WHERE id IN (SELECT deleted.id FROM deleted)     AND deleted_at IS NULL      -- Required to prevent duplicates when deleting already deleted records   END   GO    CREATE UNIQUE INDEX ix_swtest1 ON swtest(name, deleted_at)    INSERT INTO swtest (name) VALUES ('Thingy1')   INSERT INTO swtest (name) VALUES ('Thingy2')   DELETE FROM swtest WHERE id = SCOPE_IDENTITY()   INSERT INTO swtest (name) VALUES ('Thingy2')   DELETE FROM swtest WHERE id = SCOPE_IDENTITY()   INSERT INTO swtest (name) VALUES ('Thingy2')    SELECT * FROM swtest   DROP TABLE swtest   

    The select from this query returns the following

     id      name       deleted_at 1       Thingy1    NULL 2       Thingy2    2009-04-21 08:55:38.180 3       Thingy2    2009-04-21 08:55:38.307 4       Thingy2    NULL 

    So within your code you can delete records using a normal delete and let the trigger take care of the details. The only possible issue (That I could see) was that deleting already deleted records could result in duplicate rows, hence the condition in the trigger to not update the deleted_at field on an already deleted row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 164k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer UID and GID are compared to the UID and GID… May 12, 2026 at 12:28 pm
  • Editorial Team
    Editorial Team added an answer You can do something like this: PdfPTable foot = new… May 12, 2026 at 12:28 pm
  • Editorial Team
    Editorial Team added an answer You should make sure you're connecting with an account that… May 12, 2026 at 12:28 pm

Related Questions

I am working on a (database-ish) project, where data is stored in a flat
I am currently working on a project where users need to be able to
I am working on a project that needs to use a database driven MVC
I am working a project where I need to generate a series of classes

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.