Tip: Supress Errors When Duplicate Data Is Inserted in MySQL

Tip: Supress Errors When Duplicate Data Is Inserted in MySQL

There are times in which we need to suppress some errors that are not impacting what we currently are working on.

In MySQL, we may want to suppress an error that is caused by duplicate data insertion with INSERT IGNORE.

Code snippet:

CREATE TABLE `EMPLOYEE` (   `FIRSTNAME` VARCHAR(25) NOT NULL,   `LASTNAME` VARCHAR(25) NOT NULL,   PRIMARY KEY (FIRSTNAME, LASTNAME));INSERT IGNORE INTO EMPLOYEE (FIRSTNAME, LASTNAME) VALUES( 'Ray', 'Neil');

Expected result:

-- First time insertionINSERT IGNORE INTO EMPLOYEE (FIRSTNAME, LASTNAME) VALUES( 'Ray', 'Neil');/* Affected rows: 1  Found rows: 0  Warnings: 0  Duration for 1 query: 0.016 sec. */-- Subsequent insertionINSERT IGNORE INTO EMPLOYEE (FIRSTNAME, LASTNAME) VALUES( 'Ray', 'Neil');/* Affected rows: 0  Found rows: 0  Warnings: 0  Duration for 1 query: 0.000 sec. */ 

?

Visit the DevX Tip Bank

?

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular