0

I have a unique string column in my database table. The table consists of data column and removed column. If I try to insert a duplicate string into that unique column, it will show duplication error which is what I want. My removed column will indicate 0 as exist, 1 as doesn't exist.

When I try to add the unique string to data column whereby the removed column is 1 (doesn't exist), the query will returns duplication error.

What I want

id data    removed
1  hello   0
2  haha    1 <- remove column, so data column will ignore unique data
3  haha    0 <- New data

How do I insert the unique string into data column with removed equal to 0?

Abel
  • 1,494
  • 3
  • 21
  • 32

1 Answers1

1

Create your unique constraint for data and removed together, not only for data.

ALTER TABLE `table_name` ADD UNIQUE `unique_name`(`data`, `removed`);

But if you would like to save many haha with 1 in removed column, then unique constraint is not an option for MySQL. Have a look at this.

kzharas210
  • 440
  • 1
  • 5
  • 13