r/DatabaseHelp • u/Hanxarin • Feb 21 '16
SQL Table Having Composite Key but..
I need help to find a way around something. I have an SQL table which contains some fields, two of which being username (text) and deleted (true/false). I was wondering if it is possible to create a restriction where there cannot be a two of the same usernames with deleted false.
This means that multiple entries may have the same username given that only one of them has deleted set to false.
How may i specify this when creating the table? Thank you.
2
Upvotes
View all comments
Show parent comment
1
u/wolf2600 Feb 21 '16
But he's saying that it would be acceptable to have multiple records with the same username and DELETED = 'T'. Setting the PK to only those two columns were prevent that.
The question is, how do you allow multiple records with the same username, but only one record per username where the DELETED value is 'T'?
I thought about using a CHECK constraint, but can't figure out a way to do it. Which is why I suggested putting the constraint in at the application level rather than at the database level.