EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

MySQL INSERT IGNORE

By Payal UdhaniPayal Udhani

Home » Data Science » Data Science Tutorials » MySQL Tutorial » MySQL INSERT IGNORE

MySQL-INSERT-IGNORE

Introduction to MySQL INSERT IGNORE

When we want to insert the data into the tables of the MySQL database, we use the INSERT statement for performing this operation. Mysql provides the facility to add IGNORE keyword in the syntax of the INSERT statement. The usage of the INSERT IGNORE statement is always considered a good practice over the INSERT statement. This is because the INSERT IGNORE statement handles the error that arrives while the addition of the duplicate record and preventing the inconsistency and redundancy of the records in the tables of Mysql.

In this article, we will learn about the general syntax of INSERT IGNORE statement, its working, how the strict mode affects the working of INSERT IGNORE statement and learn the usage with the help of few examples to make the concept more clear.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

How INSERT IGNORE works in MySQL?

When we try to insert multiple records in a particular table of the Mysql database using the INSERT statement and due to some reason there is the occurrence of the error then MySQL will terminate the execution of the query and give the error without inserting any rows in the table that we tried to insert. But when we use INSERT IGNORE instead of just inserting statement then Mysql will give a warning and inserting all the records that were correct leaving and excluding the rows that caused the error.

Syntax

The syntax of the INSERT IGNORE statement is as follows –

INSERT IGNORE INTO table(list_of_columns)
VALUES(record1),
(record2),

Where list_of_columns are the comma-separated names of the column that you wish to insert in the record and the record1,record2,.. are the values of the columns that you have mentioned in the list_of_columns in the same order as they have been mentioned in the list.

Examples to Implement MySQL INSERT IGNORE

Let us create one table named developers that contain one auto_incremented primary key column named id, one column that has a unique constraint on it named name and other related fields that are required to store using the following query statement –

Code:

CREATE TABLE `developers` (
`developer_id` int(11) AUTO_INCREMENT PRIMARY KEY,
`team_id` int(11) NOT NULL,
`name` varchar(100) DEFAULT NULL UNIQUE,
`position` varchar(100) DEFAULT NULL,
`technology` varchar(100) DEFAULT NULL,
`salary` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Output:

related fields

Now, we will try to insert multiple records having a single record value repeated for the name column on which we have defined the unique constraint to avoid insertion of records with the same name. As inserting such records is against the rules and breaking the constraint MySQL will issue an error saying the record with a duplicate value of name column cannot be inserted. In this case, all other columns that we have mentioned in our insert query were correct as per all the constraints and restrictions were also not inserted. We can try executing the following query statement –

Code:

INSERT INTO `developers` (`team_id`, `name`, `position`, `technology`, `salary`) VALUES
( 1, 'Payal', 'Developer', 'Angular', 30000),
( 1, 'Heena', 'Developer', 'Angular', 10000),
( 3, 'Vishnu', 'Manager', 'Maven', 25000),
( 3, 'Rahul', 'Support', 'Digital Marketing', 15000),
( 3, 'Siddhesh', 'Tester', 'Maven', 20000),
( 7, 'Siddharth', 'Manager', 'Java', 25000),
( 4, 'Brahma', 'Developer', 'Digital Marketing', 30000),
( 1, 'Arjun', 'Tester', 'Angular', 19000),
( 2, 'Nitin', 'Developer', 'MySQL', 20000),
( 2, 'Ramesh', 'Administrator', 'MySQL', 30000),
( 2, 'Rohan', 'Admin', NULL, 20000),
( 2, 'Raj', 'Designer', NULL, 30000),
( 1, 'Raj', 'Manager', NULL, 56000);

Output:

query statement

Let us retrieve the records of the developer’s table by using the following query statement

Code:

SELECT * FROM developers;

Output:

MySQL INSERT IGNORE - 3

We can see that none of the records got inserted even though other than the record with the Raj name were correct. Let us now try to insert the records by using the INSERT IGNORE statement instead of the INSERT statement. Our query statement will be as follows –

Popular Course in this category
MySQL Training Program (11 Courses, 10 Projects)11 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (3,022 ratings)
Course Price

View Course

Related Courses
MS SQL Training (13 Courses, 11+ Projects)Oracle Training (14 Courses, 8+ Projects)PL SQL Training (4 Courses, 2+ Projects)

Code:

INSERT IGNORE INTO `developers` (`team_id`, `name`, `position`, `technology`, `salary`) VALUES
( 1, 'Payal', 'Developer', 'Angular', 30000),
( 1, 'Heena', 'Developer', 'Angular', 10000),
( 3, 'Vishnu', 'Manager', 'Maven', 25000),
( 3, 'Rahul', 'Support', 'Digital Marketing', 15000),
( 3, 'Siddhesh', 'Tester', 'Maven', 20000),
( 7, 'Siddharth', 'Manager', 'Java', 25000),
( 4, 'Brahma', 'Developer', 'Digital Marketing', 30000),
( 1, 'Arjun', 'Tester', 'Angular', 19000),
( 2, 'Nitin', 'Developer', 'MySQL', 20000),
( 2, 'Ramesh', 'Administrator', 'MySQL', 30000),
( 2, 'Rohan', 'Admin', NULL, 20000),
( 2, 'Raj', 'Designer', NULL, 30000),
( 1, 'Raj', 'Manager', NULL, 56000);

Output:

MySQL INSERT IGNORE - 4

We can see that 12 rows were affected and the query got executed with one warning. To see the warning we can execute the following command –

Code:

SHOW WARNINGS;

Output:

duplicate

The warning shows that there was one record with the name Raj that had duplicate value for the name column having a unique constraint on it. Let us retrieve the records of the developers table and check which records got inserted using the following query –

Code:

SELECT * FROM developers;

Output:

12 rows

We can see that 12 rows got inserted when in reality we tried to insert 13 rows but as Raj record was duplicated all the remaining rows got inserted successfully. Also, note that the autoincremented column of developer id has got values inserted beginning from 14,15 and so on because the previous 13 records that we tried to insert using just INSERT query caused an error and then all the records that were being inserted got rollbacked leaving the sequence value associated with auto-incremented column updated to 14.

Value Exceeding the Range of the Column

When we try to insert the value that exceeds the limit of the column size defined then the INSERT statement results in the error. However, usage of INSERT IGNORE inserts the truncated value and gives the warning saying that the range exceeded and hence value has been truncated for insertion. Consider the following example:

Length of salary columns is 11 and we try inserting 12- digit number using INSERT statement gives following output –

Code:

INSERT INTO `developers` (`team_id`, `name`, `position`, `technology`, `salary`) VALUES
( 1, 'Pihu', 'Developer', 'Angular', 300000000000);

Output:

MySQL INSERT IGNORE - 7

Let’s try using INSERT IGNORE statement –

Code:

INSERT IGNORE INTO `developers` (`team_id`, `name`, `position`, `technology`, `salary`) VALUES
( 1, 'Pihu', 'Developer', 'Angular', 300000000000);

Output:

MySQL INSERT IGNORE - 8

with warning –

Code:

SHOW WARNINGS;

Output:

MySQL INSERT IGNORE - 9

Let us retrieve and check the inserted value –

Code:

SELECT * FROM developers WHERE name='pihu';

Output:

MySQL INSERT IGNORE - 10

Conclusion

Using the INSERT IGNORE statement instead of just inserting statements is always a good practice as Mysql tries to adjust the values to arrange them in the correct format and inserts the correct records excluding the one that can cause an error.

Recommended Articles

This is a guide to MySQL INSERT IGNORE. Here we discuss an introduction to MySQL INSERT IGNORE, syntax, how does it works with examples. You can also go through our other related articles to learn more –

  1. MySQL count()
  2. SELECT in MySQL
  3. ORDER BY in MySQL
  4. MySQL Database Repair

MySQL Training Program (11 Courses, 10 Projects)

11 Online Courses

10 Hands-on Projects

92+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
MySQL Tutorial
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
  • Basic
    • Introduction to MySQL
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • How To Install MySQL
    • MySQL OpenSource
    • MySQL Commands
    • Views in MySQL
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • Cheat Sheet MySQL
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Functions
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL count()
    • MIN() in MySQL
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • TRUNCATE TABLE MySQL
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL 
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT 
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL COALESCE
    • MySQL Cluster
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL UUID
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL Transaction
    • MySQL sort_buffer_size
    • MySQL Sync
    • MySQL Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - MySQL Training Program (11 Courses, 10 Projects) Learn More