devxlogo

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

?

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.