Dealing with duplicates and consistency errors

Our product table does not have any UNIQUE index. However, at least one column should contain a unique value; in fact, it would be rather uncommon to have several products with the same name. So, let's add a UNIQUE index:

ALTER TABLE product ADD UNIQUE INDEX unq_name (name);

Now that we have a UNIQUE index, we can use it as an example to discuss how we can deal with duplicates with MariaDB.

Suppose that we want to add a new product, or modify an existing product. In both cases, there is a possibility that the new product name is already in use, causing a conflict. MariaDB supports several statements to automatically handle this conflict:

Syntax

Behavior

INSERT

MariaDB will simply reject the new ...

Get MariaDB Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.