Categories
MySQL sql

INSERT IGNORE MySQL

The INSERT MySQL statement allows you to add multiple rows into a table but should an error occur, MySQL terminates and returns an error. This means no row is inserted into the table. When you need to ignore the error and allow insertion of valid data the INSERT IGNORE statement is one you can use. MySQL will throw a warning but the valid data will be inserted into the table.

We will create a table members with a unique mobile column, this will be our constraint ensuring we don’t duplicate mobile numbers.

CREATE TABLE members (INT PRIMARY KEY AUTO_INCREMENT,ย mobile VARCHAR(13) NOT NULL UNIQUE
);

Now we insert a new row:

INSERT INTO members(mobile) VALUES('+254711111111');

This works fine, now lets try inserting three rows into the table

INSERT INTO members(mobile) VALUES('+254711111123'), ('+254711111112'), ('+254711111111');