I want to setup a constraint that no product names should be the same.
When I create an unique key to the field called ProductName
where the ProductID is the primary key,
when I insert a same ProductName to the database, it overwrites the existing record.
How do I avoid that?
Thanks
Jack
You'll have to show us the code -- ideally the table creation statement and the code used to insert the new record.
(Inserting a new record, using the INSERT statement, never overwrites an existing record - unless there is an trigger which does something behind the scenes, but you don't mention triggers in your post - so there must be something else amiss).
Martin Vajsar wrote:You'll have to show us the code -- ideally the table creation statement and the code used to insert the new record.
(Inserting a new record, using the INSERT statement, never overwrites an existing record - unless there is an trigger which does something behind the scenes, but you don't mention triggers in your post - so there must be something else amiss).
I don't use that tool, but I'd expect that the unique key would be highlighted in the menu, since it should exist. Can you try to click it once more, just for sure? If there already were duplicates when you tried to create the key, the creation could have failed.
If you have duplicates, it seems the unique key doesn't exist or is disabled (if this is possible in MariaDB). In your original post, you said
Jacky Luk wrote:when I insert a same ProductName to the database, it overwrites the existing record.
That would be strange. Duplicates, on the other hand, simply indicate a non-existent or disabled unique constraint.
Martin Vajsar wrote:I don't use that tool, but I'd expect that the unique key would be highlighted in the menu, since it should exist. Can you try to click it once more, just for sure? If there already were duplicates when you tried to create the key, the creation could have failed.
If you have duplicates, it seems the unique key doesn't exist or is disabled (if this is possible in MariaDB). In your original post, you said
Jacky Luk wrote:when I insert a same ProductName to the database, it overwrites the existing record.
That would be strange. Duplicates, on the other hand, simply indicate a non-existent or disabled unique constraint.