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

  • SEARCH
  • Home
  • 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 991083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:03:01+00:00 2026-05-16T06:03:01+00:00

Say I have two tables, user and comment . They have table definitions that

  • 0

Say I have two tables, user and comment. They have table definitions that look like this:

CREATE TABLE `user` (
  `id`       INTEGER NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(255) NOT NULL,
  `deleted`  TINYINT(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY (`username`)
) ENGINE=InnoDB;
CREATE TABLE `comment` (
  `id`      INTEGER NOT NULL AUTO_INCREMENT,
  `user_id` INTEGER NOT NULL,
  `comment` TEXT,
  `deleted` TINYINT(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_comment_user_id` FOREIGN KEY (`user_id`)
    REFERENCES `user` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE
) ENGINE=InnoDB;

This is great for enforcing data integrity and all that, but I want to be able to “delete” a user and keep all its comments (for reference’s sake).

To this end, I’ve added deleted so that I can SET deleted = 1 on a record. By listing everything with deleted = 0 by default, I can hide away all the deleted records until I need them.

So far so good.

The problem comes when:

  • A user signs up with a username (say, “Sam”),
  • I soft-delete that user (for unrelated reasons), and
  • Someone else comes along to sign up as Sam, and suddenly we’ve violated the UNIQUE constraint on user.

I want users to be able to edit their own usernames, so I shouldn’t make username the primary key, and we’ll still have the same problem when deleting users.

Any thoughts?

Edit for clarification: Added following RedFilter’s answer and comments below.

I’m concerned with the case where the “deleted” users and comments are not visible to the public, but are visible only administrators, or are kept for the purpose of calculating statistics.

This question is a thought experiment, with the user and comment tables just being examples. Still, username wasn’t the best one to use; RedFilter makes valid points about user identity, particularly when the records are presented in a public context.

Regarding “Why isn’t username the primary key?“: this is just an example, but if I apply this to a real problem I’ll be needing to work within the constraints of an existing system that assumes the existence of a surrogate primary key.

  • 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. Editorial Team
    Editorial Team
    2026-05-16T06:03:02+00:00Added an answer on May 16, 2026 at 6:03 am

    Add unique constraint on fields(username, deleted)
    Change field type for ‘deleted’ to INTEGER.

    During delete operation (it can be done in trigger, or in part of code where you need actually delete user) copy value of id field to deleted field.

    This approach allow you:

    • keep unique names for active users (deleted = 0)
    • allow delete users with same username several times

    Field ‘Deleted’ can’t have only 2 value because the following scenario will not work:

    1. you create user ‘Sam’
    2. User Sam is deleted
    3. You create new user witn userName ‘Sam’
    4. You try delete user with userName ‘Sam’ – fail. You already have record userName = ‘Sam’ and deleted = ‘1’
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that I have two tables like those: Employers (id, name, .... , deptId).
Let's say I have two tables: Report Comment And assuming I have a database
I'm wondering if this is possible in SQL. Say you have two tables A
I have two tables say, t1 and t2 that have t1.t1_id and t2.t2_id as
Say I have two tables I want to join. Categories: id name ---------- 1
Lets say I have two tables - child and parent with many-to-one relation. What
Scenario: Let's say I have two tables, TableA and TableB. TableB's primary key is
Let's say I have two tables, news and comments. news ( id, subject, body,
Let's say you have two tables, Users and UserRoles. Here's how the two tables
Let's say I have two existing tables, dogs and cats: dog_name | owner ---------+------

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.